My Version of CreaBotLib
Fork of CreaBotLib by
CreaBot.h@10:79509113310a, 2019-04-17 (annotated)
- Committer:
- sepp_nepp
- Date:
- Wed Apr 17 21:48:00 2019 +0000
- Revision:
- 10:79509113310a
- Parent:
- 9:efe9f76d6f76
- Child:
- 11:5a94af0afa12
Refactor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sepp_nepp | 7:3a793ddc3490 | 1 | /* |
sepp_nepp | 8:3b5b0a82b429 | 2 | * \file CreaBot.h |
sepp_nepp | 8:3b5b0a82b429 | 3 | * \brief File contains Creabot Library. |
sepp_nepp | 6:4d8938b686a6 | 4 | |
sepp_nepp | 9:efe9f76d6f76 | 5 | * CreaBot.h contains the Creabot class, and required enums and structs. |
sepp_nepp | 10:79509113310a | 6 | * Imports "mbed.h" and "motor.h" |
sepp_nepp | 6:4d8938b686a6 | 7 | |
sepp_nepp | 6:4d8938b686a6 | 8 | * Refactorings: |
sepp_nepp | 9:efe9f76d6f76 | 9 | * All variables with suffixes "_cm_sec" = speeds in centimeters per second. |
sepp_nepp | 9:efe9f76d6f76 | 10 | * MotCommand -> BotCommand. |
sepp_nepp | 9:efe9f76d6f76 | 11 | * cmdbot_t -> BotCmdVerb. |
sepp_nepp | 9:efe9f76d6f76 | 12 | * cm -> dist_cm. |
sepp_nepp | 9:efe9f76d6f76 | 13 | * 'motorxx' -> 'wheelxxx': each motor now becomes a 'wheel with a diameter' |
sepp_nepp | 9:efe9f76d6f76 | 14 | * FIFO queue management merged with Creabot Class, now called Queue |
sepp_nepp | 9:efe9f76d6f76 | 15 | * Queue does not use another extra ticker anymore. Instead triggers from Motor End commands. |
sepp_nepp | 10:79509113310a | 16 | * Queue is worked through, and Callback is not called, until queue is empty. |
sepp_nepp | 9:efe9f76d6f76 | 17 | * |
sepp_nepp | 10:79509113310a | 18 | * @author Tarek Lule based on work of Francois Druilhe, et al. |
sepp_nepp | 10:79509113310a | 19 | * @date 21. April 2019 |
sepp_nepp | 8:3b5b0a82b429 | 20 | * @see https://os.mbed.com/users/sepp_nepp/code/CreaBotLib/ */ |
sepp_nepp | 6:4d8938b686a6 | 21 | |
sepp_nepp | 9:efe9f76d6f76 | 22 | // -------------------- wheel --------------------------- |
sepp_nepp | 6:4d8938b686a6 | 23 | |
garphil | 0:a7fb03c9ea9d | 24 | #ifndef CREABOT_H |
garphil | 0:a7fb03c9ea9d | 25 | #define CREABOT_H |
garphil | 0:a7fb03c9ea9d | 26 | |
garphil | 0:a7fb03c9ea9d | 27 | #include "mbed.h" |
garphil | 0:a7fb03c9ea9d | 28 | #include "motor.h" |
garphil | 0:a7fb03c9ea9d | 29 | |
sepp_nepp | 8:3b5b0a82b429 | 30 | #define DEFAULT_SPEED_CM_SEC 2.0f /**< Default advancement speed = 2.0cm/sec, was DEFAULT_SPEED */ |
sepp_nepp | 9:efe9f76d6f76 | 31 | #define DEPTH_Queue 256 /**< Initialize the depth of the command Queue to 256 */ |
garphil | 0:a7fb03c9ea9d | 32 | |
sepp_nepp | 8:3b5b0a82b429 | 33 | /** \enum BotCmdVerb |
sepp_nepp | 8:3b5b0a82b429 | 34 | * \brief Robot Commands Verbs, gives the movement direction |
sepp_nepp | 10:79509113310a | 35 | * IDLE is no longer supported |
sepp_nepp | 9:efe9f76d6f76 | 36 | */ |
sepp_nepp | 6:4d8938b686a6 | 37 | typedef enum { |
sepp_nepp | 9:efe9f76d6f76 | 38 | FORWARD, /**< Advance the robot straight forward */ |
sepp_nepp | 9:efe9f76d6f76 | 39 | BACKWARD, /**< Reverse the robot straight backwards */ |
sepp_nepp | 9:efe9f76d6f76 | 40 | ROTATE, /**< Rotate around its own axis*/ |
sepp_nepp | 9:efe9f76d6f76 | 41 | LEFT, /**< Advance in a left curve */ |
sepp_nepp | 9:efe9f76d6f76 | 42 | RIGHT, /**< Advance in a right curve */ |
sepp_nepp | 9:efe9f76d6f76 | 43 | BACKLEFT, /**< Reverse in a left curve */ |
sepp_nepp | 9:efe9f76d6f76 | 44 | BACKRIGHT /**< Reverse in a right curve */ |
sepp_nepp | 6:4d8938b686a6 | 45 | } BotCmdVerb; |
sepp_nepp | 6:4d8938b686a6 | 46 | |
sepp_nepp | 5:efe80c5db389 | 47 | |
sepp_nepp | 9:efe9f76d6f76 | 48 | /** \enum TWheelsState |
sepp_nepp | 10:79509113310a | 49 | * \brief Possible states of the two wheels of the CreaBot |
sepp_nepp | 10:79509113310a | 50 | * Can be ored together */ |
sepp_nepp | 6:4d8938b686a6 | 51 | typedef enum { |
sepp_nepp | 9:efe9f76d6f76 | 52 | LRWHEELS_STOP = 0, /**< All wheels have stopped */ |
sepp_nepp | 9:efe9f76d6f76 | 53 | LWHEEL_RUNS = 1, /**< Left wheel runs */ |
sepp_nepp | 10:79509113310a | 54 | RWHEEL_RUNS = 2, /**< Right wheel runs */ |
sepp_nepp | 9:efe9f76d6f76 | 55 | LRWHEELS_RUN = 3, /**< Both wheels run */ |
sepp_nepp | 9:efe9f76d6f76 | 56 | } TWheelsState; |
sepp_nepp | 6:4d8938b686a6 | 57 | |
sepp_nepp | 8:3b5b0a82b429 | 58 | /** \struct BotCommand |
sepp_nepp | 10:79509113310a | 59 | * \brief Structure of a CreaBot Command. |
sepp_nepp | 9:efe9f76d6f76 | 60 | * The command structure is put into command Queue, and treated by the Queue-Handler */ |
sepp_nepp | 6:4d8938b686a6 | 61 | typedef struct { |
sepp_nepp | 10:79509113310a | 62 | BotCmdVerb command; /**< Command type that gives movement direction.*/ |
sepp_nepp | 8:3b5b0a82b429 | 63 | float dist_cm; /**< Distance in dist_cm for translational movements .*/ |
sepp_nepp | 9:efe9f76d6f76 | 64 | float turn_angle_deg; /**< Angle in degree for rotational movement .*/ |
sepp_nepp | 9:efe9f76d6f76 | 65 | void set(BotCmdVerb Acommand, float Aturn_angle_deg, float Adist_cm); /**< Helper; set structure fields to values */ |
sepp_nepp | 8:3b5b0a82b429 | 66 | void set(BotCmdVerb Acommand, float Aparam); /**< Helper; set structure fields to values */ |
sepp_nepp | 6:4d8938b686a6 | 67 | } BotCommand; |
sepp_nepp | 6:4d8938b686a6 | 68 | |
garphil | 0:a7fb03c9ea9d | 69 | |
sepp_nepp | 8:3b5b0a82b429 | 70 | /** \class Creabot |
garphil | 0:a7fb03c9ea9d | 71 | |
sepp_nepp | 9:efe9f76d6f76 | 72 | * \brief Synchronous Control of 2 wheels as part of a two wheel robot |
sepp_nepp | 9:efe9f76d6f76 | 73 | * |
sepp_nepp | 10:79509113310a | 74 | * Handles two instances of CreaMot from motor.h library simultaneously. |
sepp_nepp | 9:efe9f76d6f76 | 75 | * Using the set distance between the wheels, allows to run pecise curves. |
sepp_nepp | 10:79509113310a | 76 | * A first other set of movement functions starting with qXXX are using command verbs, |
sepp_nepp | 10:79509113310a | 77 | * these commands are queued up in a waiting queue, |
sepp_nepp | 10:79509113310a | 78 | * and are executed one by one in programmed order. |
sepp_nepp | 10:79509113310a | 79 | * A second set of movement functions starting with iXXX are also using command verbs, |
sepp_nepp | 10:79509113310a | 80 | * however these commands are executed immediately, |
sepp_nepp | 10:79509113310a | 81 | * they override each other if issued while the previous movement still ongoing |
sepp_nepp | 10:79509113310a | 82 | * A third set of movements functions starting with moveXXXX are provided, |
sepp_nepp | 10:79509113310a | 83 | * each function performs one specific movement, also immediately. |
sepp_nepp | 10:79509113310a | 84 | * So they also override each other, and collide with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 85 | * A callback is supplied to react to the end of all programmed movements. |
sepp_nepp | 9:efe9f76d6f76 | 86 | * |
sepp_nepp | 9:efe9f76d6f76 | 87 | * Example: |
sepp_nepp | 9:efe9f76d6f76 | 88 | * @code |
sepp_nepp | 9:efe9f76d6f76 | 89 | * // --- Define the Four PINs & Time of movement used for wheel drive ----- |
sepp_nepp | 10:79509113310a | 90 | * CreaMot wheelLeft(PA_12, PB_0, PB_1, PB_6); // Declare first the 2 wheels (to avoid to have an object with 8 pins to create) |
sepp_nepp | 10:79509113310a | 91 | * CreaMot wheelRight(PA_5,PA_4,PA_3,PA_1); |
sepp_nepp | 9:efe9f76d6f76 | 92 | * Creabot mybot(&wheelLeft, &WheelRight, 10.0f, 13.0f); // insert the wheels and indicate wheel diameter (10cm) and distance between wheels (13cm) |
sepp_nepp | 9:efe9f76d6f76 | 93 | * |
sepp_nepp | 9:efe9f76d6f76 | 94 | * int main() { |
sepp_nepp | 9:efe9f76d6f76 | 95 | * |
sepp_nepp | 9:efe9f76d6f76 | 96 | * mybot.setSpeed(12.5); // Preset speed to 12.5cm/s |
sepp_nepp | 9:efe9f76d6f76 | 97 | * mybot.iMove(FORWARD,10); // Go forward of 10cm |
sepp_nepp | 9:efe9f76d6f76 | 98 | * mybot.iWaitEnd(); // Wait end of Move |
sepp_nepp | 9:efe9f76d6f76 | 99 | * mybot.iMove(ROTATE,90); // Start rotation of 90° around the center between wheels (two wheels running in same direction at same speed) |
sepp_nepp | 9:efe9f76d6f76 | 100 | * mybot.iMove(BACKWARD,40); // Stop immediately the rotation and go backward of 40cm |
sepp_nepp | 9:efe9f76d6f76 | 101 | * mybot.iWaitEnd(); // Wait end of Backward |
sepp_nepp | 9:efe9f76d6f76 | 102 | * mybot.iMoveAndWait(LEFT,60); // Move Left of 60° in circle, center being the left wheel (off). Wait end of move |
sepp_nepp | 9:efe9f76d6f76 | 103 | * mybot.iWaitEnd(); // Not needed, as already waited... |
sepp_nepp | 9:efe9f76d6f76 | 104 | * mybot.iMoveAndWait(RIGHT,45, 33); // Move Right of 45°, center being at 33cm of the right wheel. Right wheel moving slower and on a shorter distance than left one. |
sepp_nepp | 9:efe9f76d6f76 | 105 | * mybot.iMoveAndWait(ROTATE,90); |
sepp_nepp | 9:efe9f76d6f76 | 106 | * mybot.iMove(ROTATE,-90); // Opposite direction. |
sepp_nepp | 9:efe9f76d6f76 | 107 | * mybot.iWaitEnd(6); // with time-out => if after 6s the move is not ended, continue the execution |
sepp_nepp | 9:efe9f76d6f76 | 108 | * mybot.iStop(); // Stop the movement in case it was not ended before ... |
sepp_nepp | 9:efe9f76d6f76 | 109 | * |
sepp_nepp | 9:efe9f76d6f76 | 110 | * // Same with a Queue of command, opposite to the move, receiving a new command will not stop the current execution, but the bot will store it. |
sepp_nepp | 9:efe9f76d6f76 | 111 | * mybot.qMove(FORWARD,10); // Already starting... |
sepp_nepp | 9:efe9f76d6f76 | 112 | * mybot.qMove(BACKWARD,10); // will wait end of previous command to go |
sepp_nepp | 9:efe9f76d6f76 | 113 | * mybot.qMove(ROTATE,120.0); |
sepp_nepp | 9:efe9f76d6f76 | 114 | * mybot.qMove(LEFT, 30, 120); |
sepp_nepp | 9:efe9f76d6f76 | 115 | * mybot.qMove(RIGHT, 25); |
sepp_nepp | 9:efe9f76d6f76 | 116 | * ... // insert other code here that executes while the motors continue to move |
sepp_nepp | 9:efe9f76d6f76 | 117 | * mybot.iWaitEnd(100000); // wait until Queue end... can flush anytime with stopMove... |
sepp_nepp | 9:efe9f76d6f76 | 118 | * mybot.qEmpty(); // before end... Flush the Queue and remove all instructions… |
sepp_nepp | 9:efe9f76d6f76 | 119 | * |
sepp_nepp | 9:efe9f76d6f76 | 120 | * while(1) { |
sepp_nepp | 9:efe9f76d6f76 | 121 | * }; |
sepp_nepp | 9:efe9f76d6f76 | 122 | * } |
sepp_nepp | 9:efe9f76d6f76 | 123 | * @endcode |
sepp_nepp | 9:efe9f76d6f76 | 124 | */ |
sepp_nepp | 5:efe80c5db389 | 125 | |
garphil | 0:a7fb03c9ea9d | 126 | class Creabot { |
garphil | 0:a7fb03c9ea9d | 127 | public: |
sepp_nepp | 9:efe9f76d6f76 | 128 | /** Create a Creabot object with 2 wheels |
garphil | 0:a7fb03c9ea9d | 129 | * |
sepp_nepp | 10:79509113310a | 130 | * @param <left CreaMot> object, corresponding to left wheel of the Creabot |
sepp_nepp | 10:79509113310a | 131 | * @param <right CreaMot> object, corresponding to right wheel of the Creabot |
sepp_nepp | 9:efe9f76d6f76 | 132 | * @param <wheel_diam_cm> diameter in cm of the wheels (both must be the same diameter) |
sepp_nepp | 9:efe9f76d6f76 | 133 | * @param <bot_diam_cm> distance cm between center of left wheel and center of right wheel |
garphil | 0:a7fb03c9ea9d | 134 | */ |
sepp_nepp | 10:79509113310a | 135 | Creabot(CreaMot *left, CreaMot *right, float wheel_diam_cm, float bot_diam_cm); |
sepp_nepp | 6:4d8938b686a6 | 136 | |
sepp_nepp | 9:efe9f76d6f76 | 137 | /** Property access to wheel state, indicating which of the wheels are moving */ |
sepp_nepp | 9:efe9f76d6f76 | 138 | TWheelsState getState() {return wheelsState; }; |
garphil | 0:a7fb03c9ea9d | 139 | |
sepp_nepp | 9:efe9f76d6f76 | 140 | /** Setup a Callback method. It is called when all programmed movements are finished. */ |
sepp_nepp | 10:79509113310a | 141 | void setCallBack(void (*Acallback)(int status)) { extCallBack = Acallback; }; |
sepp_nepp | 9:efe9f76d6f76 | 142 | |
sepp_nepp | 9:efe9f76d6f76 | 143 | /** High level: set bot-speed parameter for all future wheel commands. |
sepp_nepp | 10:79509113310a | 144 | * The set speed is used for immediate as well as the queued movements. |
sepp_nepp | 10:79509113310a | 145 | * In a curve movement it determines the speed of the outer wheel. */ |
sepp_nepp | 9:efe9f76d6f76 | 146 | void setSpeed(float AbotSpeed_cm_sec); |
sepp_nepp | 7:3a793ddc3490 | 147 | |
sepp_nepp | 7:3a793ddc3490 | 148 | public: |
sepp_nepp | 9:efe9f76d6f76 | 149 | |
sepp_nepp | 10:79509113310a | 150 | /** High level, queued: move bot according to command and parameter |
sepp_nepp | 9:efe9f76d6f76 | 151 | * Composes a BotCommand and appends it to the queue for queued execution. |
sepp_nepp | 9:efe9f76d6f76 | 152 | * Use for commands that need only one parameter: FORWARD, BACKWARD, ROTATE |
sepp_nepp | 9:efe9f76d6f76 | 153 | * For details refer to docu of moveForward(), moveBackward(), moveRotate(). |
sepp_nepp | 9:efe9f76d6f76 | 154 | * Can also be called with IDLE, but then has no effect. |
sepp_nepp | 10:79509113310a | 155 | * Preset the speed using setSpeed().] |
sepp_nepp | 10:79509113310a | 156 | * |
sepp_nepp | 9:efe9f76d6f76 | 157 | * @param[in] <Acommand> Requested movement, of type BotCmdVerb. |
sepp_nepp | 9:efe9f76d6f76 | 158 | * @param[in] <Aparam> Requested amount, defines angle for ROTATE, or distance for FORWARD, BACKWARD. |
sepp_nepp | 9:efe9f76d6f76 | 159 | */ |
sepp_nepp | 9:efe9f76d6f76 | 160 | void qMove(BotCmdVerb Acommand, float Aparam); |
sepp_nepp | 9:efe9f76d6f76 | 161 | |
sepp_nepp | 9:efe9f76d6f76 | 162 | /** High level, queued : move bot according to command and parameters |
sepp_nepp | 9:efe9f76d6f76 | 163 | * Composes a BotCommand and appends it to the queue for queued execution. |
sepp_nepp | 9:efe9f76d6f76 | 164 | * Use for commands that need two parameters: LEFT, RIGHT, BACKLEFT, BACKRIGHT |
sepp_nepp | 10:79509113310a | 165 | * For details refer to docu of moveLeft(), moveRight(), moveBackLeft(), moveBackRight(). |
sepp_nepp | 9:efe9f76d6f76 | 166 | * Can also be called with IDLE, but then has no effect. |
sepp_nepp | 9:efe9f76d6f76 | 167 | * |
sepp_nepp | 10:79509113310a | 168 | * Reserves a new command element at the head of the Queue |
sepp_nepp | 9:efe9f76d6f76 | 169 | * |
sepp_nepp | 9:efe9f76d6f76 | 170 | * Adds incoming commands at the head of the ring buffer at position writeIdx, |
sepp_nepp | 9:efe9f76d6f76 | 171 | * If Queue is full, it passes back NULL |
sepp_nepp | 9:efe9f76d6f76 | 172 | * Otherwise Advances the write index once and returns a pointer the next free command struct. |
sepp_nepp | 9:efe9f76d6f76 | 173 | * The caller then must fill the command structure at that position. |
sepp_nepp | 9:efe9f76d6f76 | 174 | * Do not free memory associated to the pointer. |
sepp_nepp | 9:efe9f76d6f76 | 175 | * |
sepp_nepp | 9:efe9f76d6f76 | 176 | * @param[in] <Acommand> Requested movement, of type BotCmdVerb. |
sepp_nepp | 9:efe9f76d6f76 | 177 | * @param[in] <Aturn_angle_deg> Requested angle in degrees. |
sepp_nepp | 9:efe9f76d6f76 | 178 | * @param[in] <Adist_cm> Requested distance in centimeters. |
sepp_nepp | 9:efe9f76d6f76 | 179 | */ |
sepp_nepp | 9:efe9f76d6f76 | 180 | void qMove(BotCmdVerb Acommand, float Aturn_angle_deg, float Adist_cm); |
sepp_nepp | 9:efe9f76d6f76 | 181 | |
sepp_nepp | 9:efe9f76d6f76 | 182 | /** Execute and remove the oldest element at the tail of the Queue |
sepp_nepp | 9:efe9f76d6f76 | 183 | * |
sepp_nepp | 9:efe9f76d6f76 | 184 | * If Queue is empty, nothing happens |
sepp_nepp | 9:efe9f76d6f76 | 185 | * Otherwise executes oldest commands from the tail of the ring buffer at position readIdx, |
sepp_nepp | 10:79509113310a | 186 | * Then advances the read index once. */ |
sepp_nepp | 9:efe9f76d6f76 | 187 | void qExecuteNext(); |
sepp_nepp | 9:efe9f76d6f76 | 188 | |
sepp_nepp | 9:efe9f76d6f76 | 189 | /** Public access of Queue used Count. |
sepp_nepp | 9:efe9f76d6f76 | 190 | * |
sepp_nepp | 9:efe9f76d6f76 | 191 | * @return <int> the number of commands in the Queue */ |
sepp_nepp | 9:efe9f76d6f76 | 192 | int qCount() {return Count;} |
sepp_nepp | 8:3b5b0a82b429 | 193 | |
sepp_nepp | 9:efe9f76d6f76 | 194 | /* Immediately end all queue activites, and the motor |
sepp_nepp | 9:efe9f76d6f76 | 195 | * Does not call the external callback handler */ |
sepp_nepp | 9:efe9f76d6f76 | 196 | void qStopAll(); |
sepp_nepp | 9:efe9f76d6f76 | 197 | |
sepp_nepp | 9:efe9f76d6f76 | 198 | private: |
sepp_nepp | 9:efe9f76d6f76 | 199 | /** Empty the Queue. |
sepp_nepp | 10:79509113310a | 200 | * Since the ring buffer is static, it suffice to set all pointers to 0, |
sepp_nepp | 10:79509113310a | 201 | * Commands are left in memory. */ |
sepp_nepp | 9:efe9f76d6f76 | 202 | void qReset() { qBlock = true; readIdx=writeIdx=Count=0; qBlock = false; qCollide = false;}; |
sepp_nepp | 9:efe9f76d6f76 | 203 | |
sepp_nepp | 10:79509113310a | 204 | /** Check if Queue is full. |
sepp_nepp | 9:efe9f76d6f76 | 205 | * |
sepp_nepp | 9:efe9f76d6f76 | 206 | * Space is limited to DEPTH_Queue=256 BotCommand elements. |
sepp_nepp | 10:79509113310a | 207 | * If in doubt it should be checked before trying to add new elements |
sepp_nepp | 9:efe9f76d6f76 | 208 | * @return <bool> True if Queue is full*/ |
sepp_nepp | 9:efe9f76d6f76 | 209 | bool qIsFull() {return Count>=DEPTH_Queue;} |
sepp_nepp | 9:efe9f76d6f76 | 210 | |
sepp_nepp | 10:79509113310a | 211 | /** Check if Queue is empty. |
sepp_nepp | 10:79509113310a | 212 | * |
sepp_nepp | 9:efe9f76d6f76 | 213 | * Should be checked before trying to get new elements |
sepp_nepp | 9:efe9f76d6f76 | 214 | * @return <bool> True if Queue is empty*/ |
sepp_nepp | 9:efe9f76d6f76 | 215 | bool qIsEmpty() {return Count<=0;} |
sepp_nepp | 9:efe9f76d6f76 | 216 | |
sepp_nepp | 9:efe9f76d6f76 | 217 | /** 256 elements deep Command Queue, to put BotCommand in a queue. |
sepp_nepp | 9:efe9f76d6f76 | 218 | * Stores all BotCommand in this static 256 array used as a circular ring buffer. */ |
sepp_nepp | 9:efe9f76d6f76 | 219 | BotCommand cmd[DEPTH_Queue]; /**< Actual static Queue array where all elements reside. */ |
sepp_nepp | 9:efe9f76d6f76 | 220 | |
sepp_nepp | 9:efe9f76d6f76 | 221 | int writeIdx; /**< Index in Queue array where to put the next new element to. */ |
sepp_nepp | 9:efe9f76d6f76 | 222 | int readIdx; /**< Index in Queue array where to get the oldest element from. */ |
sepp_nepp | 9:efe9f76d6f76 | 223 | int Count; /**< Counts and keeps track of the number of elements in array used. */ |
sepp_nepp | 9:efe9f76d6f76 | 224 | bool qBlock; /**< Blocks read access while a write is ongoing. */ |
sepp_nepp | 9:efe9f76d6f76 | 225 | bool qCollide;/**< Indicates a colliding access to the queue. */ |
sepp_nepp | 10:79509113310a | 226 | |
sepp_nepp | 9:efe9f76d6f76 | 227 | public: |
sepp_nepp | 9:efe9f76d6f76 | 228 | /** High level, immediate: move bot and wait for movement end. |
sepp_nepp | 10:79509113310a | 229 | * Simple wrapper for iMove(Acommand,Aparam) and iWaitEnd(). |
sepp_nepp | 9:efe9f76d6f76 | 230 | * Refer to those methods for further docs. |
sepp_nepp | 9:efe9f76d6f76 | 231 | */ |
sepp_nepp | 9:efe9f76d6f76 | 232 | void iMoveAndWait(BotCmdVerb Acommand, float Aparam); |
sepp_nepp | 9:efe9f76d6f76 | 233 | |
sepp_nepp | 9:efe9f76d6f76 | 234 | /** High level, immediate: move bot and wait for movement end. |
sepp_nepp | 10:79509113310a | 235 | * Simple wrapper for iMove(Acommand,Aturn_angle_deg,Adist_cm) and iWaitEnd(). |
sepp_nepp | 9:efe9f76d6f76 | 236 | * Refer to those methods for further docs. |
sepp_nepp | 9:efe9f76d6f76 | 237 | */ |
sepp_nepp | 9:efe9f76d6f76 | 238 | void iMoveAndWait(BotCmdVerb Acommand, float Aturn_angle_deg, float Adist_cm); |
sepp_nepp | 9:efe9f76d6f76 | 239 | |
sepp_nepp | 9:efe9f76d6f76 | 240 | /** High level, immediate: move bot according to command and parameter |
sepp_nepp | 9:efe9f76d6f76 | 241 | * Composes a BotCommand and passes it to executeCommand(). |
sepp_nepp | 8:3b5b0a82b429 | 242 | * Use for commands that need only one parameter: FORWARD, BACKWARD, ROTATE |
sepp_nepp | 9:efe9f76d6f76 | 243 | * For details refer to docu of moveForward(), moveBackward(), moveRotate(). |
sepp_nepp | 8:3b5b0a82b429 | 244 | * Can also be called with IDLE, but then has no effect. |
sepp_nepp | 9:efe9f76d6f76 | 245 | * Preset the speed using setSpeed(). |
sepp_nepp | 8:3b5b0a82b429 | 246 | * Warning: Collides with queued commands. |
sepp_nepp | 10:79509113310a | 247 | * |
sepp_nepp | 9:efe9f76d6f76 | 248 | * @param[in] <Acommand> Requested movement, of type BotCmdVerb. |
sepp_nepp | 9:efe9f76d6f76 | 249 | * @param[in] <Aparam> Requested amount, defines angle for ROTATE, or distance for FORWARD, BACKWARD. |
sepp_nepp | 9:efe9f76d6f76 | 250 | */ |
sepp_nepp | 9:efe9f76d6f76 | 251 | void iMove(BotCmdVerb Acommand, float Aparam); |
sepp_nepp | 9:efe9f76d6f76 | 252 | |
sepp_nepp | 9:efe9f76d6f76 | 253 | /** High level, immediate: move bot according to command and parameters |
sepp_nepp | 9:efe9f76d6f76 | 254 | * Composes a BotCommand and passes it to executeCommand(). |
sepp_nepp | 9:efe9f76d6f76 | 255 | * Use for commands that need two parameters: LEFT, RIGHT, BACKLEFT, BACKRIGHT |
sepp_nepp | 10:79509113310a | 256 | * For details refer to docu of moveLeft(), moveRight(), moveBackLeft(), moveBackRight(). |
sepp_nepp | 9:efe9f76d6f76 | 257 | * Can also be called with IDLE, but then has no effect. |
sepp_nepp | 9:efe9f76d6f76 | 258 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 259 | * Warning: Collides with queued commands. |
sepp_nepp | 10:79509113310a | 260 | * |
sepp_nepp | 9:efe9f76d6f76 | 261 | * @param[in] <Acommand> Requested movement, of type BotCmdVerb. |
sepp_nepp | 9:efe9f76d6f76 | 262 | * @param[in] <Aturn_angle_deg> Requested angle in degrees. |
sepp_nepp | 9:efe9f76d6f76 | 263 | * @param[in] <Adist_cm> Requested distance in centimeters. |
sepp_nepp | 8:3b5b0a82b429 | 264 | */ |
sepp_nepp | 9:efe9f76d6f76 | 265 | void iMove(BotCmdVerb Acommand, float Aturn_angle_deg, float Adist_cm); |
sepp_nepp | 9:efe9f76d6f76 | 266 | |
sepp_nepp | 9:efe9f76d6f76 | 267 | /** High level, immediate: move bot according to prefilled command structures. |
sepp_nepp | 10:79509113310a | 268 | * Recommended to use iMove() methods to fill the command structure correctly. |
sepp_nepp | 9:efe9f76d6f76 | 269 | * Branches to the moveXXXX() methods. For details see docs for those methods. |
sepp_nepp | 9:efe9f76d6f76 | 270 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 271 | * Warning: Collides with queued commands if called individually. |
sepp_nepp | 10:79509113310a | 272 | * |
sepp_nepp | 9:efe9f76d6f76 | 273 | * @param[in] <*cmd> Pointer to type BotCommand, the prefilled command structure, |
sepp_nepp | 9:efe9f76d6f76 | 274 | */ |
sepp_nepp | 9:efe9f76d6f76 | 275 | void iExeCommand(BotCommand *cmd); |
sepp_nepp | 9:efe9f76d6f76 | 276 | |
sepp_nepp | 9:efe9f76d6f76 | 277 | /** High Level: spends all time with waits, |
sepp_nepp | 9:efe9f76d6f76 | 278 | * returns only once wheelState = LRWHEELS_STOP, |
sepp_nepp | 9:efe9f76d6f76 | 279 | * not recommended due its blocking behavior */ |
sepp_nepp | 9:efe9f76d6f76 | 280 | void iWaitEnd(); |
sepp_nepp | 9:efe9f76d6f76 | 281 | |
sepp_nepp | 9:efe9f76d6f76 | 282 | /** High Level: spends all time with waits, |
sepp_nepp | 9:efe9f76d6f76 | 283 | * returns only once wheelState = LRWHEELS_STOP, |
sepp_nepp | 9:efe9f76d6f76 | 284 | * but waits no more than max_wait_ms milli seconds. |
sepp_nepp | 9:efe9f76d6f76 | 285 | * Not recommended due its blocking behavior */ |
sepp_nepp | 9:efe9f76d6f76 | 286 | void iWaitEnd(uint32_t max_wait_ms); // time-out |
sepp_nepp | 9:efe9f76d6f76 | 287 | |
sepp_nepp | 9:efe9f76d6f76 | 288 | /** High level, immediate: Both wheels get stopped, and turned off |
sepp_nepp | 9:efe9f76d6f76 | 289 | * updates the wheel state wheelsState, does not call the callback handler! |
sepp_nepp | 10:79509113310a | 290 | * It does not empty the queue. |
sepp_nepp | 10:79509113310a | 291 | * Adding new elements into the queue after calling iStop() |
sepp_nepp | 10:79509113310a | 292 | * Would continue using all commands still in the queue. */ |
sepp_nepp | 9:efe9f76d6f76 | 293 | void iStop(); |
garphil | 0:a7fb03c9ea9d | 294 | |
sepp_nepp | 7:3a793ddc3490 | 295 | public: |
sepp_nepp | 5:efe80c5db389 | 296 | |
sepp_nepp | 9:efe9f76d6f76 | 297 | /** Mid level, immediate: advance bot straight forward for a given distance, |
sepp_nepp | 9:efe9f76d6f76 | 298 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 299 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 300 | */ |
sepp_nepp | 7:3a793ddc3490 | 301 | void moveForward(float dist_cm); |
sepp_nepp | 8:3b5b0a82b429 | 302 | |
sepp_nepp | 9:efe9f76d6f76 | 303 | /** Mid level, immediate: reverse bot straight backwards for a given distance, |
sepp_nepp | 9:efe9f76d6f76 | 304 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 305 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 306 | */ |
sepp_nepp | 7:3a793ddc3490 | 307 | void moveBackward(float dist_cm); |
sepp_nepp | 8:3b5b0a82b429 | 308 | |
sepp_nepp | 8:3b5b0a82b429 | 309 | /* Mid level, immediate: turn bot forward right, |
sepp_nepp | 8:3b5b0a82b429 | 310 | * around a radius twice the bot size , |
sepp_nepp | 9:efe9f76d6f76 | 311 | * Same as moveRight(turn_angle_deg, 0); |
sepp_nepp | 9:efe9f76d6f76 | 312 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 313 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 314 | */ |
sepp_nepp | 9:efe9f76d6f76 | 315 | void moveRight(float turn_angle_deg); |
sepp_nepp | 8:3b5b0a82b429 | 316 | |
sepp_nepp | 8:3b5b0a82b429 | 317 | /** Mid level, immediate: turn bot forward right, |
sepp_nepp | 8:3b5b0a82b429 | 318 | * around a radius that is center_cm away from the right wheel, |
sepp_nepp | 9:efe9f76d6f76 | 319 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 320 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 321 | */ |
sepp_nepp | 9:efe9f76d6f76 | 322 | void moveRight(float turn_angle_deg, float center_cm); |
sepp_nepp | 8:3b5b0a82b429 | 323 | |
sepp_nepp | 8:3b5b0a82b429 | 324 | /** Mid level, immediate: turn bot forward left, |
sepp_nepp | 8:3b5b0a82b429 | 325 | * around a radius twice the bot size, |
sepp_nepp | 9:efe9f76d6f76 | 326 | * Same as moveLeft(turn_angle_deg, 0); |
sepp_nepp | 9:efe9f76d6f76 | 327 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 328 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 329 | */ |
sepp_nepp | 9:efe9f76d6f76 | 330 | void moveLeft(float turn_angle_deg); |
sepp_nepp | 8:3b5b0a82b429 | 331 | |
sepp_nepp | 8:3b5b0a82b429 | 332 | /** Mid level, immediate: turn bot forward left, |
sepp_nepp | 9:efe9f76d6f76 | 333 | * around a radius that is center_cm away from the left wheel, |
sepp_nepp | 9:efe9f76d6f76 | 334 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 335 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 336 | */ |
sepp_nepp | 9:efe9f76d6f76 | 337 | void moveLeft(float turn_angle_deg, float center_cm); |
sepp_nepp | 9:efe9f76d6f76 | 338 | |
sepp_nepp | 9:efe9f76d6f76 | 339 | /* Mid level, immediate: turn bot backwards right, |
sepp_nepp | 9:efe9f76d6f76 | 340 | * around a radius twice the bot size , |
sepp_nepp | 10:79509113310a | 341 | * Same as moveBackRight(turn_angle_deg, 0); |
sepp_nepp | 9:efe9f76d6f76 | 342 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 343 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 344 | */ |
sepp_nepp | 10:79509113310a | 345 | void moveBackRight(float turn_angle_deg); |
sepp_nepp | 9:efe9f76d6f76 | 346 | |
sepp_nepp | 9:efe9f76d6f76 | 347 | /** Mid level, immediate: turn bot backwards right, |
sepp_nepp | 9:efe9f76d6f76 | 348 | * around a radius that is center_cm away from the right wheel, |
sepp_nepp | 9:efe9f76d6f76 | 349 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 350 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 351 | */ |
sepp_nepp | 10:79509113310a | 352 | void moveBackRight(float turn_angle_deg, float center_cm); |
sepp_nepp | 9:efe9f76d6f76 | 353 | |
sepp_nepp | 9:efe9f76d6f76 | 354 | /** Mid level, immediate: turn bot backwards left, |
sepp_nepp | 9:efe9f76d6f76 | 355 | * around a radius twice the bot size, |
sepp_nepp | 10:79509113310a | 356 | * Same as moveBackLeft(turn_angle_deg, 0); |
sepp_nepp | 9:efe9f76d6f76 | 357 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 358 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 359 | */ |
sepp_nepp | 10:79509113310a | 360 | void moveBackLeft(float turn_angle_deg); |
sepp_nepp | 9:efe9f76d6f76 | 361 | |
sepp_nepp | 9:efe9f76d6f76 | 362 | /** Mid level, immediate: turn bot backwards left, |
sepp_nepp | 8:3b5b0a82b429 | 363 | around a radius that is center_cm away from the left wheel, |
sepp_nepp | 9:efe9f76d6f76 | 364 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 365 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 366 | */ |
sepp_nepp | 10:79509113310a | 367 | void moveBackLeft(float turn_angle_deg, float center_cm); |
sepp_nepp | 9:efe9f76d6f76 | 368 | |
sepp_nepp | 8:3b5b0a82b429 | 369 | /** Mid level, immediate: turn bot on its place for a given angle. |
sepp_nepp | 8:3b5b0a82b429 | 370 | * positive angles turn right, negative angles turn left, |
sepp_nepp | 9:efe9f76d6f76 | 371 | * Preset the speed using setSpeed(). |
sepp_nepp | 9:efe9f76d6f76 | 372 | * Warning: Collides with queued commands. |
sepp_nepp | 9:efe9f76d6f76 | 373 | */ |
sepp_nepp | 9:efe9f76d6f76 | 374 | void moveRotate(float turn_angle_deg); |
sepp_nepp | 7:3a793ddc3490 | 375 | |
sepp_nepp | 6:4d8938b686a6 | 376 | private: |
sepp_nepp | 7:3a793ddc3490 | 377 | |
sepp_nepp | 9:efe9f76d6f76 | 378 | /** Low level, immediate: sets Both wheel speeds */ |
sepp_nepp | 9:efe9f76d6f76 | 379 | void wheelsSetSpeed(float mot_speed_cm_sec); |
sepp_nepp | 8:3b5b0a82b429 | 380 | |
sepp_nepp | 9:efe9f76d6f76 | 381 | /** Callback when left wheel stops; |
sepp_nepp | 9:efe9f76d6f76 | 382 | * updates the wheel state wheelsState */ |
sepp_nepp | 9:efe9f76d6f76 | 383 | void wheelLeftStoppedCB(); |
sepp_nepp | 8:3b5b0a82b429 | 384 | |
sepp_nepp | 9:efe9f76d6f76 | 385 | /** Callback when right wheel stops. |
sepp_nepp | 9:efe9f76d6f76 | 386 | * updates the wheel state wheelsState */ |
sepp_nepp | 9:efe9f76d6f76 | 387 | void wheelRightStoppedCB(); |
sepp_nepp | 8:3b5b0a82b429 | 388 | |
sepp_nepp | 9:efe9f76d6f76 | 389 | /** Callback when all wheels have stopped. |
sepp_nepp | 9:efe9f76d6f76 | 390 | * updates the wheel state wheelsState, switch off all Phases */ |
sepp_nepp | 9:efe9f76d6f76 | 391 | void wheelsAllStoppedCB(); |
sepp_nepp | 7:3a793ddc3490 | 392 | |
sepp_nepp | 8:3b5b0a82b429 | 393 | /** Callback function pointer: |
sepp_nepp | 9:efe9f76d6f76 | 394 | * If set non-NULL it is called when All wheels Stopped()*/ |
sepp_nepp | 7:3a793ddc3490 | 395 | void (*extCallBack)(int status); |
sepp_nepp | 7:3a793ddc3490 | 396 | |
sepp_nepp | 9:efe9f76d6f76 | 397 | /** Wheel status register: |
sepp_nepp | 9:efe9f76d6f76 | 398 | * Remembers which of the wheels still run */ |
sepp_nepp | 9:efe9f76d6f76 | 399 | TWheelsState wheelsState; |
sepp_nepp | 7:3a793ddc3490 | 400 | |
sepp_nepp | 9:efe9f76d6f76 | 401 | /** geometric properties of the bot */ |
sepp_nepp | 9:efe9f76d6f76 | 402 | float botDiameter; |
sepp_nepp | 9:efe9f76d6f76 | 403 | |
sepp_nepp | 8:3b5b0a82b429 | 404 | /** Ratio of wheel diameter and bot diameter */ |
sepp_nepp | 8:3b5b0a82b429 | 405 | float ratio_wheel_bot; |
sepp_nepp | 9:efe9f76d6f76 | 406 | |
sepp_nepp | 8:3b5b0a82b429 | 407 | /** last requested bot speed */ |
sepp_nepp | 9:efe9f76d6f76 | 408 | float botSpeed_cm_sec; |
sepp_nepp | 8:3b5b0a82b429 | 409 | |
sepp_nepp | 9:efe9f76d6f76 | 410 | /** The two wheels, as pointers to their classes */ |
sepp_nepp | 10:79509113310a | 411 | CreaMot *wheelLeft; |
sepp_nepp | 10:79509113310a | 412 | CreaMot *wheelRight; |
garphil | 0:a7fb03c9ea9d | 413 | }; |
garphil | 0:a7fb03c9ea9d | 414 | |
sepp_nepp | 9:efe9f76d6f76 | 415 | |
garphil | 0:a7fb03c9ea9d | 416 | #endif |