Advertisement
Guest User

Untitled

a guest
May 6th, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.18 KB | None | 0 0
  1. // Yellow bot code (now with cereal box)
  2. // By Tyler Bletsch (Tyler.Bletsch@gmail.com)
  3.  
  4. #include <SimpleSoftwareServo.h> // this is my library; you can replace with regular servo library too)
  5. #include <SoftwareSerial.h>
  6.  
  7. int noSignal = 0;
  8.  
  9. SoftwareSerial bluetooth(A0,A1); //RX,TX  note that RX will go to the TX on the bluetooth, and TX will go to the RX on the bluetooth
  10.  
  11. #define control_serial bluetooth // set to Serial for wired, bluetooth for wireless
  12.  
  13. SimpleSoftwareServo servo1;
  14. int servo1_pin = 4;
  15.  
  16. #define    STX          0x02
  17. #define    ETX          0x03
  18.  
  19. // for Joystick BT Commander
  20. int joyX;
  21. int joyY;
  22. bool button_state[6] = {0};
  23. long sendInterval = 750;                               // interval between Buttons status transmission (milliseconds)
  24.  
  25. int motorL[] = {5,6};
  26. int motorR[] = {9,10};
  27.  
  28. void motor_setSpeed(int pin1, int pin2, int speed) {
  29.   if (speed<0) { // backward
  30.     speed = -speed;
  31.     analogWrite(pin1,0);
  32.     analogWrite(pin2,speed);
  33.   } else {
  34.     analogWrite(pin1,speed);
  35.     analogWrite(pin2,0);
  36.   }
  37. }
  38.  
  39. #define motorL_setSpeed(speed) motor_setSpeed(motorL[0],motorL[1],speed)
  40. #define motorR_setSpeed(speed) motor_setSpeed(motorR[0],motorR[1],speed)
  41.  
  42. void setup(){
  43.   Serial.begin(9600);
  44.   Serial.println("Robot booted.");
  45.   bluetooth.begin(9600);
  46.   motorL_setSpeed(0);
  47.   motorR_setSpeed(0);
  48.   servo1.attach(servo1_pin);
  49.  
  50.   pinMode(motorL[0],OUTPUT);
  51.   pinMode(motorL[1],OUTPUT);
  52.   pinMode(motorR[0],OUTPUT);
  53.   pinMode(motorR[1],OUTPUT);
  54.  
  55.  
  56.   while (0) {
  57.     digitalWrite(5,1);
  58.     digitalWrite(6,0);
  59.   }
  60.   //myServo.attach(9);
  61.  
  62. }
  63.  
  64. long latency = -1;
  65.  
  66. void loop() {
  67.   long tStart = micros();
  68.   // interact with Joystick BT Commander via bluetooth serial, populates globals joyX, joyY (in range -100..+100), and button_state[]
  69.   receiveBluetooth();
  70.   sendBluetooth();
  71.  
  72.   motor_setSpeed_curve(-joyY/100.0,-joyX/100.0);
  73.  
  74.   int servo_angle = button_state[0] ? 45 : 180;
  75.  
  76.   servo1.write(servo_angle);
  77.   SimpleSoftwareServo::refresh();  
  78.  
  79.   latency = micros() - tStart;
  80.  
  81.   //delay(40);
  82. }
  83.  
  84. void motor_setSpeed_curve(float mag, float curve) {
  85.   float sensitivity = 0.5;
  86.   float leftOutput, rightOutput; // -1..+1
  87.   float value,ratio;
  88.   if (curve < 0) {
  89.     value = log(-curve);
  90.     ratio = (value - sensitivity) / (value + sensitivity);
  91.     if (ratio == 0) {
  92.       ratio = .0000000001;
  93.     }
  94.     leftOutput = mag / ratio;
  95.     rightOutput = mag;
  96.   } else if (curve > 0) {
  97.     value = log(curve);
  98.     ratio = (value - sensitivity) / (value + sensitivity);
  99.     if (ratio == 0) {
  100.         ratio = .0000000001;
  101.     }
  102.     leftOutput = mag;
  103.     rightOutput = mag / ratio;
  104.   } else {
  105.     leftOutput = mag;
  106.     rightOutput = mag;
  107.   }
  108.  
  109.   motorL_setSpeed(255*leftOutput);
  110.   motorR_setSpeed(255*rightOutput);
  111.  
  112. }
  113.  
  114. // The bluetooth format code from Joystick BT Commander, adapted and cleaned up from 'AndroTest_V20_POSTED' - http://forum.arduino.cc/index.php?topic=173246.msg1766646#msg1766646
  115. void receiveBluetooth() {
  116.   byte cmd[8] = {0};                 // bytes received
  117.   if(control_serial.available())  {                           // data received from smartphone
  118.     delay(2);
  119.     cmd[0] =  control_serial.read();  
  120.     if(cmd[0] == STX)  {
  121.       int i=1;      
  122.       while(control_serial.available())  {
  123.         delay(1);
  124.         cmd[i] = control_serial.read();
  125.         if(cmd[i]>127 || i>7)                 break;     // Communication error
  126.         if((cmd[i]==ETX) && (i==2 || i==7))   break;     // Button or Joystick data
  127.         i++;
  128.       }
  129.       if     (i==2)          getButtonState(cmd[1]);    // 3 Bytes  ex: < STX "C" ETX >
  130.       else if(i==7)          getJoystickState(cmd);     // 6 Bytes  ex: < STX "200" "180" ETX >
  131.     }
  132.   }
  133. }
  134.  
  135. void sendBluetooth()  {
  136.   static long previousMillis = 0;                            
  137.   long currentMillis = millis();
  138.   if(currentMillis - previousMillis > sendInterval) {   // send data back to smartphone
  139.     previousMillis = currentMillis;
  140.  
  141. // Data frame transmitted back from Arduino to Android device:
  142. // < 0X02   Buttons state   0X01   DataField#1   0x04   DataField#2   0x05   DataField#3    0x03 >  
  143. // < 0X02      "01011"      0X01     "120.00"    0x04     "-4500"     0x05  "Motor enabled" 0x03 >    // example
  144.  
  145.     control_serial.print((char)STX);                                             // Start of Transmission
  146.     control_serial.print("butstat?");  control_serial.print((char)0x1);   // buttons status feedback    // (no evidence this is used --tkb)
  147.     control_serial.print(millis());            control_serial.print((char)0x4);   // datafield #1
  148.     control_serial.print(latency);          control_serial.print((char)0x5);   // datafield #2
  149.     control_serial.print("Sup");                                         // datafield #3
  150.     control_serial.print((char)ETX);                                             // End of Transmission
  151.   }  
  152. }
  153.  
  154. void getJoystickState(byte data[8])    {
  155.   int _joyX = (data[1]-'0')*100 + (data[2]-'0')*10 + (data[3]-'0');       // obtain the Int from the ASCII representation
  156.   int _joyY = (data[4]-'0')*100 + (data[5]-'0')*10 + (data[6]-'0');
  157.   _joyX = _joyX - 200;                                                  // Offset to avoid
  158.   _joyY = _joyY - 200;                                                  // transmitting negative numbers
  159.  
  160.   if(_joyX<-100 || _joyX>100 || _joyY<-100 || _joyY>100)     return;      // commmunication error
  161.  
  162.   joyX = _joyX;
  163.   joyY = _joyY;
  164. }
  165.  
  166. void button_on(int b) {
  167.   button_state[b] = true;
  168.   Serial.print("Button "); Serial.print(b); Serial.println(" ON");
  169. }
  170.  
  171. void button_off(int b) {
  172.   button_state[b] = false;
  173.   Serial.print("Button "); Serial.print(b); Serial.println(" off");
  174. }
  175.  
  176. void getButtonState(byte bStatus)  {
  177.   switch (bStatus) {
  178.     case 'A':  button_on(0); break;
  179.     case 'B': button_off(0); break;
  180.     case 'C':  button_on(1); break;
  181.     case 'D': button_off(1); break;
  182.     case 'E':  button_on(2); break;
  183.     case 'F': button_off(2); break;
  184.     case 'G':  button_on(3); break;
  185.     case 'H': button_off(3); break;
  186.     case 'I':  button_on(4); break;
  187.     case 'J': button_off(4); break;
  188.     case 'K':  button_on(5); break;
  189.     case 'L': button_off(5); break;
  190.   }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement