Friday, January 27, 2012

If an X-bee can control RGB on an LED, why not the motors of a Sea Perch, see?

 
 Good morning, Playful PORPOISEs!
 
 Today we make preparations for wireless communication with our Sea Sparrow 
(and via tether, our Sea Perch).  How? 
We will use Xbee Shields.
What are they?
According to Sparkfun's tutorial:
"Normally when you send data from your Arduino to your computer it 
travels over a USB cable. You can use XBee as a "cable replacement", 
this means the data will travel wirelessly between the Arduino and your 
computer without the need for a cable to connect the two devices. "
 
I tried doing this last week using the Parallax Xbee shield kit I got from Frye's electronics. 
I got the two Xbee units talking via the serial monitor (doing the echo sketch) but that was as far as
far as I got because of the difficulty of wiring the pins of the Xbee board to the Arduino.
Obviously Parallax makes their Xbee units to talk to their Basic Stamp controller or Propellor 
Microcontroller - it is, after all, their product. 

So I decided to go to Sparkfun for their X-Bee Shield Kit, and because it mounts directly ON the arduino
things are ever so much simpler!
 
With the Sparkfun Shield Kit and their on-line tutorials you are up and running right outside of the box.
No difficult configuring or wiring of the Xbees! Yay!
 

The getting started tutorial is fun, and it is great if you have the Sparkfun Arduino Inventor's kit because
the second sketch in it (replicated below) makes use of the Circuit-12 three color RGB LED tutorial using
this circuit here:
  
 As you will readily appreciate from the quick video we made below here in the Motion Picture
Marine lab, once you have used the  XBeeCableReplacement.pde sketch to send signals from the
 X-bee Explorer (connected to a computer) to a remote battery powered Arduino 
with the X-bee shield connected to a four pin RGB LED and demonstrated to yourself that you can
turn on and off the different colored lights using commands from the keyboard via the serial 
monitor, why then you know you can modify that code slightly to turn on and off different motors from 
a distance too!
 
 
 _I think you know where I'm going with this!  Instead of using the 'r' 'g' and 'b' keys to turn
on red green and blue lights, we can use 'f', 'b' and 'u' to control our sea perch motors! Yup,
this unlocks the possibility of putting the controller on the Sea Sparrow, tethered to the Sea Perch 
underwater, and stand safely on the shore with a laptop or smart phone and tell the sea perch (and the sea
sparrow) where to go!
 
Cool, huh?
 
The tutorial codes are replicated below in case the github site is down for some reason.
 
Enjoy!
 
PS: It appears that contrary to the instructions below, you DON'T have to always remove the Xbee Shield or the Xbee itself 
every time you want to upload a program to the Arduino; the work around is to place the toggle
switch on the shield from UART to DLINE when you are loading programs from the USB
Port of your computer to the board, and back to UART when you are using it to send information
to the XBee:
"When the switch on the shield is in the "DLINE" position the hardware 
serial port (which uses the RX and TX pins) of the Arduino can be used 
for uploading sketches...When the switch on the shield is in the "UART" position the hardware 
serial port of the Arduino is used to transfer data from the Arduino to 
the XBee module."
 
 This ability to use a simple switch on the Sparkfun Shield to upload programs to the 
Arduino (in DLINE position) and then switch to UART to use the Xbee is a great feature for
students since two much removing of the XBee or shield will ultimately damage the pins 
or the chips themselves.  Thanks Sparkfun! 
__________________________________________________________________________
 from the github sparkfun site:
(copy and paste everything below) 
 
/*

  XBeeSerialEcho -- Simple sketch for demonstrating two-way XBee communication

  Requires:
  
    (These items are found in the SparkFun XBee Wireless Kit Retail product
     or can be obtained individually.)
  
       * 1 x SparkFun XBee Explorer USB
    
       * 1 x SparkFun XBee Wireless Shield
    
       * 2 x "Series 1" Digi XBee communication modules
    

  Instructions:
  
    * Upload this sketch as normal to your Arduino without the XBee Wireless
      Shield connected.
      
    * Disconnect the Arduino from your computer.
  
    * Both XBee modules should be configured to use their default 
      configuration and communicate at 9600 baud.
      
    * One XBee module should be connected to the XBee Explorer board.
      Ensure the orientation of the XBee module is correct with the
      beveled end of the module pointing away from the USB connector
      so the module outline matches the diagonal white lines
      silkscreened on the board.
      
    * The other XBee module should be connected to the XBee Wireless
      Shield for the Arduino. Again, ensure the orientation of the
      XBee module is correct with the beveled end pointing over the
      edge of the shield away from the USB connector on the Arduino
      board. The outline of the module should match the the diagonal
      white lines silkscreened on the shield.
      
    * The small switch located near the TX/RX pins on the shield
      should be set to the "UART" position so the XBee module is
      connected to the hardware serial port on the Arduino.
    
    * Connect the XBee Wireless Shield to your Arduino.
  
    * Connect the XBee Explorer USB to your computer with a mini-USB cable.
  
    * Select the correct serial port for your XBee Explorer USB from the
      Tools > Serial Port menu in the Arduino IDE.
  
    * Connect your Arduino with the XBee Wireless Shield on it to a
      power source--either a battery, a "wall wart" power adapter or
      another USB port.
      
    * Open the Serial Monitor window of the Arduino IDE and make sure
      the correct communication speed of "9600 baud" is selected from
      the pop-up menu in the lower right corner of the Serial Monitor
      window.
      
    * Start typing and whenever you press the button labeled "Send" or
      press the "Enter" or "Return" key what you type should be echoed
      back to you. (If only "garbage" is returned you probably have
      the XBee modules configured for the wrong speed.  Reconfigure
      them to operate at 9600 baud and try again.)
     
    * The text you type is sent from your computer, over the USB cable
      to the XBee Explorer USB board. From there it is directed to the
      XBee module on the board which sends it wirelessly to the second
      XBee module on the shield. The second XBee module then directs
      the text to the hardware serial port of the Arduino. Your
      Arduino reads each character of the text from the serial port
      and then sends it back (that is, "echos" it) through the serial
      port to the second XBee module. The second XBee module then
      sends the echoed text wirelessly to the first XBee module which
      directs it to the Explorer USB board which sends it back up the
      USB cable to the Arduino IDE serial monitor window. *Phew*.
      
      
  Enhancements:
  
    * You can change the configuration of the XBee wireless modules to
      communicate at a faster speed (try 19200 baud and then 57600
      baud) if you also change the communication speed of the
      "Serial.begin()" line below and in the Serial Monitor window of
      the Arduino IDE to match.


   For more details and help read the XBee Shield and Explorer USB
   Quickstart Guide:
   
      <http://sparkfun.com/tutorials/192>

 */

void setup() {
  Serial.begin(9600); // See "Enhancements" above to learn when to change this.
}

void loop() {
  if (Serial.available()) {
    Serial.print((char) Serial.read());
    delay(10);
  }
}
 
____________________________________________________________________ 
 
After running that test successfully, I run this test (code below is from this site):
 
/*

  XBeeCableReplacement.pde
   
  Control the color of an RGB LED wirelessly via XBee.
   
   
  Required (in addition to your Arduino):
  
    (These items are found in the SparkFun XBee Wireless Kit Retail product
     or can be obtained individually.)
  
       * 1 x SparkFun XBee Explorer USB
    
       * 1 x SparkFun XBee Wireless Shield
    
       * 2 x "Series 1" Digi XBee communication modules
   
   
  Instructions:
  
    * Ensure both XBee communication modules are configured to use
      their default configuration and communicate at 9600 baud.
  
    * Put the XBee Wireless Shield onto your Arduino but do not insert
      the XBee communication module.
  
    * Wire an RGB LED to your Arduino as shown in CIRC-12 of the
      SparkFun Inventor's Guide.
      
    * Connect your Arduino to your computer using a USB cable.
    
    * Upload this sketch as normal to your Arduino.
      
    * Test the functionality of the sketch works when connected via
      the USB cable. Open the Serial Monitor window and ensure it is
      set to communicate at 9600 baud. Type a sequence of one or more
      of the characters 'r', 'g' and 'b' and then press the "Send"
      button. The RGB LED should change color in response. A space
      character in the sequence will cause the LED to turn off.
      
    * Disconnect your Arduino from your computer.
    
    * Insert one XBee module into the XBee Explorer board. Ensure the
      orientation of the XBee module is correct with the beveled end
      of the module pointing away from the USB connector so the module
      outline matches the diagonal white lines silkscreened on the
      board.
      
    * Insert the other XBee module into the XBee Wireless Shield.
      Again, ensure the orientation of the XBee module is correct with
      the beveled end pointing over the edge of the shield away from
      the USB connector on the Arduino board. The outline of the
      module should match the the diagonal white lines silkscreened on
      the shield.
    
    * The small switch located near the TX/RX pins on the shield
      should be set to the "UART" position so the XBee module is
      connected to the hardware serial port on the Arduino.

    * Connect the XBee Explorer USB to your computer with a mini-USB
      cable.
  
    * Select the correct serial port for your XBee Explorer USB from
      the Tools > Serial Port menu in the Arduino IDE.
  
    * Connect your Arduino with the XBee Wireless Shield on it to a
      power source--either a battery, a "wall wart" power adapter or
      another USB port.
      
    * Open the Serial Monitor window of the Arduino IDE and make sure
      the correct communication speed of "9600 baud" is selected from
      the pop-up menu in the lower right corner of the Serial Monitor
      window.
      
    * You may need to press the reset button on the shield to
      redisplay the instructions.
      
    * Control your LED as before--except you've now replaced the cable
      with air!

    
   For more details and help read the XBee Shield and Explorer USB
   Quickstart Guide:
   
      <http://sparkfun.com/tutorials/192>

 */

// LED leads connected to PWM pins
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;

// Change this value if you want fast color changes
const int WAIT_TIME_MS = 500;


void setLedColour(int redIntensity, int greenIntensity, int blueIntensity) {
   /*

      This routine sets the PWM value for each color of the RGB LED.

    */
   analogWrite(RED_LED_PIN, redIntensity);
   analogWrite(GREEN_LED_PIN, greenIntensity);
   analogWrite(BLUE_LED_PIN, blueIntensity);
}


void setup() {
  // Configure the serial port and display instructions.
  Serial.begin(9600);
  Serial.println("Send the characters 'r', 'g' or 'b' to change LED colour:");
}


void loop() {

  // When specific characters are sent we change the current color of the LED.
  if (Serial.available()) {
    int characterRead = Serial.read();
    
    // If the character matches change the state of the LED,
    // otherwise ignore the character.
    switch(characterRead) {
       case 'r':
         setLedColour(255, 0, 0);
         break;
       
       case 'g':
         setLedColour(0, 255, 0);       
         break;
        
       case 'b':
         setLedColour(0, 0, 255);       
         break;
      
       case ' ':
         setLedColour(0, 0, 0);       
         break;

       default:
         // Ignore all other characters and leave the LED
         // in its previous state.
         break;
    }
    
    delay(WAIT_TIME_MS);
  }

}

1 comment:

  1. Your post is very impressive. For more information about Linux, you can use XBee for your working project. Therefore, Letsgetting started with Xbee in effective way.

    ReplyDelete