Bluetooth Servos

Olá a todos.
Gostaria de partilhar convosco o meu mais recente projecto.

Eu queria controlar um sistema de Pan e Tilt do meu telemóvel.

Usando:

2 Servos (Pan & Tilt)
1 Arduino Duemilanove
1 Aplicação em Processing (no computador)
1 Aplicação em Mobile Processing (no telemóvel)

Código

Para ver o código, clica em baixo onde diz "Ler Mais".

Code - Arduino

  1. /*
  2.  * BluetoothServos (Arduino)
  3.  * --------------
  4.  * Servo control from the Mobile Phone
  5.  *  Ricardo Dias (2010) - http://ricardo-dias.com/
  6.  */
  7.  
  8. #include <Servo.h>
  9.  
  10. // Servo handle
  11. Servo hServoPan;
  12. Servo hServoTilt;
  13.  
  14. // Servo pin
  15. const int pinServoPan = 9;
  16. const int pinServoTilt = 10;
  17.  
  18. // Cropping
  19. const int minServoPan = 0;
  20. const int maxServoPan = 180;
  21. const int minServoTilt = 50;
  22. const int maxServoTilt = 150;
  23.  
  24. // Current values
  25. int valServoPan;
  26. int valServoTilt;
  27.  
  28. // Auxiliar variables
  29. int received;
  30. char data[5];
  31. boolean dataReady;
  32.  
  33. void setup() {
  34.   hServoPan.attach(pinServoPan);
  35.   hServoTilt.attach(pinServoTilt);
  36.   valServoPan = maxServoPan - ((maxServoPan - minServoPan)/2);
  37.   valServoTilt = maxServoTilt - ((maxServoTilt - minServoTilt)/2);
  38.   Serial.begin(19200);
  39.   hServoPan.write(valServoPan);
  40.   hServoTilt.write(valServoTilt);
  41. }
  42.  
  43. void loop() {
  44.   // wait for serial input
  45.   if (Serial.available() > 0) {
  46.     int startChar = Serial.read();
  47.     if(startChar == (int)'#'){
  48.       while(Serial.available() < 5); // wait to receive more 5 chars
  49.       for(int i = 0; i < 5; i++){
  50.         data[i] = Serial.read();
  51.       }
  52.     }
  53.    
  54.     // Now I have an array data[] that has 5 chars of information
  55.     // I can do whatever I want with that information
  56.     if(data[0] == '1') valServoTilt+=2;
  57.     if(data[1] == '1') valServoPan+=2;
  58.     if(data[2] == '1') valServoTilt-=2;
  59.     if(data[3] == '1') valServoPan-=2;
  60.    
  61.     // Servo cropping
  62.     if(valServoPan > maxServoPan) valServoPan = maxServoPan;
  63.     else if(valServoPan < minServoPan) valServoPan = minServoPan;
  64.     if(valServoTilt > maxServoTilt) valServoTilt = maxServoTilt;
  65.     else if(valServoTilt < minServoTilt) valServoTilt = minServoTilt;
  66.    
  67.     hServoPan.write(valServoPan);
  68.     hServoTilt.write(valServoTilt);
  69.    
  70.   }
  71. }

Code - Processing

  1. /*
  2.  * BluetoothServos (Processing)
  3.  * --------------
  4.  * Servo control from the Mobile Phone
  5.  *  Ricardo Dias (2010) - http://ricardo-dias.com/
  6.  */
  7.  
  8. import bluetoothDesktop.*;
  9. import processing.serial.*;
  10.  
  11. Serial myPort;  // Create object from Serial class
  12.  
  13. PFont font;
  14. Bluetooth bt;
  15. String msg = "inactive";
  16. Client server;
  17. final String SERVICE_NAME = "simpleService";
  18. boolean connected;
  19.  
  20. int count = 0;
  21. String recebido = "";
  22.  
  23. void setup() {
  24.   size(600,300);
  25.   font = createFont("Courier", 15);
  26.   textFont(font);
  27.   try {
  28.     bt = new Bluetooth(this, Bluetooth.UUID_RFCOMM); // RFCOMM
  29.  
  30.     // Start finding the service
  31.     bt.find();
  32.     msg = "searching...";
  33.   }
  34.   catch (RuntimeException e) {
  35.     msg = "error. is your bluetooth on?";
  36.     println(e);
  37.   }
  38.   String portName = Serial.list()[0];
  39.   myPort = new Serial(this, portName, 19200);
  40. }
  41.  
  42. void draw() {
  43.   background(0);
  44.   fill(255);
  45.   text(msg, 10, height/2);
  46.   if (connected == true){
  47.        if (server.available() > 0){
  48.              recebido = server.readUTF();
  49.              // when the mobile exits the application, it sends "exit"
  50.              if(recebido.equals("exit")){
  51.                recebido = "bye bye!";
  52.                exit();
  53.              }
  54.         }
  55.   }
  56.  
  57.   text(recibido, 10, 30);
  58.   String toSend = "#"+recebido+".";
  59.   myPort.write(toSend);
  60. }
  61.  
  62.  
  63. // this gets called when the search process is over
  64. void serviceDiscoveryCompleteEvent(Service[] s) {
  65.   Service[] services = (Service[])s;
  66.  
  67.   msg = "Search completed.";
  68.  
  69.   // now search for the service we want
  70.   for (int i=0; i<services.length; i++) {
  71.     println(services[i].name);
  72.     if (services[i].name.equals(SERVICE_NAME)) {
  73.       msg = "Service " + SERVICE_NAME + " found";
  74.      
  75.       try {
  76.         // we found our service, so try to connect to it
  77.         // if we try to connect to it more than once, this will throw an error.
  78.         server = services[i].connect();
  79.         msg = "Connected to service " + SERVICE_NAME + " on server " + server.device.name;
  80.         connected = true;
  81.         return;
  82.       }
  83.       catch (Exception e) {
  84.         msg = "Found service " + SERVICE_NAME + " on Server " + server.device.name + ", but connection failed";
  85.         println(e);
  86.         return;
  87.       }
  88.     }
  89.   }
  90.  
  91.   msg = "Service " + SERVICE_NAME + " not found.";
  92. }

Code - Mobile Processing

  1. *
  2.  * BluetoothServos (Mobile Processing)
  3.  * --------------
  4.  * Servo control from the Mobile Phone
  5.  *  Ricardo Dias (2010) - http://ricardo-dias.com/
  6.  */
  7.  
  8. import processing.bluetooth.*;
  9.  
  10. final String SERVICE_NAME = "simpleService";
  11. Bluetooth bt;
  12. String[] clients = new String[0];
  13. Client teste;
  14. PFont font;
  15. String msg;  // status message
  16. boolean clientOn = false;
  17.  
  18. // teclas
  19. boolean tUp = false;
  20. boolean tRight = false;
  21. boolean tDown = false;
  22. boolean tLeft = false;
  23.  
  24. void setup() {
  25.   //framerate(15);
  26.  
  27.   // set up font
  28.   font = loadFont();
  29.   textFont(font);
  30.  
  31.   // initialize Bluetooth library
  32.   bt = new Bluetooth(this, 0x0003); // RFCOMM
  33.  
  34.   // start service
  35.   bt.start(SERVICE_NAME);
  36.  
  37.   msg = "Waiting for base...";
  38. }
  39.  
  40. void destroy() {
  41.   if(clientOn) teste.writeUTF("exit");
  42.   bt.stop();
  43. }
  44.  
  45. void draw() {
  46.   background(255);
  47.  
  48.   fill(0);
  49.   text(msg, 3,20);
  50.  
  51.   // draw connected clients
  52.   for (int i=0; i<clients.length; i++) {
  53.     text(clients[i], 6, 20+(i+1)*17);
  54.   }
  55.  
  56.   if(clientOn){
  57.     String send = "";
  58.     if(tUp) send += "1"; else send += "0";
  59.     if(tRight) send += "1"; else send += "0";
  60.     if(tDown) send += "1"; else send += "0";
  61.     if(tLeft) send += "1"; else send += "0";
  62.     teste.writeUTF(send);
  63.   }
  64. }
  65.  
  66. void keyPressed()
  67. {
  68.   if (keyCode == UP) {
  69.     tUp = true;
  70.   }else if (keyCode == RIGHT) {
  71.     tRight = true;
  72.   }else if (keyCode == DOWN) {
  73.     tDown = true;
  74.   }else if (keyCode == LEFT) {
  75.     tLeft = true;
  76.   }
  77. }
  78.  
  79. void keyReleased()
  80. {
  81.   if (keyCode == UP) {
  82.     tUp = false;
  83.   }else if (keyCode == RIGHT) {
  84.     tRight = false;
  85.   }else if (keyCode == DOWN) {
  86.     tDown = false;
  87.   }else if (keyCode == LEFT) {
  88.     tLeft = false;
  89.   }
  90. }
  91.  
  92. // gets called by BT if something happens
  93. void libraryEvent(Object library, int event, Object data) {
  94.   if (library == bt) {
  95.     if (event == Bluetooth.EVENT_CLIENT_CONNECTED && !clientOn) {
  96.       // a new client is connected.
  97.       teste = (Client)data;
  98.       clients = (String[]) append(clients, ((Client) data).device.name);
  99.       msg = "Linked to:";
  100.      clientOn = true;
  101.     }
  102.   }
  103. }

Tags: