Advertisement
Guest User

motorTest

a guest
Sep 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////
  2. //Define libraries
  3. ////////////////////////////////////////////////////////
  4. //The libary section of the code does not need to be altered
  5.  
  6. // The Intel Curie libraries are required for reading sensor values and
  7. // communicating with Bluetooth
  8. #include <CurieBLE.h>
  9. #include <CurieIMU.h>
  10.  
  11. // Servo library is required for sending high frequency PWM
  12. // signals to the ESCs.
  13. #include <Servo.h>
  14.  
  15. // BLE library is required to send data to the USB BLE dongle
  16. #include <BLESerial.h>
  17.  
  18. ////////////////////////////////////////////////////////
  19. //Define variables
  20. ////////////////////////////////////////////////////////
  21.  
  22. //System variables
  23. int serialPrintCounter = 0;
  24. int bluetoothPrintCounter = 0;
  25. char BLEString[50];
  26. long sampleTime;
  27. long sampleTimeOld;
  28. float sampleTimeMillis;
  29.  
  30. //Motor control variables
  31. int addressESCLeftFront = 3;  //Svart
  32. int addressESCRightFront = 5; //SvartR
  33. int addressESCLeftBack = 6;   //RödR
  34. int addressESCRightBack = 9;  //Röd
  35.  
  36. int controlSignal;
  37.  
  38. Servo ESCRightFront;
  39. Servo ESCLeftFront;
  40. Servo ESCRightBack;
  41. Servo ESCLeftBack;
  42.  
  43. ////////////////////////////////////////////////////////
  44. //Functions for handling interrupts
  45. ////////////////////////////////////////////////////////
  46.  
  47. void setup()
  48. {
  49.   //Code in setup() function will only execute once, when power is supplied to
  50.   //the CPU, when restarted or when new firmware is loaded
  51.  
  52.   ////////////////////////////////////////////////////////
  53.   //System setup
  54.   ////////////////////////////////////////////////////////
  55.  
  56.   Serial.begin(9600); // Start serial communication (with computer) at 57600 bps
  57.   while (!Serial);
  58.  
  59.   sampleTimeOld = micros();
  60.  
  61.   //BLESerial.setName("Bluno101"); // Start bluetooth communication
  62.   //BLESerial.begin();
  63.   //while(!BLESerial);
  64.  
  65.   ////////////////////////////////////////////////////////
  66.   //Initiate the ESCs
  67.   ////////////////////////////////////////////////////////
  68.  
  69.   //Declare the address (pin) of the ESCs
  70.   pinMode(addressESCLeftFront, OUTPUT);
  71.   pinMode(addressESCRightFront, OUTPUT);
  72.   pinMode(addressESCLeftBack, OUTPUT);
  73.   pinMode(addressESCRightBack, OUTPUT);
  74.   pinMode(13, OUTPUT);
  75.  
  76.   //Start with zero speed command to the ESCs
  77.   ESCLeftFront.attach(addressESCLeftFront);
  78.   ESCRightFront.attach(addressESCRightFront);
  79.   ESCLeftBack.attach(addressESCLeftBack);
  80.   ESCRightBack.attach(addressESCRightBack);
  81.  
  82.   ESCLeftFront.writeMicroseconds(1000);
  83.   ESCRightFront.writeMicroseconds(1000);
  84.   ESCLeftBack.writeMicroseconds(1000);
  85.   ESCRightBack.writeMicroseconds(1000);
  86.  
  87. }
  88.  
  89. void loop()
  90. {
  91.   //Code in loop() function will execute repeatedly for as long as the CPU has power
  92.  
  93.   ////////////////////////////////////////////////////////
  94.   //System update
  95.   ////////////////////////////////////////////////////////
  96.  
  97.   serialPrintCounter++;
  98.   bluetoothPrintCounter++;
  99.  
  100.   sampleTime = micros() - sampleTimeOld;
  101.   sampleTimeMillis = (float) sampleTime / 1000;
  102.   sampleTimeOld = micros();
  103.  
  104.   if (BLESerial.operator bool() == true) { //Turn on LED if com is ok
  105.     digitalWrite(13, HIGH);
  106.   }
  107.  
  108.   ////////////////////////////////////////////////////////
  109.   //Read the sensor values
  110.   ////////////////////////////////////////////////////////
  111.  
  112.   // To read from the gyroscope, you must first call the
  113.   // read function. When this exits, it'll update the
  114.   // input variables with the most current data.
  115.  
  116.   // To read from the accelerometer, you must first call the
  117.   // read function. When this exits, it'll update the
  118.   // input variables with the most current data.
  119.  
  120.   if (Serial.available() > 0) {
  121.  
  122.     // read the incoming byte:
  123.     String myNumber = Serial.readString();
  124.  
  125.     if (myNumber.toInt() > 500 && myNumber.toInt() < 2500) {
  126.       Serial.println(myNumber);
  127.       controlSignal = myNumber.toInt();
  128.     }
  129.   }
  130.  
  131.   ////////////////////////////////////////////////////////
  132.   //Control signal saturation check
  133.   ////////////////////////////////////////////////////////
  134.  
  135.   if (controlSignal > 2000) {
  136.     controlSignal = 2000;
  137.   }
  138.   else if (controlSignal < 1000) {
  139.     controlSignal = 1000;
  140.   }
  141.  
  142.   ////////////////////////////////////////////////////////
  143.   //ESC control signal write
  144.   ////////////////////////////////////////////////////////
  145.  
  146.   ESCLeftFront.writeMicroseconds(controlSignal);
  147.   ESCRightFront.writeMicroseconds(controlSignal);
  148.   ESCLeftBack.writeMicroseconds(controlSignal);
  149.   ESCRightBack.writeMicroseconds(controlSignal);
  150.  
  151.   ////////////////////////////////////////////////////////
  152.   //Print signal values
  153.   ////////////////////////////////////////////////////////
  154.  
  155.   if (serialPrintCounter >= 999) {
  156.  
  157.     Serial.println(controlSignal);
  158.     /*
  159.         Serial.print("G: ");
  160.         Serial.print(gx);
  161.         Serial.print(", ");
  162.         Serial.print(gy);
  163.         Serial.print(", ");
  164.         Serial.println(gz);
  165.         Serial.print("A: ");
  166.         Serial.print(ax);
  167.         Serial.print(", ");
  168.         Serial.print(ay);
  169.         Serial.print(", ");
  170.         Serial.println(az);
  171.         Serial.print("IMU Pitch: ");
  172.         Serial.println(sensorPitch);
  173.         Serial.print("IMU Roll: ");
  174.         Serial.println(sensorRoll);
  175.         Serial.print("IMU Yaw: ");
  176.         Serial.println(sensorYaw);
  177.         Serial.print("Transmitter Throttle: ");
  178.         Serial.print(transmitterThrottleRaw);
  179.         Serial.print(", ");
  180.         Serial.println(transmitterThrottleMapped);
  181.         Serial.print("Transmitter Pitch: ");
  182.         Serial.print(transmitterPitchRaw);
  183.         Serial.print(", ");
  184.         Serial.println(transmitterPitchMapped);
  185.         Serial.print("Transmitter Roll: ");
  186.         Serial.print(transmitterRollRaw);
  187.         Serial.print(", ");
  188.         Serial.println(transmitterRollMapped);
  189.         Serial.print("Transmitter Yaw: ");
  190.         Serial.print(transmitterYawRaw);
  191.         Serial.print(", ");
  192.         Serial.println(transmitterYawMapped);
  193.         Serial.print("PID Pitch: ");
  194.         Serial.print(errorPitch);
  195.         Serial.print(", ");
  196.         Serial.print(PpartPitch);
  197.         Serial.print(", ");
  198.         Serial.print(IpartPitch);
  199.         Serial.print(", ");
  200.         Serial.print(DpartPitch);
  201.         Serial.print(", ");
  202.         Serial.println(PIDPitch);
  203.         Serial.print("PID Roll: ");
  204.         Serial.print(errorRoll);
  205.         Serial.print(", ");
  206.         Serial.print(PpartRoll);
  207.         Serial.print(", ");
  208.         Serial.print(IpartRoll);
  209.         Serial.print(", ");
  210.         Serial.print(DpartRoll);
  211.         Serial.print(", ");
  212.         Serial.println(PIDRoll);
  213.         Serial.print("PID Yaw: ");
  214.         Serial.print(errorYaw);
  215.         Serial.print(", ");
  216.         Serial.print(PpartYaw);
  217.         Serial.print(", ");
  218.         Serial.print(IpartYaw);
  219.         Serial.print(", ");
  220.         Serial.print(DpartYaw);
  221.         Serial.print(", ");
  222.         Serial.println(PIDYaw);
  223.         Serial.print("Control Signals: ");
  224.         Serial.print(controlSignalRightFront);
  225.         Serial.print(", ");
  226.         Serial.print(controlSignalLeftFront);
  227.         Serial.print(", ");
  228.         Serial.print(controlSignalRightBack);
  229.         Serial.print(", ");
  230.         Serial.println(controlSignalLeftBack);
  231.         Serial.print("Sample Time: ");
  232.         Serial.println(sampleTimeMillis);
  233.  
  234.         //Variables for graphing
  235.         Serial.print(transmitterThrottleMapped);
  236.         Serial.print("SensorPitch: ");
  237.         Serial.print(sensorPitch);
  238.         Serial.print(" SensorRoll: ");
  239.         Serial.print(sensorRoll);
  240.         Serial.print(" SensorYaw: ");
  241.         Serial.println(sensorYaw);
  242.         Serial.print("PIDPitch: ");
  243.         Serial.println(PIDPitch);
  244.         Serial.print("PIDRoll: ");
  245.         Serial.println(PIDRoll);
  246.         Serial.print("PIDYaw: ");
  247.         Serial.println(PIDYaw);
  248.         Serial.print("LF: ");
  249.         Serial.println(controlSignalLeftFront);
  250.         Serial.print("RF: ");
  251.         Serial.println(controlSignalRightFront);
  252.         Serial.print("LB: ");
  253.         Serial.println(controlSignalLeftBack);
  254.         Serial.print("RB: ");
  255.         Serial.println(controlSignalRightBack);
  256.     */
  257.     serialPrintCounter = 0;
  258.   }
  259.  
  260.   ////////////////////////////////////////////////////////
  261.   //Collect bluetooth data for PID parameter analyzing
  262.   ////////////////////////////////////////////////////////
  263.  
  264.   if (bluetoothPrintCounter >= 200 && BLESerial.operator bool() == true) {
  265.     /*
  266.       sprintf(BLEString, "%6ld %03.01f %03.01f\n", transmitterPitchMapped, sensorPitch, PIDPitch);
  267.  
  268.       sprintf(BLEString, "%6ld %03.01f %03.01f\n", transmitterRollMapped, sensorRoll, PIDRoll);
  269.  
  270.       sprintf(BLEString, "%6ld %03.01f %03.01f\n", transmitterYawMapped, sensorYaw, PIDYaw);
  271.  
  272.       BLESerial.write(BLEString);
  273.     */
  274.  
  275.     bluetoothPrintCounter = 0;
  276.   }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement