Simulating Robots!

logoPicture

A project activity for Girls Into Coding using a online version of the free open source Webots robot simulator.

This session is designed to be fun! The idea is that we can follow it together online, but that we can be free to move at our own pace. We’re going to be doing some basic python programming in this activity. If you’re not too familiar with Python, don’t worry, you’ll be able to follow along :) !





What is a robot simulator?


A robot simulator is like a video game, with robots in it! It’s a very realistic version of the world, with one or more virtual robots inside. Let’s try it out !!

Activity #1

We’re going to be using robotbenchmark in this activity, which is a online version of the ‘Webots’ simulator.

  • Navigate there in your browser now, by clicking this link.
  • Click on the start button next to the pick and place activity.
  • You will see a simulation running already, in the top right hand corner of the screen, try clicking on it to change the view.

Left click: rotate Right click: move

Activity1Picture

You can click on any of the ‘run’ buttons to see someone else’s attempt at coding the robot.

  • Try clicking on the run button next to Konstantina’s record.
  • Click on the ‘play’ button in the bottom left hand corner



Why simulate robots?


Why would we want to simulate robots? …. Well because it’s helpful for designing them. Let’s take a look at this video.

Flying robot swarm video

In the video you can see robotics professor Sabine Hauert talking about swarms of flying robots that she designed. The robots are tested in simulation!. If something is wrong with the software, and the robots crash :( it doesn’t break the real robot! Sabine can just change the software and restart the simulation until the robots are flying well! Then the robotics team can try it on the real swarm of robots! Here is another cool video of flying robots

Poll

Question As a future robot designer, what kind of robots will you prefer to build?



First steps using the robot simulator


Activity #2

  • Let’s return to the robotbenchmark website.
  • Click on the Start button next to the Viewpoint Control activity
  • Click on Start programming this benchmark
  • Follow the instructions in the top left hand corner of the screen
  • When you have found the correct view click on the record button on the metrics tab

Activity2Picture



Programming the robot!


Activity #3

  • Let’s return to the robotbenchmark website.
  • Click on the Start button next to the Robot Programming activity
  • Click on Start programming this benchmark
  • Read the instructions in the top left hand corner of the screen. You can resize the instruction window by dragging its bottom corner.
  • When you’re ready to program the robot, right click on it, and select Edit controller

Activity3Picture1

  • A window will pop up with python code in it! This is how we program our virtual robot.
  • The same code is shown below with some comments which describe the purpose of each line
"""Simple robot controller."""

from controller import Robot ## THESE LINES IMPORT CODE LIBRARIES SO WE CAN TALK TO THE ROBOT :)
import sys 

# Define the target motor position in radians.
target = 12 ## WE CREATE THIS VARIABLE TO SET HOW FAR THE ROBOT WILL MOVE :)

# Get pointer to the robot.
robot = Robot() ## HERE WE MAKE A LINK TO THE VIRTUAL ROBOT :)

# Print the program output on the console
# ## IF YOU CLICK ON THE CONSOLE BUTTON YOU WILL SEE THE PRINT MESSAGES ! :) 
print("Move the motors of the Thymio II to position " + str(target) + ".")
sys.stderr.write("This is a sample error message.\n")

# Set the target position of the left and right wheels motors.
## THESE LINES TELL EACH MOTOR ON THE ROBOT HOW FAR TO MOVE :)
robot.getMotor("motor.left").setPosition(target)
robot.getMotor("motor.right").setPosition(target)
  • The instructions ask us to change the value of the variable target
  • Go ahead and do this now
  • Then save the python file by clicking on the three bar icon, and click save
  • Now click on the play button to see your virtual robot run your program

Activity3Picture2

  • Try different values for the target variable. How about negative values, what does this do?
  • When you want to change the program repeat the steps above
  • To reset the simulation click on the reset simulation button



Making the robot turn on the spot (creating a new python variable)


Activity #4


  • We’re still going to be using the Robot Programming activity for this program!
  • How do you think you would make the robot turn? So that it doesn’t move in a straight line?
  • Right click on the robot and select Edit controller
  • Have a look at the code, is there a way you could make a different target for each motor by making a new variable?
  • Give it a go ! if you get stuck you can take a look at the example answer :)
  • Don’t worry if a completion message comes up, if you click on the okay button the simulation will keep running
  • Follow the steps above to save your robot program and reset the simulation

Python variables explaination at W3 schools :)


Try making a new variable, so that you have a target position for each wheel!


"""Simple robot controller."""

## This controller makes the robot turn on the spot!

from controller import Robot
import sys

# Define the target for each motor position in radians.
targetLeft = -100
targetRight = 100

# Get pointer to the robot.
robot = Robot()

# Print the program output on the console
print("Move the left motor of the Thymio II to position " + str(targetLeft) + ".")
print("Move the left motor of the Thymio II to position " + str(targetRight) + ".")
sys.stderr.write("This is a sample error message.\n")

# Set the target position of the left and right wheels motors.
robot.getMotor("motor.left").setPosition(targetLeft)
robot.getMotor("motor.right").setPosition(targetRight)    


Activity3Picture3

Activity3CompMessage



Working with python functions


Activity #5


  • We’re still going to be using the Robot Programming activity for this program!
  • It would be nice to write a program so that we can just say to the robot “turn left” or “go backwards for 2 seconds”
  • We can achieve this using python functions (this is also called functional programming). If you’ve not used python functions before, have a look at the resources!!
  • Right click on the robot and select Edit controller
  • Below is an example of some code with two functions: stopRobot() and moveForwardsInStraightLine(numberofTimeSteps)
  • The numberofTimeSteps tell the robot how long to carry out that movement
  • Have a look at the code, is there a way you could make three more functions?: moveForwardsInStraightLine, turnClockwiseOnSpot, and turnAntiClockwiseOnSpot
  • Give it a go ! if you get stuck you can take a look at the example answer :)
  • Don’t worry if a completion message comes up, if you click on the okay button the simulation will keep running
  • Follow the steps above to save your robot program and reset the simulation


Python functions explaination at W3 schools :)


# Here is an example of how to write a function to turn on the spot (**turnClockwiseOnSpot**). 
# Copy this code into your robot controller and experiment with it. Can you see how it works? 
# Can you use the same idea to write the other functions? </p>

"""A simple robot controller with functions"""

# --- Import the code libraries we need---
from controller import Robot
import sys

# --- Make global variabes --
robotVelocity = 7 # The speed we want the robot to travel at (takes values 0->9.5)

# --- Create the functions we are going to use ---
def stopRobot():
    targetVelocity = 0 # To stop the robot we set the motor target to zero!
    robot.getMotor("motor.left").setVelocity(targetVelocity)
    robot.getMotor("motor.right").setVelocity(targetVelocity)

def setRobotSpeed(myVelocity):
    robot.getMotor("motor.left").setVelocity(myVelocity)
    robot.getMotor("motor.right").setVelocity(myVelocity)

def moveForwardsInStraightLine(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    target = 9999 # If this number is big and positive the robot will move forwards
    robot.getMotor("motor.left").setPosition(target) #Assign the motor directions!
    robot.getMotor("motor.right").setPosition(target)
    robot.step(numberOfTimeSteps) # The robot moves a little bit each time step

def turnClockwiseOnSpot(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    targetLeft = 9999 # Turn wheel forwards
    targetRight = -9999 # Turn wheel backwards
    robot.getMotor("motor.left").setPosition(targetLeft) #Assign the motor directions!
    robot.getMotor("motor.right").setPosition(targetRight)
    robot.step(numberOfTimeSteps)

# --- Main code---

# Get pointer to the robot.
robot = Robot()

# The number in brackets tells the robot how long to move for
turnClockwiseOnSpot(1000)
moveForwardsInStraightLine(1000)  
stopRobot()    


"""A simple robot controller with functions"""

# --- Import the code libraries we need---
from controller import Robot
import sys

# --- Make global variabes --
robotVelocity = 7 # The speed we want the robot to travel at (takes values 0->9.5)

# --- Create the functions we are going to use ---
def stopRobot():
    targetVelocity = 0 # To stop the robot we set the motor target to zero!
    robot.getMotor("motor.left").setVelocity(targetVelocity)
    robot.getMotor("motor.right").setVelocity(targetVelocity)

def setRobotSpeed(myVelocity):
    robot.getMotor("motor.left").setVelocity(myVelocity)
    robot.getMotor("motor.right").setVelocity(myVelocity)

def moveForwardsInStraightLine(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    target = 9999 # If this number is big and positive the robot will move forwards
    robot.getMotor("motor.left").setPosition(target) #Assign the motor directions!
    robot.getMotor("motor.right").setPosition(target)
    robot.step(numberOfTimeSteps) # The robot moves a little bit each time step

def moveBackwardsInStraightLine(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    target = -9999 # If this number is big and negative the robot will move backwards
    robot.getMotor("motor.left").setPosition(target)
    robot.getMotor("motor.right").setPosition(target)
    robot.step(numberOfTimeSteps)

def turnClockwiseOnSpot(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    targetLeft = 9999 # Turn wheel forwards
    targetRight = -9999 # Turn wheel backwards
    robot.getMotor("motor.left").setPosition(targetLeft) #Assign the motor directions!
    robot.getMotor("motor.right").setPosition(targetRight)
    robot.step(numberOfTimeSteps)

def turnAntiClockwiseOnSpot(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    targetLeft = -9999 # Turn wheel backwards
    targetRight = 9999 # Turn wheel forwards
    robot.getMotor("motor.left").setPosition(targetLeft)
    robot.getMotor("motor.right").setPosition(targetRight)
    robot.step(numberOfTimeSteps)


# --- Main code---

# Get pointer to the robot.
robot = Robot()

# The number in brackets tells the robot how long to move for
moveForwardsInStraightLine(1000) 
moveBackwardsInStraightLine(1000)
turnClockwiseOnSpot(1000)
turnAntiClockwiseOnSpot(1000)
turnClockwiseOnSpot(1000)
stopRobot()    


"""A simple robot controller with functions"""

# --- Import the code libraries we need---
from controller import Robot
import sys

# --- Make global variabes --
robotVelocity = 7 # The speed we want the robot to travel at (takes values 0->9.5)

# --- Create the functions we are going to use ---
def stopRobot():
    targetVelocity = 0 # To stop the robot we set the motor target to zero!
    robot.getMotor("motor.left").setVelocity(targetVelocity)
    robot.getMotor("motor.right").setVelocity(targetVelocity)
    
def setRobotSpeed(myVelocity):
    robot.getMotor("motor.left").setVelocity(myVelocity)
    robot.getMotor("motor.right").setVelocity(myVelocity)

def moveForwardsInStraightLine(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    target = 9999 # If this number is big and positive the robot will move forwards
    robot.getMotor("motor.left").setPosition(target) #Assign the motor directions!
    robot.getMotor("motor.right").setPosition(target)
    robot.step(numberOfTimeSteps) # The robot moves a little bit each time step


# --- Main code---

# Get pointer to the robot.
robot = Robot()

# The number in brackets tells the robot how long to move for
moveForwardsInStraightLine(1000)  
stopRobot()    



Using python loops to make shapes with the robot!


Activity #6


  • We’re still going to be using the Robot Programming activity for this program!
  • It would be nice to use our python functions so the robot can travel in a shape that we choose ! Like a square or a zig zag path
  • Can you think of how we might do this with python loops? … an example is below


Python loops explaination at W3 schools :)



"""A simple robot controller with functions"""

# --- Import the code libraries we need---
from controller import Robot
import sys

# --- Make global variabes --
robotVelocity = 7 # The speed we want the robot to travel at (takes values 0->9.5)

# --- Create the functions we are going to use ---
def stopRobot():
    targetVelocity = 0 # To stop the robot we set the motor target to zero!
    robot.getMotor("motor.left").setVelocity(targetVelocity)
    robot.getMotor("motor.right").setVelocity(targetVelocity)

def setRobotSpeed(myVelocity):
    robot.getMotor("motor.left").setVelocity(myVelocity)
    robot.getMotor("motor.right").setVelocity(myVelocity)

def moveForwardsInStraightLine(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    target = 9999 # If this number is big and positive the robot will move forwards
    robot.getMotor("motor.left").setPosition(target) #Assign the motor directions!
    robot.getMotor("motor.right").setPosition(target)
    robot.step(numberOfTimeSteps) # The robot moves a little bit each time step

def moveBackwardsInStraightLine(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    target = -9999 # If this number is big and negative the robot will move backwards
    robot.getMotor("motor.left").setPosition(target)
    robot.getMotor("motor.right").setPosition(target)
    robot.step(numberOfTimeSteps)

def turnClockwiseOnSpot(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    targetLeft = 9999 # Turn wheel forwards
    targetRight = -9999 # Turn wheel backwards
    robot.getMotor("motor.left").setPosition(targetLeft) #Assign the motor directions!
    robot.getMotor("motor.right").setPosition(targetRight)
    robot.step(numberOfTimeSteps)

def turnAntiClockwiseOnSpot(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    targetLeft = -9999 # Turn wheel backwards
    targetRight = 9999 # Turn wheel forwards
    robot.getMotor("motor.left").setPosition(targetLeft)
    robot.getMotor("motor.right").setPosition(targetRight)
    robot.step(numberOfTimeSteps)


# --- Main code---

# Get pointer to the robot.
robot = Robot()

# The number in brackets tells the robot how long to move for
for i in range(5):
    moveForwardsInStraightLine(1000) 
    turnClockwiseOnSpot(600)
stopRobot()        



Putting our code to make shapes into python functions!


Activity #7


  • We’re still going to be using the Robot Programming activity for this program!
  • It would be nice to use our python functions so that we can write a function to ‘move in a square pattern’ or ‘move in a zig zag’.
  • This is called abstraction
  • Can you think of how we might do this with python functions? … and example is below


"""A simple robot controller with functions"""

# --- Import the code libraries we need---
from controller import Robot
import sys

# --- Make global variabes --
robotVelocity = 7 # The speed we want the robot to travel at (takes values 0->9.5)

# --- Create the functions we are going to use ---
def stopRobot():
    targetVelocity = 0 # To stop the robot we set the motor target to zero!
    robot.getMotor("motor.left").setVelocity(targetVelocity)
    robot.getMotor("motor.right").setVelocity(targetVelocity)

def setRobotSpeed(myVelocity):
    robot.getMotor("motor.left").setVelocity(myVelocity)
    robot.getMotor("motor.right").setVelocity(myVelocity)

def moveForwardsInStraightLine(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    target = 9999 # If this number is big and positive the robot will move forwards
    robot.getMotor("motor.left").setPosition(target) #Assign the motor directions!
    robot.getMotor("motor.right").setPosition(target)
    robot.step(numberOfTimeSteps) # The robot moves a little bit each time step

def moveBackwardsInStraightLine(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    target = -9999 # If this number is big and negative the robot will move backwards
    robot.getMotor("motor.left").setPosition(target)
    robot.getMotor("motor.right").setPosition(target)
    robot.step(numberOfTimeSteps)

def turnClockwiseOnSpot(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    targetLeft = 9999 # Turn wheel forwards
    targetRight = -9999 # Turn wheel backwards
    robot.getMotor("motor.left").setPosition(targetLeft) #Assign the motor directions!
    robot.getMotor("motor.right").setPosition(targetRight)
    robot.step(numberOfTimeSteps)

def turnAntiClockwiseOnSpot(numberOfTimeSteps):
    setRobotSpeed(robotVelocity) # First we set the velocity (in case the robot just stopped)
    targetLeft = -9999 # Turn wheel backwards
    targetRight = 9999 # Turn wheel forwards
    robot.getMotor("motor.left").setPosition(targetLeft)
    robot.getMotor("motor.right").setPosition(targetRight)
    robot.step(numberOfTimeSteps)

def completeSquareMovement(numberOfItterations):
    # The number in brackets tells the robot how long to move for
    # The numberOfItterations tell the robot how many squares to complete
    for i in range(4*numberOfItterations):
        moveForwardsInStraightLine(1000) 
        turnClockwiseOnSpot(600)
    stopRobot()    

def completeZigZagMovement(numberOfItterations):
    # The number in brackets tells the robot how long to move for
    # The numberOfItterations tell the robot how many zigZags to complete
    zigZagDirection = 1
    for i in range(2*numberOfItterations):
        moveForwardsInStraightLine(1000) 
        if zigZagDirection == 1:
            turnClockwiseOnSpot(600)
        else:
            turnAntiClockwiseOnSpot(600)
        zigZagDirection = zigZagDirection * -1
    stopRobot()     

# --- Main code---

# Get pointer to the robot.
robot = Robot()

completeSquareMovement(2)
completeZigZagMovement(2)   

Robotbenchmark Activity (GirlsIntoCoding)

  • Robotbenchmark Activity (GirlsIntoCoding)

A super fun activity to control robots in simulation

Powered by Bootstrap 4 Github Pages