General how to use the kit

Back to docs
This is the document that was given to you on launch day. There maybe typing errors that reference events on launch day.

Part 1 – Your Kit Check that all of the correct parts are in the box, the contents list in the rules is wrong. Refer to the website for a correct list.

Part 2 – Using the kit 

Plug in the tablet to power and connect the battery to the brain box. Then connect the black connector to the on/off. 

Connect the tablet to the brain box using the password that was given to you after step 2. 

Open chrome and go to http://robot.sr 

Open IDLE. Find the file called ‘Hello World’ on the desktop. You should change the code to print out something of your choice. Then upload and run the code by doing the following: 

Save the program on the desktop then run the program by uploading onto the website (http://robot.sr) via the “upload” link and then press the “Choose File” button, then press “upload”.  

Return to the home page of the website, click the “run” link and then press the “start” button. 

Part 3 – GPIOs as outputs 

Create a new python file in your editor.

At the beginning of the script write the following import statements: 

import time 

from sr.robot import * 

Initialise the Robot and assign the LED to a GPIO pin using the following code: 

R = Robot() #initialises the Robot 

LED_PIN = 2 #Sets the LED to GPIO pin 2 

Set the GPIO pin to output. 

R.gpio.pin_mode(<PIN>, <MODE>)
where <PIN> is the GPIO Pin you are using and <MODE> is either input or output. 

R.gpio.digital_write(<PIN>, True)  

Then, wait for 1 second using the line: 

time.sleep(<SECONDS>) 

Use a while loop to make this repeat.

You can then use these lines to change the LED however you like. Try making it flash on and off for a bit. 

Part 4a – GPIOs as digital inputs 

Connect one of your power switches to GPIO Pin 4 and the ground connection. 

Create a new python file and initialise your robot (as described in step 9 and 10). 

Set up pin 1 as a digital input. 

R.gpio.pin_mode(4, INPUT_PULLUP) 

To read the pin you need to call the digital_read(<PIN>)function which will return the value of the pin. Assign it to a variable as below: 

reading = R.gpio.digital_read(4)  

This will assign the value to “reading”. If you want to see the value of reading you can print it like so: 

print reading   

Place the read function in a ‘while True’ loop so that we are constantly sampling and printing our sample.  

It is probably a good idea to put a sleep at the end of the loop as the program doesn’t need to sample that much.   

while True:   

reading = R.gpio.digital_read(4) 

print reading  

time.sleep(0.1) 

Run the code using the robot.sr website. 

Part 4b – GPIOs as analog inputs  

Connect the potentiometer to the GPIO connecter (see information sheet to find where they are): 

  • Black goes to ground 
  • Green goes to GPIO 1  
  • Red goes to 5V

The wires will be connected by inserting them into the empty ports on the GPIO plug. Tighten the screw above to secure them.   

Open either IDLE and create a new file Analog.py and import time and everything in the ‘sr.robot’ library.   

Call the robot library and initialise it as before. 

Set up pin 1 as an analogue input. This configures pin 1 to take be able to take readings.  

R.gpio.pin_mode(1, INPUT_ANALOG)  

The syntax is as follows R.gpio.pin_mode(<PIN>,<MODE>).  

To read the pin you need to call the analog_read(<PIN>function which will return the value of the pin. Assign it to a variable as below: 

reading = R.gpio.analog_read(1)  

This will assign the value to “reading”. If you want to see the value of reading you can print it like so.  

print reading   

Place the read function in a while True loop so that we are constantly sampling and printing our sample.  

It is probably a good idea to put a sleep at the end of the loop as the program doesn’t need to sample that much.  

while True:   

reading = R.gpio.analog_read(1) 

print reading  

time.sleep(0.1) 

Save the program on the desktop then run the program by uploading onto the website via the “upload” link and then press the “Choose File” button, then press “upload”. Return to the home page of the website, click the “run” link and then press the “start” button. Turn the potentiometer and you should see the number of the screen change.   

 

(a) Now let’s use this value to make something happen, for example turn on an LED after a certain voltage.   

(b) Connect an LED’s positive leg (the long one) to GPIO 2 and the shorter one in the same port as the black wire on the potentiometer (leave the potentiometer wire in the port).  

Set it as an output and set it low, before the while loop.   

R.gpio.pin_mode(2, OUTPUT)  

R.gpio.digital_write(2, 0)    

Now in the while loop, but after you take the reading, create an if statement that will check if the value of pin 1 is more than 800. If it is, set pin 2 to high, else it sets it low:  

if (Reading > 800):  

R.gpio.digital_write(2, 1)  

else:  

R.gpio.digital_write(2, 0)     

Now run the code and turn your potentiometer. The LED should turn on and off depending on the position of the potentiometer.   

 

Part 5 – Motor control  

Controlling a motor is fairly simple. The syntax for setting motor power is as follows, where <CONNECTION> is the index of the I2C connection you want to use (unless you’ve added your own power board, that’s ‘0’), and <INDEX> is the index of the motor (0 for physical connection M1 on the Brain Box, 1 for physical connection M2):  

R.motors[<CONNECTION>].m<INDEX>.power = <0-70>  

For example, to set the motor at physical connection M1 to full throttle, the command would be:  

R.motors[0].m0.power = 70 

Try and get a motor to spin at full speed for 10 seconds before coming to a stop.  

Now see if you can get one to spin forwards at full speed for five seconds, then backwards at half speed for ten. 

 

Part 5 – Servo control  

Controlling a servo is very straightforward. The syntax for setting servo position is as follows, where <INDEX> is the index of the servo you want to use:  

R.servos[<INDEX>] = <-100 to 100>  

For example, to set the servo at index 0 to the centre, the command would be:  

R.servos[0] = 0   

Try setting a servo to position 100, then back to 0 again.  

Now connect up another servo, and set one to -75 and the other to 75.  

Try writing a program that will set a servo back and forth from 100 to -100 every two seconds in an infinite loop.  

 

Extension! 

If you’re feeling happy with the above, try the following extension questions (Don’t worry if you can’t do them, they’re pretty tough!):  

Turn an LED on/off with the black button in your kit.

Change the brightness of an LED to 50%.

Get the LED’s brightness to be affected by the input voltage from a potentiometer.

 

 

 

 

 

 

Last Updated On November 25, 2017