OCT3_DC.ino 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <Wire.h>
  2. #include <SparkFun_TB6612.h>
  3. // i2c configuration
  4. #define SLAVE_ADDRESS 0x04
  5. // Motor A - Vertical
  6. #define STANDBY_A A0 // Shared with B
  7. #define AIN1 8
  8. #define AIN2 9
  9. #define PWMA 10
  10. #define INVERT_A 1 // -1 to flip direction
  11. // Motor B Firing
  12. #define STANDBY_B A0 // Shared with B
  13. #define BIN1 7
  14. #define BIN2 6
  15. #define PWMB 5
  16. #define INVERT_B 1 // -1 to flip direction
  17. // Motor C - Horizontal
  18. #define STANDBY_C A1 // Separate from A and B
  19. #define CIN1 13
  20. #define CIN2 12
  21. #define PWMC 11
  22. #define INVERT_C 1 // -1 to flip direction
  23. #define LOADED_SWITCH A2
  24. // Motors!
  25. Motor m_vert( AIN1, AIN2, PWMA, INVERT_A, STANDBY_A);
  26. //Motor m_horiz( CIN1, CIN2, PWMC, INVERT_C, STANDBY_C);
  27. //Motor m_firing(BIN1, BIN2, PWMB, INVERT_B, STANDBY_B);
  28. Motor m_horiz( BIN1, BIN2, PWMB, INVERT_B, STANDBY_B);
  29. Motor m_firing(CIN1, CIN2, PWMC, INVERT_C, STANDBY_C);
  30. // Speeds
  31. // Actual PWM is a linear percentage between minspeed and maxspeed. Each speed step is 10%
  32. #define VERTICAL 0
  33. #define HORIZONTAL 1
  34. #define FIRING 2
  35. int speed[] = { 0, 0, 0 }; // Starting Motor Speeds (vertical, horizontal, firing)
  36. int minspeed[] = { 96, 32, 32}; // Min PWM
  37. int maxspeed[] = { 192, 64, 255}; // Max PWM
  38. // Run once at boot
  39. void setup()
  40. {
  41. Serial.begin(115200);
  42. Serial.println("Initializing...");
  43. // Initialize our switches
  44. pinMode(LOADED_SWITCH, INPUT_PULLUP);
  45. // i2c:
  46. Wire.begin(SLAVE_ADDRESS);
  47. Wire.onReceive(receiveData); // Interrupt
  48. Wire.onRequest(sendData); // Interrupt
  49. // Directions. Note that these are the same over the i2c bus.
  50. Serial.println("Use asdw to increase/decrease speed of the directional motors.");
  51. Serial.println("Use rf to increase/decrease speed of the firing motor.");
  52. Serial.println("Use ' ' to stop all motion.");
  53. Serial.println("Ready");
  54. /*
  55. for(int i = -100; i <= 100; i++) {
  56. Serial.print("Speed: ");
  57. Serial.print(i);
  58. Serial.print("; PWM: ");
  59. Serial.println(calc_speed(i, minspeed[VERTICAL], maxspeed[VERTICAL]));
  60. }
  61. */
  62. }
  63. void loop()
  64. {
  65. handle_serial(); /* Adjust to any user input */
  66. /* i2c bus will also be fired automatically as data is received */
  67. m_vert.drive(calc_speed(speed[VERTICAL]*10, minspeed[VERTICAL], maxspeed[VERTICAL]));
  68. m_horiz.drive(calc_speed(speed[HORIZONTAL]*10, minspeed[HORIZONTAL], maxspeed[HORIZONTAL]));
  69. m_firing.drive(calc_speed(speed[FIRING]*10, minspeed[FIRING], maxspeed[FIRING]));
  70. }/* --(end main loop )-- */
  71. int calc_speed(int speed, int min, int max) {
  72. // Returns a PWM speed that is "speed percent" of the way between min and max.
  73. int sign = 1;
  74. if(speed < -100) {
  75. speed = -100;
  76. }
  77. if(speed > 100) {
  78. speed = 100;
  79. }
  80. if(speed == 0) {
  81. return 0;
  82. }
  83. if(speed < 0) {
  84. sign = -1;
  85. speed *= -1;
  86. }
  87. return sign * ( (speed * (max - min) / 100) + min);
  88. }
  89. void handle_serial(void) {
  90. // Read bytes from the serial port if they're available and process them.
  91. while(Serial.available() > 0) {
  92. char rc = Serial.read();
  93. process_char(rc);
  94. }
  95. }
  96. void receiveData(int byteCount) {
  97. while(Wire.available() > 0) {
  98. char rc = Wire.read();
  99. process_char(rc);
  100. }
  101. }
  102. void sendData() {
  103. // Master has asked us for data, so we send our three speeds
  104. char buffer[512];
  105. sprintf(buffer, "Master requested current speeds: Vertical: %i; Horizontal: %i; Firing: %i", speed[VERTICAL], speed[HORIZONTAL], speed[FIRING]);
  106. Serial.println(buffer);
  107. Wire.write(speed[FIRING]);
  108. Wire.write(speed[HORIZONTAL]);
  109. Wire.write(speed[VERTICAL]);
  110. }
  111. void process_char(char rc) {
  112. char buffer[512]; // I smell security issue, but right now I odn't know how to do it better.
  113. bool changed = false;
  114. Serial.print("Processing character: ");
  115. Serial.println(rc);
  116. switch(rc) {
  117. case 'a':
  118. changed = true;
  119. speed[HORIZONTAL]--;
  120. Serial.print("Decreasing horizontal speed. New speed: ");
  121. Serial.println(speed[HORIZONTAL]);
  122. break;
  123. case 's':
  124. changed = true;
  125. speed[VERTICAL]--;
  126. Serial.print("Decreasing vertical speed. New speed: ");
  127. Serial.println(speed[VERTICAL]);
  128. break;
  129. case 'd':
  130. changed = true;
  131. speed[HORIZONTAL]++;
  132. Serial.print("Increasing horizontal speed. New speed: ");
  133. Serial.println(speed[HORIZONTAL]);
  134. break;
  135. case 'w':
  136. changed = true;
  137. speed[VERTICAL]++;
  138. Serial.print("Increasing vertical speed. New speed: ");
  139. Serial.println(speed[VERTICAL]);
  140. break;
  141. case 'r':
  142. changed = true;
  143. speed[FIRING]++;
  144. Serial.print("Increasing firing speed. New speed: ");
  145. Serial.println(speed[FIRING]);
  146. break;
  147. case 'f':
  148. changed = true;
  149. speed[FIRING]--;
  150. Serial.print("Decreasing firing speed. New speed: ");
  151. Serial.println(speed[FIRING]);
  152. break;
  153. case ' ':
  154. changed = true;
  155. speed[HORIZONTAL] = 0;
  156. speed[VERTICAL] = 0;
  157. speed[FIRING] = 0;
  158. Serial.println("Set all speeds to 0");
  159. break;
  160. default:
  161. Serial.println("No action associated.");
  162. }
  163. if(changed == true) {
  164. sprintf(buffer, "New Speeds. Current speeds: Vertical: %i; Horizontal: %i; Firing: %i", speed[VERTICAL], speed[HORIZONTAL], speed[FIRING]);
  165. Serial.println(buffer);
  166. }
  167. }