CS 160 Robots

For the week of November 10-14th we covered robots, the Intellibrain robots from Ridgesoft.
Ridgesoft.com

Here are snips of the sample code I covered:

First progam printing to the Screen

Here is part of the sample code for printing to the screen, the whole source code is linked below.

The first part is setting up the display and clearing lines 1 and 2 to blank output. The LCD is a two line output so you have to tell it which line you want the output on and what String you want to print.
 	Display display = IntelliBrain.getLcdDisplay();
        display.print(0, "");
        display.print(1, "");

To set up the lightSensor you ask the board for the analog sensor and you give it the port the sensor is plugged into and it returns an AnalogInput. In this case the AnalogInput would need to be plugged into the pins on row 7.

	 AnalogInput lineSensor = IntelliBrain.getAnalogInput(7);


After the lightSensor is declared and initialized you go into an infinite loop and sample the lightSensor. This code prints the results on both lines of the display....two different ways to print an int as a String.

   	while (true) {
            int sample = lineSensor.sample();
            display.print(0, Integer.toString(sample));
            display.print(1, "" + sample);
          }


And that's all the code to read from the light sensor forever.
LightSensor.java

Twinkle Twinkle song.

This code is an example of playing a song on the robot. You need to grab the speaker from the board, then you call a play method that takes the note you want to play (int) and a duration to play the note (int). In class I showed how I use a GC.java file to hold my generic constants.
Grab the speaker from the board:

	Speaker speaker = IntelliBrain.getBuzzer();
 
Then you call the play method with the two ints, note and duration, I am using the constants (final) in the GC class.
         
	speaker.play(GC.C, GC.QUARTER_NOTE);

GC definitions of the two ints used:

    public static final int QUARTER_NOTE = 400;
    public static final int HALF_NOTE = QUARTER_NOTE * 2;
    public static final int C = 262;

After a note you want the program to pause before it plays the next note so they don't all run together. To have your program pause you need to do a call to Thread.sleep(int); This call must be in a try{} block followed by a catch like in the following example:

     try {
            
             speaker.play(GC.C, GC.QUARTER_NOTE);
             Thread.sleep(100);
	     speaker.play(GC.C, GC.QUARTER_NOTE);
	}
        catch (Throwable t) {
            t.printStackTrace();
        }

Here is a link to all three files in the Twinkle, Twinkle Little Star program:

Movement

To get your wheels to move you first have to create a Servo object using the servo port, after that you create a Motor object using the Servo. To create a Motor object you call the constructor of the ContinuousRotationServo with the Servo, a boolean which is used for saying which way the servo is facing, and an int value for full speed (14 is usually used).

        Motor leftMotor = new ContinuousRotationServo(IntelliBrain.getServo(1), false, 14);
        Motor rightMotor = new ContinuousRotationServo(IntelliBrain.getServo(2), true, 14);

Then to get them to move you set the power on the wheels with a positive integer, or use negative integer values for reverse. I use a method like the following that I can call with two int values to move the wheels and then the following method is a method to stop the wheels.

  public void go(int r, int l) {
        leftMotor.setPower(r);
        rightMotor.setPower(l);        
    }


    public void stop() {
        leftMotor.stop();
        rightMotor.stop();
    }
Here is a link to the code that made the robot go forward full speed for 2 seconds and then turn for 2 seconds and repeat four times in a for loop:

Following a line

The follwing program uses the analog sensor to read the light value coming in on pins 6.
After it gets the value it determines if it's on a white space turn left, if it's on a black line turn right. This program will follow a black line on a white piece of paper forever or until the black line ends.

import com.ridgesoft.intellibrain.IntelliBrain;
import com.ridgesoft.io.Display;
import com.ridgesoft.robotics.PushButton;
import com.ridgesoft.robotics.AnalogInput;
import com.ridgesoft.robotics.Motor;
import com.ridgesoft.robotics.ContinuousRotationServo;

public class LineFollower1 {

    public static void main(String args[]) {
        Display display = IntelliBrain.getLcdDisplay();
        display.print(0, "");
        display.print(1, "");

        AnalogInput lineSensor = IntelliBrain.getAnalogInput(6);
        Motor leftMotor = new ContinuousRotationServo(IntelliBrain.getServo(1), false, 14);
        Motor rightMotor = new ContinuousRotationServo(IntelliBrain.getServo(2), true, 14);

        float gain = 0.016f;
   
        display.print(0, "Following line");
        while (true) {
          	 try{Thread.sleep(100);}catch(Throwable t){t.printStackTrace();}
         
                     int sample = lineSensor.sample();
                     display.print(0, Integer.toString(sample));                
                     float offset = (360.0f - (float)sample) * gain;
                     leftMotor.setPower((int)(8.0f + offset));
                     rightMotor.setPower((int)(8.0f - offset));
        }
    }
}

IR range sensor

The following code is an example of using a IR range sensor to play musical notes according to how far the sensor is reading. If the sensor is picking up an object under 10 inches it plays one note, 10-14 inches another note, and 15-19 inches another note.

import com.ridgesoft.io.Speaker;
import com.ridgesoft.robotics.RangeFinder;
import com.ridgesoft.robotics.sensors.SharpGP2D12;
import com.ridgesoft.robotics.AnalogInput;
import com.ridgesoft.intellibrain.IntelliBrain;
import com.ridgesoft.io.Display;


public class Driver {

    Display display;


    Driver()
    {
       display = IntelliBrain.getLcdDisplay(); 
       display.print(0, "Hello CS160");     
       try{
          
           Thread.sleep(2000);
       
        }catch(Throwable t)
        { t.printStackTrace(); }
        checkIR();   

    }

    private void checkIR()
    {
        //RangeFinder range = new SharpGP2D12(IntelliBrain.getAnalogInput(1), null);
     /*The line above is the same as the two lines below, above is a quicker way to do it.*/   
        AnalogInput irSensor = IntelliBrain.getAnalogInput(1);
        RangeFinder range = new SharpGP2D12(irSensor , null);

        Speaker sound = IntelliBrain.getBuzzer();

        try{
           
            while (true) 
           {
                range.ping();
                float distance = range.getDistanceInches();
       

               if (distance < 0.0f)
                {
				    display.print(1, "---");
                }
                else
                {

                    display.print(1, Integer.toString((int) (distance + 0.5)) + '"');
                    display.print(1, Integer.toString((int) (distance + 0.5)) + '"');

                    if(distance < 10)
                        sound.play(262, 500);
                    else if(distance <15)
                        sound.play(392, 500);
                    else if(distance < 20)
                        sound.play(494, 500);

                }
                Thread.sleep(500);                                 
            }
       }catch(Throwable t){}
    }

    public static void main(String args[]) {
        new Driver();
    }
}