Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. /*
  2. Controlling a servo position using a potentiometer (variable resistor)
  3. by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
  4. modified on 8 Nov 2013
  5. by Scott Fitzgerald
  6. http://www.arduino.cc/en/Tutorial/Knob
  7. */
  8.  
  9. #include <Servo.h>
  10. #include <NewPing.h>
  11.  
  12. Servo servoturn; // create servo object to control turning servo
  13. Servo servoaccel; // create servo object to control acceleration servo
  14.  
  15. const short int startButtonPin = 2; // the number of the pushbutton pin
  16. const short int brakeSpeed = 80; // value for braking
  17. const short int parkSpeed = 93; // value for parking
  18. const short int minSpeed = 95; // value for slow acceleration
  19. const short int maxSpeed = 100; // max speed value
  20. const short int balance = 2; // angle shift (neutral angle balance)
  21. const short int maxAnglePos = 33; // max positive angle value, left turn
  22. const short int maxAngleNeg = -35; // max negative angle value, right turn
  23. const short int startDelay = 3000; // start delay (ms)
  24. const short int periodTime = 14; // servo controlling period time
  25. const short int maxRatio = 1000; // max pulse ratio
  26.  
  27. short int exangle = 90; // current state of tire angle
  28. short int startButtonState = 0; // variable for reading the pushbutton status
  29.  
  30. // Sonar setup
  31.  
  32. #define TRIGGER_PIN 11 // Arduino pin tied to trigger pin on the ultrasonic sensor.
  33. #define ECHO_PIN1 12 // Arduino pin tied to echo pin on the ultrasonic sensor..
  34. #define ECHO_PIN2 13 // Arduino pin tied to echo pin on the ultrasonic sensor.
  35. #define ECHO_PIN3 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
  36. #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
  37.  
  38. NewPing sonar1(TRIGGER_PIN, ECHO_PIN1, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  39. NewPing sonar2(TRIGGER_PIN, ECHO_PIN2, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  40. NewPing sonar3(TRIGGER_PIN, ECHO_PIN3, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
  41.  
  42. void setup() {
  43. Serial.begin(115200);
  44. servoturn.attach(9); // attaches the turning servo on pin to the servo object
  45. servoaccel.attach(10); // attaches the acceleration servo on pin to the servo object
  46. pinMode(startButtonPin, INPUT); // initialize the pushbutton pin as an input
  47. }
  48.  
  49. void turn(short int angle, short int turnspeed){
  50.  
  51. // checks that turning angles are within safe limits
  52. if (angle > maxAnglePos && angle > 0){
  53. angle = maxAnglePos;
  54. }
  55. else if (angle < maxAngleNeg && angle < 0){
  56. angle = maxAngleNeg;
  57. }
  58.  
  59. angle = angle + balance + 90; // scale
  60.  
  61. // sets turning angle
  62. if (angle == 90 + balance){
  63. servoturn.write(angle);
  64. }
  65. else if (angle == exangle){
  66. }
  67. else if (angle < exangle){
  68. for(int i=exangle; i > angle; i--){
  69. servoturn.write(i);
  70. delay(turnspeed);
  71. }
  72. }
  73. else {
  74. for(int i=exangle; i < angle; i++){
  75. servoturn.write(i);
  76. delay(turnspeed);
  77. }
  78. }
  79. exangle = angle;
  80. }
  81.  
  82. void turnAngled(short int tireAngle, short int targetAngle) // Turn function that uses compass to turn desired angle (real angle).
  83. {
  84. int lastAngle = exangle; // Save tire angle to turn afterwards
  85. int pingDelay = 100; // Delay for reading compass module
  86. int realAngle = 0; // 0 is placeholder for compass read!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  87. int startingAngle = realAngle; // save starting angle (real compass angle) to memory
  88. boolean overflowPos = false; // Overflow if targetAngle > 359
  89. boolean overflowNeg = false; // Overflow if targetAngle < 0
  90.  
  91. // Set targetAngle to "real" angle
  92. targetAngle = realAngle + targetAngle;
  93.  
  94. if (targetAngle < 0) // Check if there is negative overflow in turning angle (example. targetAngle is -30 and real angle is 20 wich leads to targetAngle being -10, this changes it to 269)
  95. {
  96. targetAngle = 359 + targetAngle;
  97. overflowNeg = true;
  98. }
  99. else if (targetAngle > 359) // Check if there is positive overflow in turning angle
  100. {
  101. targetAngle = targetAngle - 359;
  102. overflowPos = true;
  103. }
  104.  
  105. if (realAngle != targetAngle) // Check if angle is not already magically right
  106. {
  107. // Turn tires to wanted turning angle angle
  108. turn(tireAngle, 5);
  109.  
  110. if (realAngle > targetAngle || overflowNeg==true) // Check turning direction
  111. {
  112. while (realAngle > targetAngle || overflowNeg==true && realAngle < startingAngle) // delay loop wich checks if car is turning in allowed angles
  113. {
  114. delay(pingDelay);
  115. realAngle = 0; //0 is placeholder for compass read!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  116. }
  117. }
  118. else
  119. {
  120. while (realAngle < targetAngle || overflowPos==true && realAngle > startingAngle) // same as above but to other directon
  121. {
  122. delay(pingDelay);
  123. realAngle = 0; //0 is placeholder for compass read!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  124. }
  125. }
  126. turn(lastAngle, 5); // turn tires back to las angle
  127. }
  128. }
  129.  
  130.  
  131. void accelFast(short int accelSpeed, unsigned short int accelTime){
  132.  
  133. if (accelSpeed <= maxSpeed && accelSpeed >= minSpeed){
  134. servoaccel.write(accelSpeed);
  135. delay(accelTime);
  136. }
  137. else if (accelSpeed < minSpeed && accelSpeed > 0){
  138. servoaccel.write(accelSpeed);
  139. delay(accelTime);
  140. }
  141. else if (accelSpeed > maxSpeed){
  142. servoaccel.write(maxSpeed);
  143. delay(accelTime);
  144. }
  145. else {
  146. servoaccel.write(parkSpeed);
  147. delay(accelTime);
  148. }
  149. }
  150.  
  151. /*
  152. void accelSlow(short int accelSpeed, short int accelTime, short int pulseRatio){
  153. if (pulseRatio < 2){
  154. pulseRatio = 2;
  155. }
  156. else if (pulseRatio > maxRatio){
  157. pulseRatio = maxRatio;
  158. }
  159.  
  160. if (accelSpeed <= maxSpeed && accelSpeed >= minSpeed){
  161. for (;accelTime > 0; accelTime = accelTime - (periodTime * pulseRatio) ){
  162. servoaccel.write(accelSpeed);
  163. delay(periodTime * (pulseRatio-1) );
  164. servoaccel.write(parkSpeed);
  165. delay(periodTime);
  166. }
  167. }
  168. else if (accelSpeed < minSpeed && accelSpeed > 0){
  169. servoaccel.write(accelSpeed);
  170. delay(accelTime);
  171. }
  172. else if (accelSpeed > maxSpeed){
  173. servoaccel.write(maxSpeed);
  174. delay(accelTime);
  175. }
  176. else {
  177. servoaccel.write(parkSpeed);
  178. delay(accelTime);
  179. }
  180. }
  181. */
  182.  
  183. void start(){
  184. turn(1,5);
  185. int rightSonar;
  186. int leftSonar;
  187. int frontSonar;
  188. int brake = 0;
  189. Serial.println("Start");
  190. servoaccel.write(84);
  191. while(1){
  192. delay(32);
  193. rightSonar=sonar1.ping_cm();
  194. delay(32);
  195. leftSonar=sonar2.ping_cm();
  196. delay(32);
  197. frontSonar=sonar3.ping_cm();
  198. if (rightSonar==0){
  199. rightSonar=400;
  200. }
  201. if (leftSonar==0){
  202. leftSonar=400;
  203. }
  204. if (frontSonar==0){
  205. frontSonar=400;
  206. }
  207. Serial.print(leftSonar);
  208. Serial.print(" <-vasen eteen = ");
  209. Serial.print(frontSonar);
  210. Serial.print(" oikea-> ");
  211. Serial.println(rightSonar);
  212. if (frontSonar < 300) {
  213. turn(25,5);
  214. delay(300);
  215. turn(0,5);
  216. delay(800);
  217. turn(-25,5);
  218. delay(300);
  219. turn(0,5);
  220. delay(400);
  221. turn(-25,5);
  222. delay(120);
  223. turn(0,5);
  224. delay(700);
  225. turn(25,5);
  226. delay(120);
  227. turn(0,5);
  228. delay(50);
  229. servoaccel.write(95);
  230. servoaccel.write(90);
  231. delay(70000);
  232. }
  233. }
  234. }
  235.  
  236.  
  237. void track(bool allow){
  238.  
  239. }
  240.  
  241.  
  242.  
  243. void loop() {
  244.  
  245.  
  246. start(); // main moving function
  247.  
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement