SHARE
TWEET

4-25-19Code

Mattred12 Apr 25th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Blue Robotics Example Code
  2. -------------------------------
  3.  
  4. Title: Analog Joystick Control Example (Arduino)
  5. Description: This code is an example of how to use the Blue Robotics thrusters
  6. and ESCs to control a simple underwater vehicle. The code is designed to use
  7. analog joysticks (http://www.parallax.com/product/27808) and three thrusters.
  8. The thrusters are oriented with two pointing forward, one on the left and one
  9. on the right side of the vehicle, as well as a vertical thruster.
  10. The code is designed for the Arduino Uno board and can be compiled and
  11. uploaded via the Arduino 1.0+ software.
  12. -------------------------------
  13. The MIT License (MIT)
  14. Copyright (c) 2014 Blue Robotics Inc.
  15. Permission is hereby granted, free of charge, to any person obtaining a copy
  16. of this software and associated documentation files (the "Software"), to deal
  17. in the Software without restriction, including without limitation the rights
  18. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  19. copies of the Software, and to permit persons to whom the Software is
  20. furnished to do so, subject to the following conditions:
  21. The above copyright notice and this permission notice shall be included in
  22. all copies or substantial portions of the Software.
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. THE SOFTWARE.
  30. -------------------------------*/
  31.  
  32. #include <Servo.h>
  33.  
  34. // Joystick Settings
  35. static const int JS_CENTER_0 = 500; // Analog reading at center, 0-1023
  36. static const int JS_CENTER_1 = 500;
  37. static const int JS_CENTER_2 = 500;
  38. static const int JS_CENTER_3 = 500;
  39. static const int JS_RANGE_0 = 640; // Analog range, 0-1023
  40. static const int JS_RANGE_1 = 640; // Set to 128 for Parallax joystick
  41. static const int JS_RANGE_2 = 640;
  42. static const int JS_RANGE_3 = 640;
  43. static const int JS_DIR_0 = 1; // +1 or -1
  44. static const int JS_DIR_1 = 1;
  45. static const int JS_DIR_2 = 1;
  46. static const int JS_DIR_3 = 1;
  47.  
  48. // ESC/Thruster Settings
  49. static const int MAX_FWD_REV_THROTTLE = 400; // Value between 0-400
  50. static const int MAX_TURN_THROTTLE = 400; // Value between 0-400
  51. static const int MAX_VERTICAL_THROTTLE = 400; // Value between 0-400
  52. static const int MAX_VERTICAL_THROTTLE2 = 400; // Value between 0-400
  53. static const int CENTER_THROTTLE = 1500;
  54.  
  55.  
  56. // Arduino Pins
  57. static const byte JS_ADC_0 = A0;
  58. static const byte JS_ADC_1 = A1;
  59. static const byte JS_ADC_2 = A2;
  60. static const byte JS_ADC_3 = A3;
  61. static const byte THRUSTER_LEFT = 9;
  62. static const byte THRUSTER_RIGHT = 10;
  63. static const byte THRUSTER_VERTICAL = 11;
  64. static const byte THRUSTER_VERTICAL2 = 6;
  65.  
  66.  
  67. // Servos
  68. Servo thrusterLeft;
  69. Servo thrusterRight;
  70. Servo thrusterVertical;
  71. Servo thrusterVertical2;
  72.  
  73. void setup() {
  74.   // Set up serial port to print inputs and outputs
  75.   Serial.begin(38400);
  76.  
  77.   // Set up Arduino pins to send servo signals to ESCs
  78.   thrusterLeft.attach(THRUSTER_LEFT);
  79.   thrusterRight.attach(THRUSTER_RIGHT);
  80.   thrusterVertical.attach(THRUSTER_VERTICAL);
  81.   thrusterVertical2.attach(THRUSTER_VERTICAL2);
  82.   // Set output signal to 1500 microsecond pulse (stopped command)
  83.   thrusterLeft.writeMicroseconds(CENTER_THROTTLE);
  84.   thrusterRight.writeMicroseconds(CENTER_THROTTLE);
  85.   thrusterVertical.writeMicroseconds(CENTER_THROTTLE);
  86.   thrusterVertical2.writeMicroseconds(CENTER_THROTTLE);
  87.   // Delay to allow time for ESCs to initialize
  88.   delay(7000);
  89. }
  90.  
  91. void loop() {
  92.   // Read the joysticks and use the Arduino "map" function to map the raw values
  93.   // to the desired output commands.
  94.   int forwardCommand    = map(analogRead(JS_ADC_0), // Read raw joystick value
  95.                               JS_CENTER_0-JS_DIR_0*JS_RANGE_0, // Joystick low value
  96.                               JS_CENTER_0+JS_DIR_0*JS_RANGE_0, // Joystick high value
  97.                               -MAX_FWD_REV_THROTTLE, // Command low value
  98.                               MAX_FWD_REV_THROTTLE); // Command high value
  99.   int turnCommand       = map(analogRead(JS_ADC_1), // Read raw joystick value
  100.                               JS_CENTER_1-JS_DIR_1*JS_RANGE_1, // Joystick low value
  101.                               JS_CENTER_1+JS_DIR_1*JS_RANGE_1, // Joystick high value
  102.                               -MAX_TURN_THROTTLE, // Command low value
  103.                               MAX_TURN_THROTTLE); // Command high value
  104.   int verticalCommand   = map(analogRead(JS_ADC_2), // Read raw joystick value
  105.                               JS_CENTER_2-JS_DIR_2*JS_RANGE_2, // Joystick low value
  106.                               JS_CENTER_2+JS_DIR_2*JS_RANGE_2, // Joystick high value
  107.                               -MAX_VERTICAL_THROTTLE, // Command low value
  108.                               MAX_VERTICAL_THROTTLE); // Command high value
  109.                              
  110.    
  111.   // Combine the "stopped" command with forward, turn, and vertical and send
  112.   // to the ESCs.
  113.   thrusterLeft.writeMicroseconds(CENTER_THROTTLE+forwardCommand+turnCommand);
  114.   thrusterRight.writeMicroseconds(CENTER_THROTTLE+forwardCommand-turnCommand);
  115.   thrusterVertical.writeMicroseconds(CENTER_THROTTLE+verticalCommand);
  116.   thrusterVertical2.writeMicroseconds(CENTER_THROTTLE+verticalCommand.);
  117.   // Output via serial
  118.   Serial.print("Fwd: "); Serial.print(forwardCommand);
  119.   Serial.print("Turn: "); Serial.print(turnCommand);
  120.   Serial.print("Vert: "); Serial.print(verticalCommand);
  121.   Serial.println("");
  122.  
  123.   // Delay 1/10th of a second. No need to update at super fast rates.
  124.   delay(500);
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top