Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #include <SDPArduino.h>
  2. #include <Wire.h>
  3.  
  4. /*
  5. * Master board sample code to be used in conjuction with the rotary encoder
  6. * slave board and sample code.
  7. * This sketch will keep track of the rotary encoder positions relative to
  8. * the origin. The origin is set to the position held when the master board
  9. * is powered.
  10. *
  11. * Rotary encoder positions are printed to serial every 200ms where the
  12. * first result is that of the encoder attached to the port at 11 o'clock
  13. * on the slave board (with the I2C ports at at 12 o'clock). The following
  14. * results are in counter-clockwise sequence.
  15. *
  16. * Author: Chris Seaton, SDP Group 7 2015
  17. */
  18.  
  19.  
  20. #define ROTARY_SLAVE_ADDRESS 5
  21. #define ROTARY_COUNT 6
  22. #define PRINT_DELAY 50
  23.  
  24. #define MOTOR_1 1
  25. #define MOTOR_2 0
  26. #define MOTOR_3 3
  27. #define MOTOR_4 4
  28.  
  29. // Initial motor position is 0.
  30. int positions[ROTARY_COUNT] = {0};
  31. boolean flags[ROTARY_COUNT]={0};
  32.  
  33. float velocities[ROTARY_COUNT]={0};
  34. int times[ROTARY_COUNT]={0};
  35.  
  36. int previous[ROTARY_COUNT] = {0};
  37. int current[ROTARY_COUNT] = {0};
  38.  
  39. int sensor_fwd_reading = 0;
  40.  
  41. float PID_output = 0;
  42.  
  43. int ERROR_calib = 3500;
  44.  
  45. double Kp = 0.5;
  46. double Ki=0.5;
  47. double Kd = 0.05;
  48. double area=0;
  49.  
  50. double line_new_error = 0;
  51. double line_last_error = 0;
  52.  
  53. // GLOBAL VALUES for motors
  54. double motor2_pow = 0;
  55. double vel_delta_new = 0;
  56. double vel_delta_old = 0;
  57.  
  58. double vel_desired = 7;
  59.  
  60. void setup() {
  61. Serial.begin(9600); // Serial at given baudrate
  62. Serial.print("Seial here");
  63. Wire.begin(); // Master of the I2C bus
  64.  
  65. // motor_move(0, 100);
  66.  
  67. SDPsetup();
  68. helloWorld();
  69.  
  70. }
  71.  
  72. void update_motor2_PID() {
  73. // Update motor speeds
  74. update_velocities_pieris();
  75. double vel_current = velocities[MOTOR_2];
  76. // calcualte new power
  77. vel_delta_new = vel_desired - vel_current;
  78. area+=vel_delta_new;
  79. double vel_adjustment = Kp * vel_delta_new + Kd * (vel_delta_new - vel_delta_old) + Ki * (area);
  80. vel_delta_old = vel_delta_new;
  81. motor2_pow = vel_adjustment;
  82.  
  83. // DEBUG
  84.  
  85. Serial.print("vel_current = ");
  86. Serial.println(vel_current);
  87.  
  88. Serial.print("vel_desired - vel_current = ");
  89. Serial.println(vel_delta_new);
  90.  
  91. Serial.print("vel_adjustment = ");
  92. Serial.println(vel_adjustment);
  93.  
  94. Serial.println();
  95.  
  96. // make sure you don't go over 100
  97. if (motor2_pow > 100) {
  98. motor2_pow = 100;
  99. }
  100. else if (motor2_pow < -100) {
  101. motor2_pow = -100;
  102. }
  103. // motors_speed_fwd = abs(motors_speed_fwd);
  104. }
  105.  
  106. void motor_move(int id, double speed) {
  107. Serial.println("INSIDE motor_move");
  108. if (speed >= 0) {
  109. motorForward(id, speed);
  110. Serial.print("FWD: id ");
  111. Serial.println(id);
  112. Serial.print("FWD: speed ");
  113. Serial.println(speed);
  114. }
  115. else {
  116. motorBackward(id, abs(speed));
  117. Serial.print("BKW: id ");
  118. Serial.println(id);
  119. Serial.print("BKW: speed ");
  120. Serial.println(speed);
  121. }
  122. }
  123.  
  124.  
  125. /**
  126. * Get the angle positions in a small delay and calculates the --
  127. * --velocity of a wheel spinnig
  128. * Updates globals for velocity of all wheels
  129. */
  130. void update_velocities_pieris(){
  131. int delay_read=40;
  132. for(int i=0;i<ROTARY_COUNT;i++){
  133. velocities[i]=0.0f;
  134. }
  135. Wire.requestFrom(ROTARY_SLAVE_ADDRESS, ROTARY_COUNT);
  136. for (int i = 0; i < ROTARY_COUNT; i++) {
  137. positions[i] += (float) Wire.read(); // Must cast to signed 8-bit type
  138. previous[i]=positions[i];
  139. }
  140. delay(delay_read);
  141. Wire.requestFrom(ROTARY_SLAVE_ADDRESS, ROTARY_COUNT);
  142. for (int i = 0; i < ROTARY_COUNT; i++) {
  143. positions[i] += (float) Wire.read(); // Must cast to signed 8-bit type
  144. current[i]=positions[i];
  145. }
  146. for (int i = 0; i < ROTARY_COUNT; i++) {
  147. velocities[i]=((current[i]-previous[i])*(PI/180))/((float)delay_read/1000.0f);
  148. }
  149. }
  150.  
  151. void loop() {
  152.  
  153.  
  154. // sensor_fwd_reading += random(-500, 500);
  155.  
  156. update_motor2_PID();
  157. // double vel_to_pow = map(motor2_pow,-9.16, 9.16, -100, 100);
  158. motor_move(0, motor2_pow);
  159.  
  160.  
  161. Serial.print("motor2_pow = ");
  162. Serial.println(motor2_pow);
  163.  
  164. // Serial.print("vel_to_pow = ");
  165. // Serial.println(vel_to_pow);
  166.  
  167. delay(10);
  168. //update_velocities_pieris();
  169.  
  170. //printMotorVelocities();
  171.  
  172. Serial.println();
  173.  
  174. }
  175.  
  176.  
  177.  
  178. void updateMotorPositions() {
  179. // Request motor position deltas from rotary slave board
  180. Wire.requestFrom(ROTARY_SLAVE_ADDRESS, ROTARY_COUNT);
  181.  
  182. // Update the recorded motor positions
  183. for (int i = 0; i < ROTARY_COUNT; i++) {
  184. positions[i] = (int8_t) Wire.read(); // Must cast to signed 8-bit type
  185. }
  186. }
  187.  
  188.  
  189. void printMotorPositions() {
  190. Serial.print("Motor positions: ");
  191. for (int i = 0; i < ROTARY_COUNT; i++) {
  192. Serial.print(positions[i]);
  193. Serial.print(' ');
  194. }
  195. Serial.println();
  196. delay(50); // Delay to avoid flooding serial out
  197. }
  198.  
  199. void printMotorVelocities() {
  200. Serial.print("Motor velocities: ");
  201. for (int i = 0; i < ROTARY_COUNT; i++) {
  202. Serial.print(velocities[i]);
  203. Serial.print(' ');
  204. }
  205. Serial.println();
  206. delay(PRINT_DELAY); // Delay to avoid flooding serial out
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement