Advertisement
Guest User

carcode

a guest
Apr 20th, 2018
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include <Stepper.h>
  2. #include <SoftwareSerial.h>
  3. #include <RoboClaw.h>
  4.  
  5.  
  6. //See limitations of Arduino SoftwareSerial
  7. SoftwareSerial serial(10,11);
  8. RoboClaw roboclaw(&serial,10000);
  9.  
  10. #define address 0x80
  11.  
  12. float goalSpeed = -1;
  13. float currentSpeed = -1;
  14. float goalDegree = 0;
  15. double currentDegree = 0;
  16. int currentStepSteer = 0;
  17. int currentStepAccel = 0;
  18. int goalStepSteer = 0;
  19. const int stp1 = 13; // sets step and direction pins for drivers stp1 = x and stp2 = y
  20. const int dir1 = 12;
  21. const int steerStp = 6;
  22. const int steerDir = 7;
  23. const float tol = 1;
  24.  
  25. void setup() {
  26. //Open roboclaw serial ports
  27. roboclaw.begin(38400);
  28. Serial.begin( 9600 );
  29. // Sets the pins as Outputs
  30. pinMode(stp1,OUTPUT);
  31. pinMode(dir1,OUTPUT);
  32. pinMode( steerStp, OUTPUT );
  33. pinMode( steerDir, OUTPUT );
  34. //resetActuator();
  35. }
  36.  
  37. void loop() {
  38.  
  39. readIncoming();
  40. if ( goalSpeed == 0 ) {
  41. goHome();
  42. brake();
  43. } else if ( currentSpeed < goalSpeed && currentStepAccel <= 28 ) {
  44. acellerateOneStep();
  45. currentStepAccel++;
  46. } else if ( currentSpeed > goalSpeed && currentStepAccel >= 0 ) {
  47. decelerateOneStep();
  48. currentStepAccel--;
  49. }
  50. if ( goalDegree > currentDegree && currentStepSteer <= 169200 && currentStepSteer >= -169200 ) {
  51. steerRight();
  52. } else if ( goalDegree < currentDegree && currentStepSteer <= 169200 && currentStepSteer >= -169200 ) {
  53. steerLeft();
  54. }
  55. currentDegree = convertToDegree( currentStepSteer );
  56.  
  57. }
  58.  
  59. /**
  60. * First number is goal, second number is actual.
  61. */
  62. void readIncoming() {
  63. if ( Serial.available() > 0 ) {
  64. if ( Serial.find( ":" ) ) {
  65. goalSpeed = Serial.parseFloat();
  66. currentSpeed = Serial.parseFloat();
  67. goalDegree = Serial.parseFloat();
  68.  
  69. Serial.write( Serial.print(goalSpeed) );
  70. Serial.write( " " );
  71. Serial.write( Serial.print(currentSpeed) );
  72. Serial.write( " " );
  73. Serial.write( Serial.print( goalDegree ) );
  74. Serial.write( " " );
  75. Serial.write( Serial.print( currentDegree, 6 ) );
  76. Serial.write( "\n");
  77. }
  78.  
  79. }
  80. }
  81.  
  82. void resetActuator() {
  83. roboclaw.BackwardM2( address, 120 );
  84. delay(5000);
  85. }
  86.  
  87. void steerRight() {
  88.  
  89. digitalWrite( steerDir, HIGH );
  90. delayMicroseconds( 500 );
  91. for ( int n = 0; n < 175 && goalDegree > currentDegree; n++ ) {
  92. digitalWrite( steerStp, HIGH );
  93. delayMicroseconds( 100 );
  94. digitalWrite( steerStp, LOW );
  95. delayMicroseconds( 100 );
  96. currentStepSteer++;
  97. currentDegree = convertToDegree( currentStepSteer );
  98. }
  99.  
  100. }
  101.  
  102. void steerLeft() {
  103. digitalWrite( steerDir, LOW );
  104. delayMicroseconds( 500 );
  105. for ( int n = 0; n < 175 && goalDegree < currentDegree; n++ ) {
  106. digitalWrite( steerStp, HIGH );
  107. delayMicroseconds( 100 );
  108. digitalWrite( steerStp, LOW );
  109. delayMicroseconds( 100 );
  110. currentStepSteer--;
  111. currentDegree = convertToDegree( currentStepSteer );
  112. }
  113. }
  114.  
  115. void acellerateOneStep() {
  116.  
  117. digitalWrite( dir1, HIGH );
  118. delayMicroseconds( 50 );
  119. digitalWrite( stp1, HIGH );
  120. delayMicroseconds( 50 );
  121. digitalWrite( stp1, LOW );
  122. delay( 2 );
  123.  
  124. }
  125.  
  126. void decelerateOneStep() {
  127.  
  128. digitalWrite( dir1, LOW );
  129. delayMicroseconds( 50 );
  130. digitalWrite( stp1, HIGH );
  131. delayMicroseconds( 50 );
  132. digitalWrite( stp1, LOW );
  133. delay( 2 );
  134.  
  135. }
  136.  
  137.  
  138. void brake() {
  139. roboclaw.ForwardM2( address, 127 );
  140. delay(1500);
  141. roboclaw.BackwardM2( address, 127 );
  142. delay(1000);
  143. while ( goalSpeed == 0 ) {
  144. roboclaw.ForwardM2( address, 0 );
  145. readIncoming();
  146. }
  147. roboclaw.BackwardM2( address, 127 );
  148. delay(500);
  149. roboclaw.ForwardM2( address, 0 );
  150. }
  151.  
  152. void goHome() {
  153. digitalWrite( dir1, LOW);
  154. for ( int i = currentStepAccel; i >= 0; i-- ) {
  155. digitalWrite( stp1, HIGH);
  156. delayMicroseconds(50);
  157. digitalWrite( stp1, LOW );
  158. delay( 2 );
  159. currentStepAccel--;
  160. //Serial.write( "\n" );
  161. }
  162. }
  163.  
  164. double convertToDegree( int step1 ) {
  165. return (1.0/209.0) * step1;
  166. }
  167.  
  168. int convertToStep( float degree ){
  169. return (int) degree / 4.4;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement