Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <SabertoothSimplified.h>
  3. // Include the required Wire library for I2C<br>#include
  4. #include <Wire.h>
  5.  
  6. // RX on pin 17 (to S2), TX on pin 16 (to S1).
  7. SoftwareSerial SWSerial(NOT_A_PIN, 16);
  8. // Use SWSerial as the serial port.
  9. SabertoothSimplified ST(SWSerial);
  10.  
  11. //////////////////ENCODER DATA//////////////////
  12. unsigned int revolutions_L_rpm = 0;
  13. unsigned int revolutions_R_rpm = 0;
  14.  
  15. int16_t x = 0;
  16. int16_t y = 0;
  17. ////////////////////////////////////////////////
  18.  
  19. //////////////VARIABLES FOR ADJUST//////////////
  20. int error = 0;
  21. int kp = 12;
  22. int adjusted = 0;
  23. ////////////////////////////////////////////////
  24.  
  25. ////////////////////MOTORS//////////////////////
  26. //Declare the arduino pins
  27. int LEDg = 7;
  28. int LEDr = 6;
  29. int LEDy = 5;
  30. int speedVar = 0;
  31. int speedOne = 0;
  32. int speedTwo = 0;
  33. int power;
  34. ////////////////////END/////////////////////////
  35.  
  36. void setup() {
  37. //initlize the mode of the pins
  38. pinMode(LEDg,OUTPUT);
  39. pinMode(LEDr,OUTPUT);
  40. pinMode(LEDy,OUTPUT);
  41. //set the serial communication rate
  42. Serial.begin(9600);
  43. SWSerial.begin(9600);
  44. Wire.begin();
  45. }
  46.  
  47. void loop()
  48. {
  49. //check whether arduino is reciving signal or not
  50. if(Serial.available() > 0){
  51. char val = Serial.read() ;//reads the signal
  52. Serial.print("Recieved: ");
  53. Serial.println(val);
  54.  
  55. switch(val){
  56. /*********Increase speed by 1 as long as e(triangle) is held*********/
  57. case 'a':
  58. forward();
  59. break;
  60.  
  61. /*********Decrease speed by 1 as long as g(x) is held*********/
  62. case 'c':
  63. reverse();
  64. break;
  65.  
  66. /*********Increase speed by 1 as long as e(triangle) is held*********/
  67. case 'd':
  68. turnLeft();
  69. break;
  70.  
  71. /*********Decrease speed by 1 as long as g(x) is held*********/
  72. case 'b':
  73. turnRight();
  74. break;
  75.  
  76. /*********Toggle when Circle is held for 5 seconds*********/
  77. case 'f':
  78. toggleSwitch(LEDy);
  79. break;
  80.  
  81. /*********Toggle when Square is held for 5 seconds*********/
  82. case 'h':
  83. stopMotors();
  84. break;
  85. }
  86. Serial.print("sppedVar = ");
  87. Serial.print(speedVar);
  88. Serial.print("tleftSpeed: ");
  89. Serial.print(speedOne);
  90. Serial.print("trightSpeed: ");
  91. Serial.println(speedTwo);
  92. }
  93.  
  94. Wire.requestFrom(9,4); // Request 4 bytes from slave arduino (9)
  95. byte a = Wire.read();
  96. Serial.print("a: ");
  97. Serial.print(a);
  98. byte b = Wire.read();
  99. Serial.print(" b: ");
  100. Serial.print(b);
  101. byte e = Wire.read();
  102. Serial.print(" --- e: ");
  103. Serial.print(e);
  104. byte f = Wire.read();
  105. Serial.print(" f: ");
  106. Serial.print(f);
  107. x = a;
  108. x = (x << 8) | b;
  109. Serial.print("tX: ");
  110. Serial.print(x);
  111. y = e;
  112. y = (y << 8) | f;
  113. Serial.print("tY: ");
  114. Serial.print(y);
  115. revolutions_L_rpm = x;
  116. revolutions_R_rpm = y;
  117.  
  118. if ((revolutions_L_rpm != revolutions_R_rpm) && (speedVar != 0)){
  119. error = 0;
  120. error = revolutions_L_rpm - revolutions_R_rpm;
  121. adjusted = error/kp;
  122. Serial.print("Error: ");
  123. Serial.print(error);
  124. Serial.print("Error/kp: ");
  125. Serial.println(adjusted);
  126.  
  127. if ((speedTwo < 20) && (speedTwo > -20)){
  128. speedTwo -= adjusted;
  129. power = speedTwo;
  130. ST.motor(2, -power);
  131. //delay(20);
  132. }
  133. }
  134.  
  135. // Print out rpm
  136. Serial.print("Left motor rps*100: ");
  137. Serial.print(revolutions_L_rpm);
  138. Serial.print(" ///// Right motor rps*100: ");
  139. Serial.println(revolutions_R_rpm);
  140.  
  141. // Print out speed
  142. Serial.print("speedOne: ");
  143. Serial.print(speedOne);
  144. Serial.print("tspeedTwo: ");
  145. Serial.println(speedTwo);
  146.  
  147. delay(1000);
  148. }
  149.  
  150. // Include the required Wire library for I2C<br>#include <Wire.h>
  151. #include <Wire.h>
  152.  
  153. // Checked for main program
  154. volatile boolean counterReady;
  155.  
  156. // Internal to counting routine
  157. unsigned int timerPeriod;
  158. unsigned int timerTicks;
  159. unsigned long overflowCount;
  160.  
  161.  
  162. // The pin the encoder is connected
  163. int encoder_in_L = 2;
  164. int encoder_in_R = 3;
  165.  
  166. // The number of pulses per revolution
  167. // depends on your index disc!!
  168. unsigned int pulsesperturn = 16;
  169.  
  170. // The total number of revolutions
  171. int16_t revolutions_L = 0;
  172. int16_t revolutions_R = 0;
  173.  
  174. int16_t revolutions_L_rpm = 0;
  175. int16_t revolutions_R_rpm = 0;
  176.  
  177. // Initialize the counter
  178. int16_t pulses_L = 0;
  179. int16_t pulses_R = 0;
  180.  
  181. byte myData[4];
  182.  
  183. // This function is called by the interrupt
  184. void count_L() {
  185. pulses_L++;
  186. }
  187. void count_R() {
  188. pulses_R++;
  189. }
  190.  
  191. void startCounting(unsigned int ms) {
  192. counterReady = false; // time not up yet
  193. timerPeriod = ms; // how many ms to count to
  194. timerTicks = 0; // reset interrupt counter
  195. overflowCount = 0; // no overflows yet
  196.  
  197. // Reset timer 2
  198. TCCR2A = 0;
  199. TCCR2B = 0;
  200.  
  201. // Timer 2 - gives us our 1 ms counting interval
  202. // 16 MHz clock (62.5 ns per tick) - prescaled by 128
  203. // counter increments every 8 µs.
  204. // So we count 125 of them, giving exactly 1000 µs (1 ms)
  205. TCCR2A = bit (WGM21) ; // CTC mode
  206. OCR2A = 124; // count up to 125 (zero relative!!!!)
  207.  
  208. // Timer 2 - interrupt on match (ie. every 1 ms)
  209. TIMSK2 = bit (OCIE2A); // enable Timer2 Interrupt
  210.  
  211. TCNT2 = 0; // set counter to zero
  212.  
  213. // Reset prescalers
  214. GTCCR = bit (PSRASY); // reset prescaler now
  215. // start Timer 2
  216. TCCR2B = bit (CS20) | bit (CS22) ; // prescaler of 128
  217. }
  218.  
  219. ISR (TIMER2_COMPA_vect){
  220. // see if we have reached timing period
  221. if (++timerTicks < timerPeriod)
  222. return;
  223.  
  224. TCCR2A = 0; // stop timer 2
  225. TCCR2B = 0;
  226. TIMSK2 = 0; // disable Timer2 Interrupt
  227. counterReady = true;
  228. if(counterReady){
  229. Serial.print("Pulses_L: ");
  230. Serial.print(pulses_L);
  231. Serial.print(" Pulses_R: ");
  232. Serial.println(pulses_R);
  233.  
  234. // multiplying by 100 to get a greater difference to compare
  235. revolutions_L_rpm = (pulses_L * 100) / pulsesperturn;
  236. revolutions_R_rpm = (pulses_R * 100) / pulsesperturn;
  237.  
  238. // Total revolutions
  239. // revolutions_L = revolutions_L + (pulses_L / pulsesperturn);
  240. // revolutions_R = revolutions_R + (pulses_R / pulsesperturn);
  241.  
  242. pulses_L = 0;
  243. pulses_R = 0;
  244. }
  245. }
  246.  
  247. void requestEvent() {
  248. myData[0] = (revolutions_L_rpm >> 8) & 0xFF;
  249. myData[1] = revolutions_L_rpm & 0xFF;
  250. myData[2] = (revolutions_R_rpm >> 8) & 0xFF;
  251. myData[3] = revolutions_R_rpm & 0xFF;
  252.  
  253. Wire.write(myData, 4); //Sent 4 bytes to master
  254. }
  255.  
  256. void setup() {
  257. Serial.begin(9600);
  258. pinMode(encoder_in_L, INPUT);
  259. pinMode(encoder_in_R, INPUT);
  260. attachInterrupt(0, count_L, RISING); //attachInterrupt(digitalPinToInterrupt(encoder_in_L, count_L, RISING);
  261. attachInterrupt(1, count_R, RISING); //attachInterrupt(digitalPinToInterrupt(encoder_in_R, count_R, RISING);
  262. // Start the I2C Bus as Slave on address 9
  263. Wire.begin(9);
  264. // Attach a function to trigger when something is received.
  265. Wire.onRequest(requestEvent);
  266. }
  267.  
  268. void loop() {
  269. // stop Timer 0 interrupts from throwing the count out
  270. byte oldTCCR0A = TCCR0A;
  271. byte oldTCCR0B = TCCR0B;
  272. TCCR0A = 0; // stop timer 0
  273. TCCR0B = 0;
  274.  
  275. startCounting (1000); // how many ms to count for
  276.  
  277. while (!counterReady)
  278. { } // loop until count over
  279.  
  280. // Print out rpm
  281. Serial.print("Left motor rps: ");
  282. Serial.println(revolutions_L_rpm);
  283. Serial.print("Right motor rps: ");
  284. Serial.println(revolutions_R_rpm);
  285.  
  286. // Print out revolutions
  287. // Serial.print("Left motor revolution count: ");
  288. // Serial.println(revolutions_L);
  289. // Serial.print("Right motor revolution count: ");
  290. // Serial.println(revolutions_R);
  291.  
  292.  
  293. // restart timer 0
  294. TCCR0A = oldTCCR0A;
  295. TCCR0B = oldTCCR0B;
  296.  
  297. delay(200);
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement