Library for the Shield Bot by SeeedStudio

Dependents:   Seeed_Bot_Shield Seeed_BlueBot_demo Seeed_BlueBot_demo_test1 LAB03_Oppgav1 ... more

Fork of SeeedShieldBot by Components

Files at this revision

API Documentation at this revision

Comitter:
screamer
Date:
Thu Jul 31 14:29:50 2014 +0000
Parent:
6:9479362be27f
Child:
8:9fd1722259a1
Commit message:
Changed constructor to accept pin parameters (instead of static ones); Added turn_left/right() and stopLeft/Right();; Fixed documentation

Changed in this revision

SeeedStudioShieldBot.cpp Show annotated file Show diff for this revision Revisions of this file
SeeedStudioShieldBot.h Show annotated file Show diff for this revision Revisions of this file
--- a/SeeedStudioShieldBot.cpp	Mon Jul 15 14:03:41 2013 +0000
+++ b/SeeedStudioShieldBot.cpp	Thu Jul 31 14:29:50 2014 +0000
@@ -2,39 +2,37 @@
 #include "mbed.h"
 
 
-SeeedStudioShieldBot::SeeedStudioShieldBot() :  rightSensor(PTB0),
-    inRightSensor(PTB1),
-    centreSensor(PTB2),
-    inLeftSensor(PTB3),
-    leftSensor(PTA4),
-    motor1A(PTA5),
-    motor1B(PTC9),
-    motor1En(PTC8),
-    motor2A(PTA12),
-    motor2B(PTD0),
-    motor2En(PTD5) {
+SeeedStudioShieldBot::SeeedStudioShieldBot(
+    PinName mot1A, PinName mot1En, PinName mot1B,
+    PinName mot2A, PinName mot2En, PinName mot2B,
+    PinName sensor_right, PinName sensor_inright, PinName sensor_center, PinName sensor_inleft, PinName sensor_left
+) :
+    motor1A(mot1A), motor1En(mot1En), motor1B(mot1B),
+    motor2A(mot2A), motor2En(mot2En), motor2B(mot2B),
+    rightSensor(sensor_right), inRightSensor(sensor_inright), centreSensor(sensor_center), inLeftSensor(sensor_inleft), leftSensor(sensor_left)
+{
     // Something should go here...
 }
 
-void SeeedStudioShieldBot::right_motor(float speed) { 
+void SeeedStudioShieldBot::right_motor(float speed)
+{
     // The bit that's actually needed...
     if (speed >= 0) {
         motor1A = speed;
         motor1B = 0;
-    }
-    else {
+    } else {
         motor1A = 1 + speed;
         motor1B = 1;
     }
 }
 
-void SeeedStudioShieldBot::left_motor(float speed) {   
+void SeeedStudioShieldBot::left_motor(float speed)
+{
     // Useful bit
     if (speed >= 0) {
         motor2A = speed;
         motor2B = 0;
-    }
-    else {
+    } else {
         motor2A = 1 + speed;
         motor2B = 1;
     }
@@ -42,65 +40,84 @@
 
 // The following two functions turn the robot on the spot...
 
-void SeeedStudioShieldBot::left(float speed) {
+void SeeedStudioShieldBot::left(float speed)
+{
     left_motor(-speed);
     right_motor(speed);
 }
 
-void SeeedStudioShieldBot::right(float speed) {
+void SeeedStudioShieldBot::right(float speed)
+{
     left_motor(speed);
     right_motor(-speed);
 }
 
+void SeeedStudioShieldBot::turn_left(float speed)
+{
+    left_motor(speed);
+    right_motor(0);
+}
+
+void SeeedStudioShieldBot::turn_right(float speed)
+{
+    left_motor(0);
+    right_motor(speed);
+}
+
 // Again, until we get PWM on the pin w/o PWM, there's no way we can really have this any other way than 1...
 // This is the case for both forwards and backwards...
 
 // FIXED, using jumper between pins 8 and 3 on top of the robot board... unfortunately it wastes an io pin, but is easier than using software IO.
 // It also means that the 'lid' can't go on top.
 
-void SeeedStudioShieldBot::forward(float speed) {
+void SeeedStudioShieldBot::forward(float speed)
+{
     if (speed == 0) {
         left_motor(0);
         right_motor(0);
-    }
-    else {
+    } else {
         left_motor(speed);
         right_motor(speed);
     }
 }
 
-void SeeedStudioShieldBot::backward(float speed) {
+void SeeedStudioShieldBot::backward(float speed)
+{
     if (speed == 0) {
         left_motor(0);
         right_motor(0);
-    }
-    else {
+    } else {
         left_motor(-speed);
         right_motor(-speed);
     }
 }
 
-void SeeedStudioShieldBot::enable_right_motor() {
+void SeeedStudioShieldBot::enable_right_motor()
+{
     motor1En = 1;
 }
 
-void SeeedStudioShieldBot::enable_left_motor() {
+void SeeedStudioShieldBot::enable_left_motor()
+{
     motor2En = 1;
 }
 
-void SeeedStudioShieldBot::disable_right_motor() {
+void SeeedStudioShieldBot::disable_right_motor()
+{
     motor1En = 0;
 }
 
-void SeeedStudioShieldBot::disable_left_motor() {
+void SeeedStudioShieldBot::disable_left_motor()
+{
     motor2En = 0;
 }
 
 // Give a value representative of the overall data received by the sensors...
 // Output between +1 and -1.
 // Positive is right, negative left...
-float SeeedStudioShieldBot::line_position() {
-    float output = 0; 
+float SeeedStudioShieldBot::line_position()
+{
+    float output = 0;
     if (rightSensor == 1) {
         output += 0.5;
     }
@@ -113,25 +130,33 @@
     if (inLeftSensor == 1) {
         output -= 0.5;
     }
-    
+
     return output;
 }
 
-void SeeedStudioShieldBot::stopAll() {
+void SeeedStudioShieldBot::stop(int motor)
+{
+    if (motor == 1) {
+        stopLeft();
+    } else if (motor == 2) {
+        stopRight();
+    }
+}
+
+void SeeedStudioShieldBot::stopLeft()
+{
     motor1A = 0;
     motor1B = 0;
+}
+
+void SeeedStudioShieldBot::stopRight()
+{
     motor2A = 0;
     motor2B = 0;
 }
 
-void SeeedStudioShieldBot::stop(int motor) {
-    if (motor == 1) {
-        // Stop motor 0...
-        motor1A = 0;
-        motor1B = 0;
-    }
-    else if (motor == 2) {
-        motor2A = 0;
-        motor2B = 0;
-    }
-}
\ No newline at end of file
+void SeeedStudioShieldBot::stopAll()
+{
+    stopLeft();
+    stopRight();
+}
--- a/SeeedStudioShieldBot.h	Mon Jul 15 14:03:41 2013 +0000
+++ b/SeeedStudioShieldBot.h	Thu Jul 31 14:29:50 2014 +0000
@@ -8,27 +8,40 @@
 class SeeedStudioShieldBot {
     public:
     
-        // Create a shield bot object, using default settings for now...
-        SeeedStudioShieldBot();
-        
+        /** Create a shield bot object
+         *
+         * @param mot1A Left Motor A pin, default D5
+         * @param mot1B Left Motor B pin, default D6
+         * @param mot1En Left Motor enable pin, default D7
+         * @param mot2A Right Motor A pin, default D3
+         * @param mot2B Right Motor B pin, default D9
+         * @param mot2En Right Motor enable pin, default D10
+         * @param sensor_right Sensor right pin, default A0
+         * @param sensor_inright Sensor in-right pin, default A1
+         * @param sensor_center Sensor right pin, default A2
+         * @param sensor_inleft Sensor in-left pin, default A3
+         * @param sensor_left Sensor left pin, default D4
+         */
+        SeeedStudioShieldBot(PinName mot1A, PinName mot1En, PinName mot1B, PinName mot2A, PinName mot2En, PinName mot2B, PinName sensor_right, PinName sensor_inright, PinName sensor_center, PinName sensor_inleft, PinName sensor_left);
+
         /** Switch on the left motor at the given speed.
          *  @param speed The speed, from 0.0 to 1.0 at which to spin the motor.
-        */
+         */
         void left_motor(float speed);
         
         /** Switch on the right motor at the given speed.
          *  @param speed The speed, from 0.0 to 1.0 at which to spin the motor.
-        */
+         */
         void right_motor(float speed);
         
         /** Switch on both motors, forwards at the given speed.
          *  @param speed The speed, from 0.0 to 1.0 at which to spin the motor.
-        */
+         */
         void forward(float speed);
         
         /** Switch on both motors, backwards at the given speed.
          *  @param speed The speed, from 0.0 to 1.0 at which to spin the motor.
-        */
+         */
         void backward(float speed);
         
         /** Switch on both motors at the given speed, in opposite directions so as to turn left.
@@ -38,38 +51,56 @@
         
         /** Switch on both motors at the given speed, in opposite directions so as to turn right.
          *  @param speed The speed, from 0.0 to 1.0 at which to spin the motors.
-        */
+         */
         void right(float speed);
-
+        
+        /** Turns left.
+         *  @param speed The speed, from 0.0 to 1.0 at which to spin the motor.
+         */
+        void turn_left(float speed);
+        
+        /** Turns right.
+         *  @param speed The speed, from 0.0 to 1.0 at which to spin the motor.
+         */
+        void turn_right(float speed);
+        
         /** Disable the left motor, by driving enable pin for the second motor low...
-        */
+         */
         void disable_left_motor();
         
         /** Disable the left motor, by driving enable pin for the first motor low...
-        */
+         */
         void disable_right_motor();
         
         /** Enable the left motor, by driving enable pin for the second motor high...
-        */
+         */
         void enable_left_motor();
         
         /** Enable the left motor, by driving enable pin for the first motor high...
-        */
+         */
         void enable_right_motor();
         
-        /** Stop both motors at the same time. Different to disable.
-        */
-        void stopAll();
-        
         /** Stop a chosen motor.
          *  @param motor Number, either 1 or 2 choosing the motor.
-        */
+         */
         void stop(int motor);
         
+        /** Stop left motor.
+         */
+        void stopLeft();
+        
+        /** Stop right motor.
+         */
+        void stopRight();
+        
+        /** Stop both motors at the same time. Different to disable.
+         */
+        void stopAll();
+        
         // Need to do something to do with detected line...
         
         /** Gives an indication of the data given by the reflectivity sensors.
-        */
+         */
         float line_position();
         
         DigitalIn rightSensor;
@@ -83,7 +114,6 @@
         DigitalOut motor1B;
         DigitalOut motor1En;
         
-        // motor2A or motor2B need to be PWM, but the freedom board doesn't support it at the moment...
         PwmOut motor2A;
         DigitalOut motor2B;
         DigitalOut motor2En;