Rover 5 can now explore a confined space – various code changes

Getting the exciting bit out there first here is a video of my Rover 5 driving itself around an enclosed space. It definitely isn’t perfect at all yet but I’m pretty happy that I got things to this state with a few basic code changes.

 

So, to get to this state I had 3 main code changes.

First I needed to have a function to Rotate the rover. Rather than create a new function I decided to modify Go() somewhat. Now we can pass Go two parameters, one for the left motors speed and one for the right motors speed. As going “forwards” for each side’s motors is a different direction (I hope that makes sense!) the code is a bit more complex than it would be otherwise.

void Go(int speed, int speed2) // pretty sure this is right_speed, left_speed but need to check
{
if(speed<0) //if we want this side to go backwards then do the following.
{
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
speed=speed*-1; //get the speed back to a positive value which can be used to drive a PWM output to send a speed to the motors.
}
else //otherwise, forward it is for this side.
{
digitalWrite(M1,HIGH);
digitalWrite(M2,HIGH);
}
if(speed2<0) //if we want this side to go backwards then do the following.
{
digitalWrite(M3,HIGH);
digitalWrite(M4,HIGH);
speed2=speed2*-1; //get the speed back to a positive value which can be used to drive a PWM output to send a speed to the motors.
}
else //otherwise, forward it is for this side.
{
digitalWrite(M3,LOW);
digitalWrite(M4,LOW);
}

analogWrite(E1,speed); //PWM Speed Control (right motors)
analogWrite(E2,speed); //PWM Speed Control
analogWrite(E3,speed2); //PWM Speed Control (left motors)
analogWrite(E4,speed2); //PWM Speed Control
}

So, we can now go Go(255,255) to go forwards at full speed, or Go(255,-255) to rotate left at full speed.

The next part I wanted to do is to give me a way to put the Rover 5 in to debug mode where it does nothing but output sensor data and does not drive it’s tracks at all. It’s pretty annoying when making changes to code to have the motors whirling away all the time. Basically, all we do here is to input a “d” over the serial to go in to debug mode, and input a “n” to go back to normal or operational mode.

The following code goes in my main loop function and handles reading in from serial.

//We'll reset readString back to nothing in case it was set last time this loop was run
readString="";

if (Serial.available() > 0)
{
// read the incoming byte:
char character = Serial.read();
readString += character; //makes the string readString

if(readString=="d") //if user wants to put us in debug mode
{
debug=1;
Serial.println("Debug Mode On");
Stop(); //it is key to run this to set all motor speeds to zero!
delay(500); //not actually required - just a self imposed delay.
}
else if(readString=="n") //if user wants to put us in operational mode
{
debug=0;
Serial.println("Debug Mode Off");
//delay(500);
}
}

Finally, I need to adapt the code to turn left when sensing objects in front rather than just stopping. But at the same time I didn’t like the behaviour to be instantly turning left or instantly going forward again. So I added some basic states that get set (go, or turn) and then detection of changes of states. When state has been changed the Rover 5 is stopped and object detection paused for a moment.

This also went in my main loop()

if(debug==0)
{
if(dist>35) //if IR Dist sensor reports object not close in front then go forward
{
currentState="go";
if(currentState!=previousState) //if we have just changed state to "go" then stop and wait for 300 msec
{
Stop();
delay(300);
}
Go(255,255);
previousState=currentState;
}
else //if IR Dist sensor reports object close in front then turn
{
currentState="turn";
if(currentState!=previousState) //if we have just seen an object then stop the rover and wait for 1 sec
{
Stop();
delay(1000);
}
Go(220,-220);
previousState=currentState;
}
}
delay(100); //delay 100 at end of loop. Mainly used right now to keep output over serial to a human readable amount.

All in all things seemed to work pretty good for now.

Next up I think I’ll try and get the ultra-sonic sensor hooked up and reading from it. However, I’m now worried that I mounted this too high on the chassis to be useful.

IMG_0452

Share on Facebook

Leave a Comment