Here we will see how we can make remote controlled car using Sun SPOTs powered by Java!!
Requirements:
1. Sun SPOT Kit. (Two Sun SPOTs)
2. IC L2393D.
3. DC Motors 2 Nos.
4. A car which you can dismantle and control using DC Motors having enough space to accomodate Sun SPOT and circuit..
5. Patience and some nice coffee and refreshments!!!
Hardware:
The Hardware basically centers around Sun SPOTs and DC Motors controlled by IC L293D.
The Basic Model is as Follows:
Here remote Sun SPOT will send data to Sun SPOT on car which will drive the IC according to DC IO pins D0 - D3. The IC will drive the Motors which will run the car.
The circuit diagram for the IC is as follows. I think its self explanatory.
Software:
The Software consist of two parts:
1. Remote Control Spot.
Make a new Sun Spot Application say - "RemoteSpot" and add the following code to the StartApp file:
package org.sunspotworld.demo; import com.sun.spot.io.j2me.radiogram.Radiogram; import com.sun.spot.io.j2me.radiogram.RadiogramConnection; import com.sun.spot.peripheral.NoRouteException; import com.sun.spot.peripheral.Spot; import com.sun.spot.peripheral.radio.IRadioPolicyManager; import com.sun.spot.sensorboard.EDemoBoard; import com.sun.spot.sensorboard.peripheral.ITriColorLED; import com.sun.spot.sensorboard.peripheral.IAccelerometer3D; import java.io.IOException; import javax.microedition.midlet.MIDletStateChangeException; import com.sun.spot.util.IEEEAddress; import com.sun.spot.util.Utils; import javax.microedition.io.Connector; /** * Remote Spot will send Tilt values to the Car Spot. * * @author Jay Mahadeokar */ public class StartApp extends javax.microedition.midlet.MIDlet { EDemoBoard demoboard = EDemoBoard.getInstance(); protected void startApp() throws MIDletStateChangeException { IRadioPolicyManager rpm = Spot.getInstance().getRadioPolicyManager(); IEEEAddress myAddr = new IEEEAddress(rpm.getIEEEAddress()); System.out.println("Hi! my address = " + myAddr.asDottedHex()); //This is my Car Sun SPOT address IEEEAddress bsAddress = new IEEEAddress(new String("0014.4F01.0000.1231")); IAccelerometer3D acc = demoboard.getAccelerometer(); while(true) { try{ RadiogramConnection conn = (RadiogramConnection)Connector.open("radiogram://0014.4F01.0000.1A29:10"); Radiogram rdg = (Radiogram)conn.newDatagram(conn.getMaximumLength()); try { //Get control called and value sent to car spot! rdg.writeUTF(""+getControl()); conn.send(rdg); Utils.sleep(200); // Sleep for sometime! } catch (NoRouteException e) { System.out.println ("No route to 0014.4F01.0000.1A29"); } finally { conn.close(); } } catch(IOException e){ System.out.println("There is a problem opening the connection"); } } } //Returns the numeric value corresponding to the current Tilt Positions of //Sun SPOT //Return values only if Tilt > 0.5 or Tilt < -0.5 public int getControl() { int ret = 0; try { double xTilt = demoboard.getAccelerometer().getTiltX(); double yTilt = demoboard.getAccelerometer().getTiltY(); if (xTilt > 0.5 && yTilt < 0.5 && yTilt > -0.5) { ret = 110; } if (xTilt < -0.5 && yTilt < 0.5 && yTilt > -0.5) { ret = 101; } if (yTilt > 0.5 && xTilt < 0.5 && xTilt > -0.5) { ret = 100; } if (yTilt < -0.5 && xTilt < 0.5 && xTilt > -0.5) { ret = 1000; } } catch (IOException ex) { ex.printStackTrace(); } return ret; } protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs(); for (int i = 0; i < 8; i++) { // turn off the LEDs when we exit leds[i].setOff(); } } protected void pauseApp() { } }
2. Car Spot Application.
Make another Sun Spot App and add following code to StartApp. This Spot will be placed on Car and will drive the IC.
package org.sunspotworld.demo; import com.sun.spot.io.j2me.radiogram.Radiogram; import com.sun.spot.io.j2me.radiogram.RadiogramConnection; import com.sun.spot.peripheral.NoRouteException; import com.sun.spot.peripheral.Spot; import com.sun.spot.sensorboard.EDemoBoard; import com.sun.spot.sensorboard.peripheral.ITriColorLED; import javax.microedition.midlet.MIDletStateChangeException; import com.sun.spot.sensorboard.io.IIOPin; import com.sun.spot.sensorboard.peripheral.LEDColor; import com.sun.spot.util.IEEEAddress; import javax.microedition.io.Connector; /** * Listens to the Tilt values being sent by the Remote Spots * Changes DC IO Pin values accordingly. * * @author Jay Mahadeokar */ public class StartApp extends javax.microedition.midlet.MIDlet { private IIOPin[] pins = EDemoBoard.getInstance().getIOPins(); private ITriColorLED[] leds = EDemoBoard.getInstance().getLEDs(); protected void startApp() throws MIDletStateChangeException { IEEEAddress myAddr = new IEEEAddress(Spot.getInstance().getRadioPolicyManager().getIEEEAddress()); System.out.println("Hi! my address = " + myAddr.asDottedHex()); while(true) { try { RadiogramConnection conn = (RadiogramConnection) Connector.open("radiogram://:10"); Radiogram rdg = (Radiogram)conn.newDatagram(conn.getMaximumLength()); try { conn.receive(rdg); String s = rdg.readUTF(); int state = Integer.parseInt(s); System.out.println("state: "+state); boolean b[] = new boolean[4]; for(int i=0;i<4;i++) { if(state%10 == 1) b[i] = true; else b[i] = false; System.out.println(""+b[i]); state = state/10; } setSignal(b); } catch (NoRouteException e) { System.out.println("No route to any one ..."); } finally { conn.close(); } } catch(Exception e){ System.out.println("A problem opening the connection ..."); } } } public void setSignal(boolean b[]) { for(int i=0;i<4;i++) { leds[i].setColor(LEDColor.CYAN); if(b[i] == true) { pins[i].setHigh(); leds[i].setOn(); } else { pins[i].setLow(); leds[i].setOff(); } } } protected void destroyApp(boolean unconditional) throws MIDletStateChangeException { for (int i = 0; i < 8; i++) { // turn off the LEDs when we exit leds[i].setOff(); } } protected void pauseApp() { } }
Thats It!!! Now biuld the Apps and Deploy On Sun SPOTs. Mount the Car SPOT on Car and control it using Remote Spot!!
You can control car by tilting Spot:
Left - To Move left and accelarate.
Right - To move Right and Accelarate.
Down - To Move the Car Forward.
Up - To Reverse.
Note: The connections of circuit may be really tedious and may take a lot of time!
Thanks a lot to Mr. Abhishek Hisaria who designed the Hardware.
Sun SPOT Controlled Car
See more Pictures at: http://picasaweb.google.com/jai.mahadeokar/SunBlog
Cheers!!





