Je na čase využít další technologii. Tentokrát použijeme Bluetooth. Budeme rover ovládat ručně! Alespoň si tak vyzkoušíme, zda je lidská obsluha lepší řidič než algoritmus. Na straně Edy použijeme např. BL modul HC-05, a jako ovladač poslouží chytrý telefon s aplikací Remote XY.
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h>
#include <RemoteXY.h>
// RemoteXY connection settings
#define REMOTEXY_SERIAL_RX 2
#define REMOTEXY_SERIAL_TX 3
#define REMOTEXY_SERIAL_SPEED 9600
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
{ 255,2,0,0,0,12,0,8,13,0,
5,15,25,0,54,54,2,26,31 };
// this structure defines all the variables of your control interface
struct {
// input variable
int8_t joystick_x; // =-100..100 x-coordinate joystick position
int8_t joystick_y; // =-100..100 y-coordinate joystick position
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/* defined the right motor control pins */
#define PIN_MOTOR_RIGHT_UP 8
#define PIN_MOTOR_RIGHT_DN 7
#define PIN_MOTOR_RIGHT_SPEED 6
/* defined the left motor control pins */
#define PIN_MOTOR_LEFT_UP 9
#define PIN_MOTOR_LEFT_DN 10
#define PIN_MOTOR_LEFT_SPEED 11
/* defined two arrays with a list of pins for each motor */
unsigned char RightMotor[3] =
{PIN_MOTOR_RIGHT_UP, PIN_MOTOR_RIGHT_DN, PIN_MOTOR_RIGHT_SPEED};
unsigned char LeftMotor[3] =
{PIN_MOTOR_LEFT_UP, PIN_MOTOR_LEFT_DN, PIN_MOTOR_LEFT_SPEED};
/*
speed control of the motor
motor - pointer to an array of pins
v - motor speed can be set from -100 to 100
*/
void Wheel (unsigned char * motor, int v)
{
if (v>100) v=100;
if (v<-100) v=-100;
if (v>0) {
digitalWrite(motor[0], HIGH);
digitalWrite(motor[1], LOW);
analogWrite(motor[2], v*2.55);
}
else if (v<0) {
digitalWrite(motor[0], LOW);
digitalWrite(motor[1], HIGH);
analogWrite(motor[2], (-v)*2.55);
}
else {
digitalWrite(motor[0], LOW);
digitalWrite(motor[1], LOW);
analogWrite(motor[2], 0);
}
}
void setup()
{
RemoteXY_Init ();
/* initialization pins */
pinMode (PIN_MOTOR_RIGHT_UP, OUTPUT);
pinMode (PIN_MOTOR_RIGHT_DN, OUTPUT);
pinMode (PIN_MOTOR_LEFT_UP, OUTPUT);
pinMode (PIN_MOTOR_LEFT_DN, OUTPUT);
}
void loop()
{
RemoteXY_Handler ();
/* manage the right motor */
Wheel (RightMotor, RemoteXY.joystick_y - RemoteXY.joystick_x);
/* manage the left motor */
Wheel (LeftMotor, RemoteXY.joystick_y + RemoteXY.joystick_x);
}