XBee Test Sketch

181214KA

Hello again!

Here’s the premise: you hit key on keyboard, robot moves in requested direction.

I initially wanted to use the up/down/right/left arrows on they keyboard to control the robot, but those don’t “send” as commands through XTCU, which is the program I’m using to control the XBee hooked up to the computer (thus, the keyboard). So, I’ve opted to use i/k/j/l, respectively, instead (I’m right handed). (Tip: keep track of caps-lock).

I was aiming for near-instantaneous control, like: I press ‘i’, it moves forward, and as soon as I let go, it stops moving. Ideas included putting all the letters into ‘if-else if’ statements, and then at the end writing an else if like:

else if(rx_byte != 'i' && rx_byte != 'j' && rx_byte != 'k'  && rx_byte != 'l'
{
setSpeeds(0);
}

Unfortunately, this doesn’t work because “nothing” isn’t received as a command… ie. I’d have to press a different key, like the spacebar, for the robot to stop moving. Which works the same as just doing this:

else
{
setSpeeds(0);
}

So, still looking for ways to interpret ‘silence’.

In the meantime, this is what I’ve gone with: the code pasted in below has a 75 second delay, but I’ve also tried a 10 second delay, and both videos are also linked.

75 second delay: 181214KA_XbeeTest_75ms

10 second delay: 181214KA_XBeeTest_10ms

You’ll note that in the 10-second video, the robot looks kind of like a slow chicken–for the same reason doing this doesn’t work:

else if(rx_byte == 'l')
 {
turn(-100);
 setSpeeds(0);
}

The second command is to stop, so the board goes bleep-bloop and the ‘turn’, in this case, took place in such a  teensy amount of time that it essentially didn’t happen.

Actual Code

(This looks similar to Roomba in the important parts, like setting up the robot class/methods, etc.)

class Motor{
 private:
 int pin_enable; //this is the thing that you set the speed with
 int pin_forward; //basically each motor has two of these pins, 
 int pin_backward; //and you use them to reverse polarity and switch direction
 
 public:
 Motor(int enable, int one, int two){
 pin_enable = enable;
 pin_forward = one;
 pin_backward = two;
 //in the old code they essentially combined these last two steps with something fancy
 //oh well, this is easier to understand lol
 }
 void motorSpeed(int Speed){
 if(Speed > 0 || Speed == 0){ //here, I'm asking which way to set the polarity, with the idea being that I can input pos. or neg. speeds into the function
 digitalWrite(pin_forward, HIGH);
 digitalWrite(pin_backward, LOW);
 analogWrite(pin_enable, Speed);
 }
 else{
 digitalWrite(pin_forward, LOW);
 digitalWrite(pin_backward, HIGH);
 analogWrite(pin_enable, -Speed);
 }
 }
};

//Reverse Polarity protector is plugged into Pin 12 on the MEGA shield
Motor right_motor(5, 42, 43);
Motor left_motor(6, 44, 45);
char SIDE;
int right_speed;
int left_speed;

void setSpeeds(int bothSpeed){
 right_motor.motorSpeed(bothSpeed);
 left_motor.motorSpeed(bothSpeed);
 }

void turn(int bothSpeed)
{
 right_motor.motorSpeed(bothSpeed);
 left_motor.motorSpeed(-bothSpeed);
}

void setup() {
 // initialize serial ports
 Serial.begin(9600); // USB serial port 0
 Serial.println("Starting up...");
 Serial3.begin(9600); // serial port 3
}

byte rx_byte = 0; // stores received byte

void loop() {
 // check for data byte on USB serial port
 if (Serial.available()) {
 // get byte from USB serial port
 rx_byte = Serial.read();
 // send byte to serial port 3
 Serial3.write(rx_byte);
 }
 // check for data byte on serial port 3
 if (Serial3.available()) {
 // get a byte from serial port 3
 rx_byte = Serial3.read();
 // send the byte to the USB serial port
 //Serial.write(rx_byte);
 //Serial.println(rx_byte);
 if(rx_byte == 'i')
 {
 Serial.println("up");
 setSpeeds(200);
 delay(75);
 setSpeeds(0);
 }
 else if(rx_byte == 'k')
 {
 Serial.println("down");
 setSpeeds(-200);
 delay(75);
 setSpeeds(0); 
 }
 else if(rx_byte == 'j')
 {
 Serial.println("left");
 turn(100);
 delay(75);
 setSpeeds(0);
 }
 else if(rx_byte == 'l')
 {
 Serial.println("right");
 turn(-100);
 delay(75);
 setSpeeds(0);
 }
 else
 {
 //don't move
 setSpeeds(0);
 }
 }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA

*