6677

Untitled

Oct 22nd, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.75 KB | None | 0 0
  1. /**********************************************************************/
  2. /*** WMRVersionAlbertENG                                            ***/
  3. /*** Version: 1.0                                                   ***/
  4. /*** Date: 21-10-2013                                               ***/
  5. /***                                                                ***/
  6. /*** This is the program code for the Dutch LMR Robot.              ***/
  7. /*** Let's make Robots                                              ***/
  8. /***                                                                ***/
  9. /*** This version is based on the WMRVersionVonFlo.                 ***/
  10. /*** Flo > Profile: http://wirmachenroboter.com/user/19715          ***/
  11. /***                                                                ***/
  12. /*** Have fun!                                                      ***/
  13. /*** Albert.  http://letsmakerobots.com/user/21141                  ***/
  14. /**********************************************************************/
  15.  
  16. // Useful functions for controlling the servo comes from this:
  17. #include <Servo.h>
  18.  
  19. // Which pin defines which function:
  20. // Function       Pin     Description
  21. #define IR_SENSOR 2    // The Sharp IR Sensor
  22. #define PWM_A     3    // Drivespeed A
  23. #define SERVO     5    // Servo
  24. #define BRAKE_B   8    // Brake B
  25. #define BRAKE_A   9    // Brake A
  26. #define PWM_B     11   // Drivespeed B
  27. #define DIR_A     12   // Direction A
  28. #define DIR_B     13   // Direction B
  29.  
  30.  
  31. // Useful settings:
  32. #define FULL_SPEED        128             // Maximumspeed can't exeed 255. A higher value isn't possible.
  33. #define TURN_SPEED        255             // Quick turn.
  34. #define LEFT              LOW             // Turn left on LOW.
  35. #define RIGHT             HIGH            // Turn right on HIGH.
  36. #define CLOSEST_DISTANCE  220             // With this value for the sensor, the robot will stop. Watch out! Adjust this value to change when the robot stops.
  37. #define SERVO_RIGHT       55              // How far the servo turns to the right.
  38. #define SERVO_LEFT        125             // How far the servo turns to the left.
  39.  
  40. Servo SensorServo;  // This element controls the servo.
  41.  
  42. // For whitch servo position is the distance to the danger the smallest?
  43. int SeekingPositionWithClosestDanger()
  44. {
  45.   // Coming to a halt.
  46.   digitalWrite(BRAKE_A, HIGH);
  47.   digitalWrite(BRAKE_B, HIGH);
  48.   int ServoPosition;
  49.   int MinimumDistance = 0;
  50.   int MinimumServoPosition = 0;
  51.  
  52.   // From right (SERVO_RIGHT = 55)to left (SERVO_LEFT=125)checking all servo position. With Servoposition++ is the value increased with 1 every time.
  53.   for(ServoPosition = SERVO_RIGHT;ServoPosition <= SERVO_LEFT; ServoPosition++)
  54.   {
  55.     // Creating servo value.
  56.     SensorServo.write(ServoPosition);
  57.     delay(10);
  58.     // Is the actual value from the sensor smaller than the minimum vaulue?
  59.     if(analogRead( IR_SENSOR ) > MinimumDistance )
  60.     {
  61.       // Yes: actual value is the new minimum.
  62.       MinimumDistance = analogRead( IR_SENSOR );
  63.       // Secure servo position.
  64.       MinimumServoPosition = ServoPosition;
  65.     }  
  66.   }
  67.   // Adjust to the found position and give back the value.
  68.   SensorServo.write(MinimumServoPosition);
  69.   return MinimumServoPosition;
  70. }
  71.  
  72. byte ServoPosition = 90;
  73. boolean TurnServo = RIGHT;
  74. // Drive straight forward and simultaniously sweep the sensor.
  75. void DriveForward()
  76. {
  77.   SensorServo.write( ServoPosition );
  78.   // Adjust both motors to drive straight forward.
  79.   digitalWrite( DIR_A, HIGH );
  80.   digitalWrite( DIR_B, HIGH );
  81.  
  82.   // Use maximum speed.
  83.   analogWrite( PWM_A, FULL_SPEED );
  84.   analogWrite( PWM_B, FULL_SPEED );
  85.  
  86.   // Release the brakes.
  87.   digitalWrite( BRAKE_A, LOW );
  88.   digitalWrite( BRAKE_B, LOW );
  89.  
  90.   // Is the servo turning to the right?
  91.   if( TurnServo == LEFT )
  92.     ServoPosition = ServoPosition+1;  // Moving further to the right. Raise value by 1.
  93.   if( TurnServo == RIGHT )
  94.     ServoPosition = ServoPosition-1;  // Otherwise turn to the left. Reduce the value by 1.
  95.    
  96.   // Has the servo reached the maximum left?
  97.   if( ServoPosition > SERVO_LEFT )
  98.     TurnServo = RIGHT; // Turn to the right.
  99.   if( ServoPosition < SERVO_RIGHT )
  100.     TurnServo = LEFT;  // Otherwise turn to the left.
  101. }
  102.  
  103. // Turn, but where to? Left for counter clockwise otherwise right.
  104. void Turn( boolean Direction )
  105. {
  106.   // Brake
  107.   digitalWrite( BRAKE_A, HIGH );
  108.   digitalWrite( BRAKE_B, HIGH );
  109.   delay( 500 );
  110.  
  111.   digitalWrite( DIR_A, Direction );    // Turn motor A in that "direction".
  112.   digitalWrite( DIR_B, !Direction );   // Turn motor B in the opposite "direction".
  113.    
  114.   // Adjust speed to turn.
  115.   analogWrite( PWM_A, TURN_SPEED );
  116.   analogWrite( PWM_B, TURN_SPEED );
  117.  
  118.   // Release brakes.
  119.   digitalWrite( BRAKE_A, LOW );
  120.   digitalWrite( BRAKE_B, LOW );
  121.  
  122.   //Turn until the sensor measures 10% more distance.
  123.   while( ( CLOSEST_DISTANCE * 1 ) < analogRead( IR_SENSOR ) )
  124.   {
  125.     delay( 50 );
  126.   }
  127.   // Stop.
  128.   digitalWrite( BRAKE_A, HIGH );
  129.   digitalWrite( BRAKE_B, HIGH );
  130.   delay(1000);
  131. }
  132.  
  133. // Before everything starts, this function will be executed once.
  134. void setup( )
  135. {
  136.   //Motor A (right) initialise
  137.   pinMode( DIR_A, OUTPUT );    // Set the pin for the direction for motor A to an output.
  138.   pinMode( BRAKE_A, OUTPUT );  // Set the pin for the brake for motor A to an output.
  139.   pinMode( DIR_B, OUTPUT );    // Set the pin for the direction for motor B to an output.
  140.   pinMode( BRAKE_B, OUTPUT );  // Set the pin for the brake for motor A to an output.
  141.  
  142.   // Turn on both brakes. HIGH = braking.
  143.   digitalWrite( BRAKE_A, HIGH );
  144.   digitalWrite( BRAKE_B, HIGH );
  145.  
  146.   // Initialise servo and set to 90°.
  147.   SensorServo.attach( SERVO );
  148.   SensorServo.write( 90 );
  149.   delay( 500 );
  150.  
  151.   // Wait until an object apears in front of the sensor.
  152.   while( CLOSEST_DISTANCE  > analogRead( IR_SENSOR ) )
  153.   {
  154.     // This is the delay time.
  155.     delay( 100 );
  156.   }
  157.  
  158.   // Start!!
  159. }
  160.  
  161. // The actual program sequence. After the setup( ) has been run through the funcion loop( ) will be endless repeated.
  162. void loop( )
  163. {
  164.   int DangerPosition;
  165.   int Distance;
  166.  
  167.   Distance = analogRead( IR_SENSOR );    // Measure the distance from the sensor
  168.   if(Distance > CLOSEST_DISTANCE )       // Close enough? Is there an obstacle nearby, then a higher value will return otherwise a smaller value.
  169.   {
  170.     DangerPosition = SeekingPositionWithClosestDanger();  // Scan everything again to know precisely where the following danger is located.
  171.    
  172.     // Is the danger on the left?
  173.     if(DangerPosition <= 90)
  174.     {
  175.       Turn( RIGHT );  // Turn to the right.
  176.     }
  177.     // Is the danger on the right?
  178.     if( DangerPosition > 90 )
  179.     {
  180.       Turn( LEFT );   // Turn to the left.
  181.     }
  182.   }
  183.   DriveForward();  // Straight forward!!
  184.   delay( 10 );
  185. }
Advertisement
Add Comment
Please, Sign In to add comment