In this activity, we will make the computer program that controls the Mars Rover. It’s like a remote-control car. You can press different keys on the keyboard to make the Mars Rover move forward, backward, turn left and turn right.
Motor and Servo Motor
In our Mars rover, there are a total of 6 motors and 5 servo motors.
The motors provide rotation to the wheels which helps the rover to attain motion in both forward and backward directions. All the left side motors (3 motors) are connected to the left motor port of Quarky and all the right side motors (3 motors) are connected to the right motor port of Quarky using a 3 port wire. This means that to control the Mars rover we have to control only 2 motors – Left and Right.
Also, there are 2 parameters to control – Direction (Forward or Backward) and Speed. With this control, the Mars rover can do all the desired motions.
The servo motors help in providing rotation to the complete wheel assembly so that the rover can change its wheel alignments and its path. These play a major role in turning cases of the Mars Rover.
We will need to turn the servo motors to the Inside Servo Position to make Mars Rover turn left and right.
Python Code
- We will make sure we have initialized Quarky and Mars Rover correctly.
- We will use the while loop to continuously detect the keys of the keyboard and react according to the way.
- We will use the python function of sprite “.iskeypressed()” to know which key has been pressed and act respectively.
- When the up arrow key is pressed, we will set all the servos to 90 degree angle with the help of the rover.home() function. We will run the mars rover with the help of quarky.runtimedrobot() function.
- With the same set of functions we will create the structure for when the users press the backward key to create the respective motion.
- When the user presses the right or left arrow key, we will set the servo angles to a specific degree – 40 to complete the turn successfully.
- By this way we will be able to turn the rover according to our needs by using the runtimedrobot() function.
sprite=Sprite('Tobi')
import time
quarky = Quarky()
rover = MarsRover(4, 1, 7, 2, 6)
while True:
if sprite.iskeypressed("up arrow"):
rover.home()
rover.setinangle(0)
quarky.runtimedrobot("F",100,3)
if sprite.iskeypressed("down arrow"):
rover.home()
rover.setinangle(0)
quarky.runtimedrobot("B",100,3)
if sprite.iskeypressed("right arrow"):
rover.home()
rover.setinangle(40)
quarky.runtimedrobot("R",100,3)
if sprite.iskeypressed("left arrow"):
rover.home()
rover.setinangle(40)
quarky.runtimedrobot("L",100,3)