# irobot create 2: random changes in driving speed

I am trying to make the Create2 dance a specific choreography. I´m not using the sensors, I just let it drive and turn for a specific amount of time. In general it works fine.

But after working with it for a while, it suddenly doesn't do a full turn any more. It seems like something changes and than driving in a specific angle and turning in place takes a bit longer or shorter. It also happens, that Roomba doesn't drive straight any more but slightly turns to the right. And than it changes again and it´s back to the first configuration.

I tried changing the baudrate, I did manual resets, I charged Roomba and tried in full battery power and low battery power but I can´t find an explanation. The bug occurs without changing code or anything. Does anyone have an idea, what the problem might be?

Another problem that I ran into: I let Create2 play songs. Most of the time it works. But from time to time I get some weird issues. The tune doesn't play until the end but lingers on one note for a moment. Than the tune stops. Often the next song won´t play at all. Next time I run the code it works just fine. Any ideas?

The whole thing makes me go crazy because it messes up the whole choreography and makes it totally unreliable...

My set up: Arduino Nano connected to serial port of Create2. Arduino-Create2 Libary https://github.com/brinnLabs/Create2

// Include Create2 libary
#include <iRobotCreate2.h>

// connect Roomba TX to pin 10 and Roomba RX to pin 11,
// connect Roomba BaudRateChange to pin 5
iRobotCreate2 roomba(true, 10,11,5);

// set pins for LED and Startbutton:
const int ledPin =  13;
const int buttonPin = 4;
int incomingByte = 0;

// variable for reading the Startbutton status
int buttonState = 0;

// full rotation of roomba with VEL 100
int fullspin100 = 7319;
int fullspin200 = 3659;
int fullspin300 = 2439;

void setup(){

Serial.begin(115200);

// set pinmode for LED and Startbutton
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);

// reset BaudRate to 19200
roomba.resetBaudRate();
delay(2000);

// start communication and safe mode
roomba.start();
roomba.safeMode();
delay(1000);

// song nr. 1
Notes songNotes[16];
songNotes[0].note = 31;
songNotes[1].note = 43;
songNotes[2].note = 31;
songNotes[3].note = 43;
songNotes[4].note = 55;
songNotes[5].note = 67;
songNotes[6].note = 79;
songNotes[7].note = 91;
songNotes[8].note = 103;
songNotes[9].note = 91;
songNotes[10].note = 79;
songNotes[11].note = 67;
songNotes[12].note = 55;
songNotes[13].note = 43;
songNotes[14].note = 31;
songNotes[15].note = 103;

songNotes[0].duration = 8;
songNotes[1].duration = 8;
songNotes[2].duration = 8;
songNotes[3].duration = 8;
songNotes[4].duration = 8;
songNotes[5].duration = 8;
songNotes[6].duration = 8;
songNotes[7].duration = 8;
songNotes[8].duration = 8;
songNotes[9].duration = 8;
songNotes[10].duration = 8;
songNotes[11].duration = 8;
songNotes[12].duration = 8;
songNotes[13].duration = 8;
songNotes[14].duration = 8;
songNotes[15].duration = 8;

roomba.createSong(1, 16, songNotes);

// turn on PowerLed red to indicate that roomba is ready for dancing
roomba.setPowerLEDs(255, 255);
delay(10);
}

void loop(){
// listen to input from the button
buttonState = digitalRead(buttonPin);

// if button is pressed, start dancing
if (buttonState == LOW) {
digitalWrite(ledPin, LOW);
dance();

}
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
}

void dance(){

roomba.turnCW(100, 0);
delay(fullspin100);
roomba.drive(0, 0);
delay(1000);

roomba.turnCCW(100, 0);
delay(fullspin100);
roomba.drive(0, 0);
delay(1000);

roomba.turnCW(200, 0);
delay(fullspin200);
roomba.drive(0, 0);
delay(1000);

roomba.turnCCW(200, 0);
delay(fullspin200);
roomba.drive(0, 0);
delay(1000);

roomba.turnCW(300, 0);
delay(fullspin300);
roomba.drive(0, 0);
delay(1000);

roomba.turnCCW(300, 0);
delay(fullspin300);
roomba.drive(0, 0);
delay(1000);

roomba.playSong(1);
delay(1300);

}


## 3 Answers

I think I found the problem that was causing the errors. I was sending serial commands to the create 2 via Arduino. To make things easier I was using the create2 libary that summarizes commands (https://github.com/brinnLabs/Create2).

It looks like the freezing of the sound results from some bugs in that libary (maybe the calculation of the songlength). And also the changes in driving speed and the problem of not driving straight seem to disappear when i send all the commands directly (up to now they didn`t appear again).

With the create2 libary I used serial communication. Now I´m using soft serial. Not sure if that was also part of the problem.

I really liked the create2 libary, because it seemd to be the best libary available for create2 that allows easy control for all functions. But be sure to check that piece of your setup when problems occure.

Looking at your code quickly, it looks like you are sending velocity commands and then waiting for some amount of time to execute your motions, without using any encoder feedback to verify the robot is moving the amount you expect. I would suggest that you implement a control loop to make your movements more accurate. I suspect a simple PI controller would get you where you need to be, though then your timings might not work out. You'd need to figure out a way to synchronize with your external track.

Please note that I am an iRobot employee, but the postings on this site are my own and don't necessarily represent iRobot's positions, strategies, or opinions.

I see that working with control loops is a more elegant way of controlling the robot. But I’m pretty sure that this isn’t the problem. I uploaded a video to demonstrate the strange behaviour:

https://www.youtube.com/watch?v=hms2z7WoUgQ&feature=youtu.be

One time it does exactly what it is supposed to do and the next day it drifts to the left, turns at different speed and ends up in the wrong place. But Running the program several times in a row it repeats the movement (doesn’t matter if it chose the right or the wrong) precisely. It’s doing exactly the same movement in every run (expect when it is changing all parameters mysteriously) So the "drive motor - wait - stop" method is actually accurate enough for me. And a control loop also wouldn't prevent the robot to drift to the left instead of driving straight. That's why to me it seems to be a different problem. Could there be something wrong in the firmware? This is my version: r3-robot/tags/release-stm32-3.7.5:6216 CLEAN

Any other ideas?

Thanks for helping!