Programming tutorial - Robosoc-Southampton/Tutorials GitHub Wiki
We'll be using some code I've written to simplify controlling the robot. You can grab the code from this repository. (Just copy and paste that into a new Arduino sketch.)
The code provides some functions to use, listed below.
Don't worry if there's anything here you don't know how to do. It's deliberately vague about some of the details, so just ask and we'll help you out.
If you're looking from home, Google is your friend. Just try not to ask anything specific about the functions I've written like "how to use setSpeeds", but instead ask C++ related questions like "how to call a function in C++".
You will need to wire up the robot as shown here (we know it's a bit difficult to read but you can see which wires connect to what ports by hovering over them): https://www.circuito.io/app?components=9442,10190,11021,13959,7654321
These functions control the speeds of the driving motors on the robot. The speed should be between -255
and 255
inclusive.
Example usage:
setLeftSpeed(255);
setRightSpeed(-128);
setSpeeds(128, 128);
This function lets you set the angle of the "tail" at the back of the robot. You pass an angle in degrees (0
-180
inclusive).
Example usage:
setTailAngle(170);
setTailAngle(0);
This function reads the distance in centimetres using the ultrasonic sensor and returns it.
Example usage:
int distance = readDistance();
Let's get some movement going. Spinning on the spot is a nice way to start so you don't accidentally drive off a table.
To spin on the spot, you want one wheel going forward and one going backwards, both at the same speed.
To do this, use the motor speed functions (e.g. setSpeeds
). You'll want to put it in either setup
or loop
as all the instructions you write in your code need to go in a function.
Try moving the tail back and forth between two positions, say 60
and 120
degrees, using setTailAngle
. You'll need to wait some time between setting the angles so the servo has a chance to get to the angle you tell it to go to. You can use a built-in Arduino function called delay
that waits for a specified number of milliseconds.
As an example,
setTailAngle(60); // move the tail to 60 degrees
delay(1000); // wait for a second
setTailAngle(120); // move the tail to 120 degrees
If it's not already doing so, try to get it moving back and forth forever.
Hint: don't just copy and paste the code you've got a load of times, there's a better way.
Now you've driven and moved the tail, let's read a distance using the ultrasonic sensor on the front.
Use the readDistance
function and print the distance using Serial.println
.
If you're up for a challenge, try reacting to the distance in some way. For example, drive forwards until the distance is less than 50cm, or make the tail move based on the distance being read.
An if statement lets you make a one-off decision and run some code only if a condition is met. For example, you might want to set the motor speed to 0 if the distance is less than 50cm.
int distance = readDistance();
if (distance < 50) {
setSpeeds(0, 0);
}
The general syntax is this:
if (<condition>) {
<code>
}
else {
<code>
}
where the else
is optional.
A while loop lets you run some code repeatedly "while" a condition is true. For example, while a distance is greater than some threshold, wag the tail.
while (readDistance() > 50) {
setTailAngle(60);
delay(1000);
setTailAngle(120);
delay(1000);
}
The general syntax is this:
while (<condition>) {
<code>
}
For loops are pretty flexible, but are generally used to do something a specific number of times using a counter.
First of all, let's do it with a while loop.
int numberOfTimes = 3;
int counter = 0;
while (counter < numberOfTimes) {
doThing();
++counter;
}
This will run doThing
exactly 3 times, but it's kinda long. Wouldn't it be cool if you could group the counting part together in one concise line, something like...
int numberOfTimes = 3;
for (int counter = 0; counter < numberOfTimes; ++counter) {
doThing();
}
Yeah, it's a bit weird, but C++, you get used to it.
You don't need the variable for the 3
, either.
for (int i = 0; i < 10; ++i) {
doThing(); // this will run 10 times, neato
}
There are a load of challenges here. Feel free to do as many or as few as you want, and skip any you cba to do.
Don't forget to take a look at the helper functions above.
- Move the tail to an angle based on the distance in front of the robot (i.e. small angle when small distance, larger angle when larger distance).
- Wag the tail back and forth while driving.
- Drive to 30cm from a wall.
- Drive only when nobody is standing in front of the robot.
- Drive only when someone is holding their hand in front of the robot.
- Drive forward as close to 1m as you can.
- You can use distance measurements or work out the time delay for this.
- Drive in a circle with ~50cm radius.
- The 50cm is very approximate.
- Drive forward and avoid obstacles (by turning to the side and driving around them).
- Extra points if it goes back to its original direction after driving round!
- Drive in a square.
- N-sided Polygons are also cool as long as it's deliberate.
- Do a dance.
- Points for creativity.
- Follow a person.
- Assume the person isn't an ass and that they walk very slowly.
- Drive in a figure of eight.
- Write your name using the robot.
- Maybe just the first few letters.
- Do all the easy challenges without using the helper functions (i.e. control the motors yourself, trying not to copy and paste, but you can use them for reference because I'm not that sadistic).