Working version without LEDs
Voici le dernier schéma de cablage (version du 08/02/2020)
https://os.mbed.com/media/uploads/max_ence/schemarobot_fev2020.pdf
CreabotLib/CreaBot.h@1:69b5d8f0ba9c, 2020-05-30 (annotated)
- Committer:
- elab
- Date:
- Sat May 30 09:31:57 2020 +0000
- Revision:
- 1:69b5d8f0ba9c
- Parent:
- 0:0e577ce96b2f
pour eLab;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elab | 0:0e577ce96b2f | 1 | |
elab | 0:0e577ce96b2f | 2 | #ifndef CREABOT_H |
elab | 0:0e577ce96b2f | 3 | #define CREABOT_H |
elab | 0:0e577ce96b2f | 4 | |
elab | 0:0e577ce96b2f | 5 | #include "mbed.h" |
elab | 0:0e577ce96b2f | 6 | #include "motor.h" |
elab | 0:0e577ce96b2f | 7 | |
elab | 0:0e577ce96b2f | 8 | #define MAX_SPEED 30 //cm per second |
elab | 0:0e577ce96b2f | 9 | #define DEFAULT_SPEED 2 //cm per second |
elab | 0:0e577ce96b2f | 10 | #define PI 3.141592f |
elab | 0:0e577ce96b2f | 11 | #define DEPTH_FIFO 256 |
elab | 0:0e577ce96b2f | 12 | |
elab | 0:0e577ce96b2f | 13 | typedef enum {FORWARD, BACKWARD, ROTATE, LEFT, RIGHT, IDLE} cmdbot_t; |
elab | 0:0e577ce96b2f | 14 | class CommandBot { |
elab | 0:0e577ce96b2f | 15 | public: |
elab | 0:0e577ce96b2f | 16 | cmdbot_t command; |
elab | 0:0e577ce96b2f | 17 | float angle; |
elab | 0:0e577ce96b2f | 18 | float cm; |
elab | 0:0e577ce96b2f | 19 | |
elab | 0:0e577ce96b2f | 20 | void add_command(cmdbot_t cmd, float p1, float p2) {command = cmd; switch (command) {case FORWARD:;case BACKWARD: cm=p1; angle=0; break; case ROTATE: angle = p1; break; case LEFT: case RIGHT: angle = p1; cm=p2;break; case IDLE:break; default: break;}}; |
elab | 0:0e577ce96b2f | 21 | }; |
elab | 0:0e577ce96b2f | 22 | |
elab | 0:0e577ce96b2f | 23 | class MyFifoCmd { |
elab | 0:0e577ce96b2f | 24 | |
elab | 0:0e577ce96b2f | 25 | public: |
elab | 0:0e577ce96b2f | 26 | MyFifoCmd() {start = end = size = 0;cmd_idle.command = IDLE;cmd_idle.angle=0;cmd_idle.cm=0;}; |
elab | 0:0e577ce96b2f | 27 | void empty() {start=end=size=0;}; |
elab | 0:0e577ce96b2f | 28 | CommandBot *put() {if(size<DEPTH_FIFO) {int old_end = end;end++;if(end==DEPTH_FIFO)end=0;size++;return &cmd[old_end];} else return NULL;}; |
elab | 0:0e577ce96b2f | 29 | CommandBot *get() {if(size>0) {int old_start = start; start++;if(start==DEPTH_FIFO)start=0; size--; return &cmd[old_start];} else return NULL;} |
elab | 0:0e577ce96b2f | 30 | int getSize() {return size;} |
elab | 0:0e577ce96b2f | 31 | private: |
elab | 0:0e577ce96b2f | 32 | int start, end, size; |
elab | 0:0e577ce96b2f | 33 | CommandBot cmd[DEPTH_FIFO]; |
elab | 0:0e577ce96b2f | 34 | CommandBot cmd_idle; |
elab | 0:0e577ce96b2f | 35 | }; |
elab | 0:0e577ce96b2f | 36 | |
elab | 0:0e577ce96b2f | 37 | |
elab | 0:0e577ce96b2f | 38 | /** Asynchronous Control of 2 motors part of a robot |
elab | 0:0e577ce96b2f | 39 | * |
elab | 0:0e577ce96b2f | 40 | * Example: |
elab | 0:0e577ce96b2f | 41 | * @code |
elab | 0:0e577ce96b2f | 42 | * // --- Define the Four PINs & Time of movement used for Motor drive ----- |
elab | 0:0e577ce96b2f | 43 | * Motor motorLeft(PA_12, PB_0, PB_1, PB_6); // Declare first the 2 motors (to avoid to have an object with 8 pins to create) |
elab | 0:0e577ce96b2f | 44 | * Motor motorRight(PA_5,PA_4,PA_3,PA_1); |
elab | 0:0e577ce96b2f | 45 | * Creabot mybot(&motorLeft, &motorRight, 10.0f,13.0f); // insert the motors and indicate wheel diameter and distance between wheels |
elab | 0:0e577ce96b2f | 46 | * |
elab | 0:0e577ce96b2f | 47 | * int main() { |
elab | 0:0e577ce96b2f | 48 | * |
elab | 0:0e577ce96b2f | 49 | * mybot.setSpeed(12.5); // 12.5cm/s |
elab | 0:0e577ce96b2f | 50 | * mybot.move(FORWARD,10); // Go forward of 10cm |
elab | 0:0e577ce96b2f | 51 | * mybot.waitEndMove(); // Wait end of Move |
elab | 0:0e577ce96b2f | 52 | * mybot.move(ROTATE,90); // Start rotation of 90° around the center between wheels (two wheels running in same direction at same speed) |
elab | 0:0e577ce96b2f | 53 | * mybot.move(BACKWARD,40); // Stop immediately the rotation and go backward of 40cm |
elab | 0:0e577ce96b2f | 54 | * mybot.waitEndMove(); // Wait end of Backward |
elab | 0:0e577ce96b2f | 55 | * mybot.moveAndWait(LEFT,60); // Move Left of 60° in circle, center being the left wheel (off). Wait end of move |
elab | 0:0e577ce96b2f | 56 | * mybot.waitEndMove(); // Not needed, as already waited... |
elab | 0:0e577ce96b2f | 57 | * mybot.moveAndWait(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. |
elab | 0:0e577ce96b2f | 58 | * mybot.moveAndWait(ROTATE,90); |
elab | 0:0e577ce96b2f | 59 | * mybot.move(ROTATE,-90); // Opposite direction. |
elab | 0:0e577ce96b2f | 60 | * mybot.waitEndMove(60); // with watchdog => if after 60s the move is not ended, continue the execution |
elab | 0:0e577ce96b2f | 61 | * mybot.stopMove(); // Stop the movement before end... |
elab | 0:0e577ce96b2f | 62 | * |
elab | 0:0e577ce96b2f | 63 | * // Same with a fifo of command, opposite to the move, receiving a new command will not stop the current execution, but the bot will store it. |
elab | 0:0e577ce96b2f | 64 | * mybot.fifo(FORWARD,10); // Already starting... |
elab | 0:0e577ce96b2f | 65 | * mybot.fifo(BACKWARD,10); // will wait end of previous command to go |
elab | 0:0e577ce96b2f | 66 | * mybot.fifo(ROTATE,120.0); |
elab | 0:0e577ce96b2f | 67 | * mybot.fifo(LEFT, 30, 120); |
elab | 0:0e577ce96b2f | 68 | * mybot.fifo(RIGHT, 25); |
elab | 0:0e577ce96b2f | 69 | * mybot.waitEndMove(100000); // wait until fifo end... can flush anytime with stopMove... |
elab | 0:0e577ce96b2f | 70 | * mybot.stopMove(); // before end... Flush the fifo and remove all instructions… |
elab | 0:0e577ce96b2f | 71 | * |
elab | 0:0e577ce96b2f | 72 | * while(1) { |
elab | 0:0e577ce96b2f | 73 | * }; |
elab | 0:0e577ce96b2f | 74 | * } |
elab | 0:0e577ce96b2f | 75 | * @endcode |
elab | 0:0e577ce96b2f | 76 | */ |
elab | 0:0e577ce96b2f | 77 | class Creabot { |
elab | 0:0e577ce96b2f | 78 | public: |
elab | 0:0e577ce96b2f | 79 | /** Create a Creabot object to synchronize 2 motors |
elab | 0:0e577ce96b2f | 80 | * |
elab | 0:0e577ce96b2f | 81 | * @param left Motor object declared before corresponding to left motor of the Creabot |
elab | 0:0e577ce96b2f | 82 | * @param right Motor object declared before corresponding to right motor of the Creabot |
elab | 0:0e577ce96b2f | 83 | * @param diameter_wheel_cm diameter in cm of the wheels (both supposed to be at same diameter) |
elab | 0:0e577ce96b2f | 84 | * @param distance_wheel_cm distance in cm between center of left wheel and center of right wheel |
elab | 0:0e577ce96b2f | 85 | */ |
elab | 0:0e577ce96b2f | 86 | Creabot(Motor *left, Motor *right, float diameter_wheel_cm, float distance_wheel_cm); |
elab | 0:0e577ce96b2f | 87 | int getStatus(); |
elab | 0:0e577ce96b2f | 88 | void setSpeed(float cm_per_second); |
elab | 0:0e577ce96b2f | 89 | void waitEndMove(); |
elab | 0:0e577ce96b2f | 90 | void waitEndMove(uint32_t delay_us); // watchdog |
elab | 0:0e577ce96b2f | 91 | void setCallBack(void (*callback)(int status)); |
elab | 0:0e577ce96b2f | 92 | void moveAndWait(cmdbot_t moveType, float angle_or_cm); |
elab | 0:0e577ce96b2f | 93 | void moveAndWait(cmdbot_t moveType, float angle, float cm); |
elab | 0:0e577ce96b2f | 94 | void move(cmdbot_t moveType, float angle_or_cm); |
elab | 0:0e577ce96b2f | 95 | void move(cmdbot_t moveType, float angle, float cm); |
elab | 0:0e577ce96b2f | 96 | void fifo(cmdbot_t moveType, float angle_or_cm); |
elab | 0:0e577ce96b2f | 97 | void fifo(cmdbot_t moveType, float angle, float cm); |
elab | 0:0e577ce96b2f | 98 | void flushFifo(); |
elab | 0:0e577ce96b2f | 99 | int moveInFifo(); |
elab | 0:0e577ce96b2f | 100 | void executeFifo(); |
elab | 0:0e577ce96b2f | 101 | void stopMove(); |
elab | 0:0e577ce96b2f | 102 | |
elab | 0:0e577ce96b2f | 103 | private: |
elab | 0:0e577ce96b2f | 104 | void moveForward(float cm); |
elab | 0:0e577ce96b2f | 105 | void moveBackward(float cm); |
elab | 0:0e577ce96b2f | 106 | void rotate(float angle); |
elab | 0:0e577ce96b2f | 107 | void moveRight(float angle); |
elab | 0:0e577ce96b2f | 108 | void moveRight(float angle, float center_cm); |
elab | 0:0e577ce96b2f | 109 | void moveLeft(float angle); |
elab | 0:0e577ce96b2f | 110 | void moveLeft(float angle, float center_cm); |
elab | 0:0e577ce96b2f | 111 | void callBackLeft(); |
elab | 0:0e577ce96b2f | 112 | void callBackRight(); |
elab | 0:0e577ce96b2f | 113 | void callBack(); |
elab | 0:0e577ce96b2f | 114 | uint32_t computeSpeed(Motor *motor, float cm_per_second); |
elab | 0:0e577ce96b2f | 115 | void setSpeedLeft(float cm_per_second); |
elab | 0:0e577ce96b2f | 116 | void setSpeedRight(float cm_per_second); |
elab | 0:0e577ce96b2f | 117 | |
elab | 0:0e577ce96b2f | 118 | void (*extCallBack)(int status); |
elab | 0:0e577ce96b2f | 119 | void moveMotorLeft(MotorDir dir, float angle); |
elab | 0:0e577ce96b2f | 120 | void moveMotorRight(MotorDir dir, float angle); |
elab | 0:0e577ce96b2f | 121 | void executeCommand(CommandBot *cmd); |
elab | 0:0e577ce96b2f | 122 | |
elab | 0:0e577ce96b2f | 123 | |
elab | 0:0e577ce96b2f | 124 | Motor *motor_left; |
elab | 0:0e577ce96b2f | 125 | Motor *motor_right; |
elab | 0:0e577ce96b2f | 126 | float diameter_wheel; |
elab | 0:0e577ce96b2f | 127 | float distance_wheel; |
elab | 0:0e577ce96b2f | 128 | float perimeter_wheel; |
elab | 0:0e577ce96b2f | 129 | float perimeter_bot; |
elab | 0:0e577ce96b2f | 130 | float degree_wheel_per_cm; |
elab | 0:0e577ce96b2f | 131 | float degree_bot_per_cm; |
elab | 0:0e577ce96b2f | 132 | float ratio_wheel_bot; |
elab | 0:0e577ce96b2f | 133 | float current_speed; |
elab | 0:0e577ce96b2f | 134 | int status; |
elab | 0:0e577ce96b2f | 135 | bool callLeft; |
elab | 0:0e577ce96b2f | 136 | bool callRight; |
elab | 0:0e577ce96b2f | 137 | bool endMove; |
elab | 0:0e577ce96b2f | 138 | MyFifoCmd fifoBot; |
elab | 0:0e577ce96b2f | 139 | CommandBot current_cmd; |
elab | 0:0e577ce96b2f | 140 | Ticker botTicker; |
elab | 0:0e577ce96b2f | 141 | bool executingFifo; |
elab | 0:0e577ce96b2f | 142 | }; |
elab | 0:0e577ce96b2f | 143 | |
elab | 0:0e577ce96b2f | 144 | #endif |