Please read our student and staff community guidance on COVID-19
Skip to main content Skip to navigation

PlayCodeShare RobotLab

Computer Science can help us understand things better, for example it allows us to model a variety of phenomena and conduct many experiments which would be impossible or too expensive in real life.

Today we are going to be programming Braitenburg Vehicles.

Braitenburg Vehicles

Braitenburg vehicles are one of the simplest examples of biologically inspired robotics. They use simple connections between sensors and motors to produce seemingly complex behaviour.

The sensor-motor connections can be set up in two ways as shown in the picture. In both cases increased light on the sensor results in the motor turning faster.

  • In the first state (fear) the sensor on the left is connected to the motor on the left and the sensor on the right is connected to the motor on the right. A vehicle set up like this will speed up when exposed to more light and tend to turn away from a light source.
  • In the second state (aggression) the sensor-motor connections are crossed. Now if the light source is to one side, the vehicle will turn towards it with increasing speed. This is the most interesting state for interaction as the robot will follow a moving light source.

braitenburg

You will be using a pre-built Lego NXT robot. The robot will have 2 motors connected to the output ports and three sensors (a sonar sensor and two light sensors) connected to the input ports. Communication with the robot is performed over a USB connection, where you can transfer your code to the robot.

The ports are labelled as shown in the image below:

nxtbrick

We will be using leJOS, this allows us to program the robots using Java - the same programming language you are using in Processing. Java is very fussy about spelling and case so be careful with that.

Getting Started

Download and unzip RobotLabLight.zip. Open the Atom text editor. Use File > Add Project Folder to add the RobotLabLight Project Folder. Go to RobotLabLight.java.

Programming a Braitenburg Vehicle

To program our own Braitenburg Vehicles we need to:

  1. Get a reading from the light sensor
  2. Use that reading to set the speed of our motor
  3. Get our robot to move

The code in RobotLabLight.java contains the following lines:

import lejos.nxt.*;

public class RobotLabLight {
  public static void main(String[] args) throws Exception {

      //set up your left and right light sensor
      LightSensor leftlight = new LightSensor(SensorPort.S2);

      //set up your left and right motors here
      NXTRegulatedMotor leftMotor = new NXTRegulatedMotor(MotorPort.B);

      while (true) {

        //save the values returned from the left and right light sensor here
        int leftlightVal = leftlight.getLightValue();

        //draw to the LCD screen
        LCD.drawInt(leftlightVal, 4, 0, 0);

        //use the sensor values to set the speed of the left and right motor
        //scale as the values are between 0-100
        leftMotor.setSpeed(leftlightVal*10);

        //tell left and right motors to move forward
        leftMotor.forward();

      } //end while
    }//end main
}//end RobotLabLight

This code sets up a light sensor and a motor just like you would set up the size and background of your canvass in processing.

The while loop acts like the draw loop in Processing, running over and over again until the program stops.

Within the while loop we

  • Create a variable called leftlightVal. Computer programs use variables to store information. Here we are storing the amount of light measured by the left light sensor (the brighter the light, the higher the light value that will be stored up to a maximum of 100)
  • We then use getLightValue() to get the value from the sensor to store in leftlightVal - this will be a number between 0 and 100 (the brighter the light the higher the light value). getLightValue() is an example of a function - a function is a small section of a program that performs a specific task that can be used repeatedly throughout a program
  • We then use LCD.drawInt() to print the value stored in leftlightVal to the robot's LCD screen.
  • We also use the number stored in leftlightVal to set the speed of the leftMotor. using leftMotor.setSpeed(leftlightVal);
  • Finally we tell the leftMotor to move forward using leftMotor.forward();
  • To compile and run the code you can press F9 (make sure your robot is connected via the USB 2.0 port and is turned on)

EX1: Can you alter the code to set up the correct ports for the leftLightSensor and leftMotor

EX2: Can you add the code to set up the rightLightSensor and rightMotor

EX3: Add the code create a variable called rightLightVal to store the value returned by rightLightSensor.getLightValue();

EX4: Use the value of rightLightVal to set the rightMotor speed

EX5: Add the code to get the right motor to move forward.