Basic Processing Code

To run a COTSBot the ‘brain’ must send commands to the ‘spinal cord’.  Here is some very basic code written in the Processing environment to drive a COTSBot.  The code itself is not stunningly interesting, but its a good place to start.

This code is designed for a COTSBot using an Arduino as the ‘spinal cord’ with the BasicSkidSteer program (https://cotsbots.wordpress.com/basic-design/netbook/basic-arduino-code/) loaded on it.

To run this code, first install the BasicSkidSteer program on the Arduino (instructions and code are here: https://cotsbots.wordpress.com/basic-design/netbook/basic-arduino-code/).  Next install and run the Processing programming environment (http://processing.org/).  Finally create a new sketch in Processing, paste the code into the sketch and run it.  

The code:

 /*
 * Processing code drive a COTSBot in a ‘square’.
 * The Arduino should have the BasicSkidSteer program loaded.
 *
 * This program drives the COTSBot in the following pattern:
 *   forward for 2 seconds,
 *   stop for 2 seconds,
 *   right for 2 seconds,
 *   stop for 2 seconds,
 * repeat.
 *
 * It will ‘draw’ a four sided shape, roughly a square,
 * if 2 seconds of turning forms a right angle.
 *
 * Author: Terence Soule, University of Idaho
 */
 
 import processing.serial.*;
 Serial port;
 char[] commandString = {‘F’,’S’,’R’,’S’};
 /* This is the series of commands:
  * Forward, Stop, Right, Stop, Forward (repeats)
  * Change to any pattern you want.
  */
 int index = 0;
 
 void setup() {
   size(256, 256);
   println(“Available serial ports:”);
   println(Serial.list());               // print the available serial ports.
   port = new Serial(this, Serial.list()[0], 9600);
  /* This program uses the first available serial port:
   * Serial.list()[0]
   * If the Arduino is plugged into a different port (check this
   * thorugh the device manager) use the following command to
   * set the correct port:
   * port = new Serial(this, “COM1”, 9600);
   * where “COM1” maybe “COM2” or “COM3”, etc. depending on the
   * port the Arduino is plugged into.
   * The 9600 sets the commenucation speed and should match the
   * speed in the command:
   * Serial.begin(9600);
   * in the Arduino code.
   */
 }
 
 void draw() {  // loops forever
    port.write(commandString[index]);  // send command to Arduino
    println(commandString[index]);       // print command
    index++;                                                // increment command
    if(index >= commandString.length)
    {                                                              // if at end of command array
      index = 0;                                           // start over at 0
    }
    delay(2000);  // continue the current movement for 2 seconds.
 }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s