Skip to main content

Introduction

We need to transport some very unstable nuclear waste across the planet, so we must program the robot to move as smoothly as we can.

  1. Some nuclear waste has been found near the base, and we need to move it to the other side of the planet as it is very dangerous.
    • Some nuclear waste has been found near the base, and we need to move it to the other side of the planet as it is very dangerous.

    • The nuclear waste is extremely unstable, so we need to make our robot accelerate and decelerate smoothly so it doesn't explode!

    • To do this, we are going to learn about variables.

  2. Put your robot together just like the picture! The connections should be:
    • Put your robot together just like the picture! The connections should be:

    • Left Motor > M1

    • Right Motor > M2

  3. Build the program in the picture!
    • Build the program in the picture!

    • Before you try programming your robot, what do you think this program will do?

  4. A good way to understand variables is to think of them like your lockers at school.
    • A good way to understand variables is to think of them like your lockers at school.

    • To use a locker, you need to put your name on it - we do the same with a variable, and you can call it anything you want!

    • We can then put whatever we like inside the locker - books, bags, clothes, anything! We can do the same with variables, but for now we'll just put numbers inside them.

    • We can add, remove and change things in the locker whenever we like - its the same for the number in the variable!

    • Most usefully, we can go back to the locker or variable at any time and see what's in it (so long as we know the name of the locker or variable!).

  5. If you don't quite understand, don't worry - for now, just remember we can do these things with variables:
    • If you don't quite understand, don't worry - for now, just remember we can do these things with variables:

    • Call them anything we like (variable name)

    • Store any number we like inside them (variable contents)

    • Change the contents at any time (add, subtract, multiply, divide and so on)

    • Access the contents at any time, so long as we know the name of the variable.

    • To make a new variable in Python, you simply type its name (it can be whatever you want), followed by an equals sign, followed by the value you want to set it to.

  6. Starting with a new program, make a new variable called speed and set it to 0.
    • Starting with a new program, make a new variable called speed and set it to 0.

    • Access the speed variable by setting both motors running using the speed variable.

    • Notice how we only use 1 equals sign when we make the variable and not 2 like in an if statement?

    • Using 1 equals sign sets one thing equal to another.

    • Using 2 equals signs checks if 1 thing is equal to another.

    • It is very important to always use the right number of equals signs!

  7. Now let's try changing the motor speeds by changing the variable!
    • Now let's try changing the motor speeds by changing the variable!

    • First, change the line that makes the speed variable, so speed is set to 50.

    • Then, add a sleep of 1 second after the drive_motor line so the robot moves forwards for a bit at the first speed.

    • Then, add a line that says: speed=100

    • Now add another drive_motor line to set the motors running at speed again - but now speed has changed, so the motors should change speed too!

  8. You might be thinking - why bother using a variable to do this? We could have just used the simple program in the picture! Well, what if we wanted to increase the speed of our robot (accelerate) slowly?
    • You might be thinking - why bother using a variable to do this? We could have just used the simple program in the picture!

    • Well, what if we wanted to increase the speed of our robot (accelerate) slowly?

    • Even if we started the speed at 0 and increased the speed by just 10 every second (0,10,20,30,40......), the program would require 10 motor blocks!

    • Have a look at the second picture to see this program - it is far too long.

  9. By combining variables and loops, we can program things like acceleration very easily.
    • By combining variables and loops, we can program things like acceleration very easily.

    • Have a look at the program in the picture - can you work out what is going on?

    • The range function in the for loop has an optional third number: how much to increase the iterable by each time. In this case, we start from 0 and increase speed by 10 each time the loop runs.

    • So the for loop runs 10 times - each time it runs, speed is increased by 10, and the speed of the motors is set to speed!

    • See how much shorter this program is?

    • Write this program and test it out.

    • If you can't remember how for loops work, have a look back at the previous section.

  10. We're nearly there! Let's change some things in our program to make the robot accelerate really smoothly, so we don't set off the nuclear waste.
    • We're nearly there! Let's change some things in our program to make the robot accelerate really smoothly, so we don't set off the nuclear waste.

    • Change the program so that:

    • The loop repeats every 0.1 seconds (100 milliseconds) instead of every second

    • Each time the loop repeats, speed is increased by 1

    • The loop runs enough times for the motors to change speed from 0 all the way to 100.

    • If you need some hints, we've marked the parts of the program you will need to change!

    • Test your program - your robot should now speed up really smoothly.

  11. We need to be able to decelerate (slow down) smoothly as well to stop on the other side of the planet. Change your code so that the motors start at 100, and decelerate smoothly to 0.
    • We need to be able to decelerate (slow down) smoothly as well to stop on the other side of the planet.

    • Change your code so that the motors start at 100, and decelerate smoothly to 0.

    • If you need it, the second picture has the correct program for smooth acceleration.

  12. Now you have learnt everything you need to safely move the waste!
    • Now you have learnt everything you need to safely move the waste!

    • Write a program that:

    • Starts at your base

    • Accelerates smoothly towards the other side of the planet

    • Decelerates smoothly and stops at the other side

    • Waits for 5 seconds so the waste can be unloaded

    • Spins on the spot 180 degrees

    • Drives back to base at full speed, and stops in the right place.

Finish Line

Tech Camp

Member since: 10/03/2012

119 Guides authored

Team

Tech Camp Staff Member of Tech Camp Staff

6 Members

140 Guides authored