Advertisement
Mattred12

5/7/2019 Arduino

May 1st, 2019
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  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 = 565; // Analog reading at center, 0-1023
  36. static const int JS_CENTER_1 = 565;
  37. static const int JS_CENTER_2 = 565;
  38. static const int JS_RANGE_0 = 128; // Analog range, 0-1023
  39. static const int JS_RANGE_1 = 128; // Set to 128 for Parallax joystick
  40. static const int JS_RANGE_2 = 128;
  41. static const int JS_DIR_0 = 1; // +1 or -1
  42. static const int JS_DIR_1 = 1;
  43. static const int JS_DIR_2 = 1;
  44.  
  45. // ESC/Thruster Settings
  46. static const int MAX_FWD_REV_THROTTLE = 400; // Value between 0-400
  47. static const int MAX_TURN_THROTTLE = 400; // Value between 0-400
  48. static const int MAX_VERTICAL_THROTTLE = 400; // Value between 0-400
  49. static const int MAX_VERTICAL_THROTTLE2 = 400; // Value between 0-400
  50. static const int CENTER_THROTTLE = 1500;
  51.  
  52.  
  53. // Arduino Pins
  54. static const byte JS_ADC_0 = A0;
  55. static const byte JS_ADC_1 = A1;
  56. static const byte JS_ADC_2 = A2;
  57. static const byte THRUSTER_LEFT = 9;
  58. static const byte THRUSTER_RIGHT = 10;
  59. static const byte THRUSTER_VERTICAL = 11;
  60.  
  61.  
  62. // Servos
  63. Servo thrusterLeft;
  64. Servo thrusterRight;
  65. Servo thrusterVertical;
  66.  
  67. void setup() {
  68. // Set up serial port to print inputs and outputs
  69. Serial.begin(38400);
  70.  
  71. // Set up Arduino pins to send servo signals to ESCs
  72. thrusterLeft.attach(THRUSTER_LEFT);
  73. thrusterRight.attach(THRUSTER_RIGHT);
  74. thrusterVertical.attach(THRUSTER_VERTICAL);
  75. // Set output signal to 1500 microsecond pulse (stopped command)
  76. thrusterLeft.writeMicroseconds(CENTER_THROTTLE);
  77. thrusterRight.writeMicroseconds(CENTER_THROTTLE);
  78. thrusterVertical.writeMicroseconds(CENTER_THROTTLE);
  79. // Delay to allow time for ESCs to initialize
  80. delay(7000);
  81. }
  82.  
  83. void loop() {
  84. // Read the joysticks and use the Arduino "map" function to map the raw values
  85. // to the desired output commands.
  86. int forwardCommand = map(analogRead(JS_ADC_0), // Read raw joystick value
  87. JS_CENTER_0-JS_DIR_0*JS_RANGE_0, // Joystick low value
  88. JS_CENTER_0+JS_DIR_0*JS_RANGE_0, // Joystick high value
  89. -MAX_FWD_REV_THROTTLE, // Command low value
  90. MAX_FWD_REV_THROTTLE); // Command high value
  91. int turnCommand = map(analogRead(JS_ADC_1), // Read raw joystick value
  92. JS_CENTER_1-JS_DIR_1*JS_RANGE_1, // Joystick low value
  93. JS_CENTER_1+JS_DIR_1*JS_RANGE_1, // Joystick high value
  94. -MAX_TURN_THROTTLE, // Command low value
  95. MAX_TURN_THROTTLE); // Command high value
  96. int verticalCommand = map(analogRead(JS_ADC_2), // Read raw joystick value
  97. JS_CENTER_2-JS_DIR_2*JS_RANGE_2, // Joystick low value
  98. JS_CENTER_2+JS_DIR_2*JS_RANGE_2, // Joystick high value
  99. -MAX_VERTICAL_THROTTLE, // Command low value
  100. MAX_VERTICAL_THROTTLE); // Command high value
  101. int joystickinput1 = analogRead(JS_ADC_0);
  102. int joystickinput2 = analogRead(JS_ADC_1);
  103. int joystickinput3 = analogRead(JS_ADC_2);
  104. // Combine the "stopped" command with forward, turn, and vertical and send
  105. // to the ESCs.
  106. thrusterLeft.writeMicroseconds(CENTER_THROTTLE+forwardCommand+turnCommand);
  107. thrusterRight.writeMicroseconds(CENTER_THROTTLE+forwardCommand-turnCommand);
  108. thrusterVertical.writeMicroseconds(CENTER_THROTTLE+verticalCommand);
  109.  
  110. // Output via serial
  111. Serial.print(" JS_0: "); Serial.print( joystickinput1);
  112. Serial.print(" JS_1: "); Serial.print( joystickinput2);
  113. Serial.print(" JS_2: "); Serial.print( joystickinput3);
  114. Serial.print("Fwd: "); Serial.print( forwardCommand);
  115. Serial.print(" Turn: "); Serial.print( turnCommand);
  116. Serial.print(" Vert: "); Serial.print( verticalCommand);
  117. Serial.println("");
  118.  
  119. // Delay 1/10th of a second. No need to update at super fast rates.
  120. delay(100);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement