Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Command.cpp@5:0032e2f8949c, 2015-11-28 (annotated)
- Committer:
- Xiaofei
- Date:
- Sat Nov 28 19:36:34 2015 +0000
- Revision:
- 5:0032e2f8949c
- Parent:
- 4:3fbe2d75f7eb
- Child:
- 6:bbaabcc5206d
m
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Xiaofei | 0:4bc34a5fcc29 | 1 | #pragma once |
| Xiaofei | 0:4bc34a5fcc29 | 2 | #include "Command.h" |
| Xiaofei | 0:4bc34a5fcc29 | 3 | #include "mbed.h" |
| Xiaofei | 1:13c4bf8989d5 | 4 | #include "Motor.h" |
| Xiaofei | 0:4bc34a5fcc29 | 5 | |
| Xiaofei | 4:3fbe2d75f7eb | 6 | Motor _LEFT_WHEEL(p25, p6, p5); // Motor A pwm, fwd, rev |
| Xiaofei | 4:3fbe2d75f7eb | 7 | Motor _RIGHT_WHEEL(p26, p7, p8); // Motor B pwm, fwd, rev |
| Xiaofei | 3:97a5a3744481 | 8 | DigitalOut _STBY(p12); // Set STBY = 1, enable both motor; Set STBY = 0; disable both motor. |
| Xiaofei | 2:3d1c13d63966 | 9 | AnalogIn _IR(p17); // IR sensor |
| Xiaofei | 1:13c4bf8989d5 | 10 | DigitalOut _LED1(LED1); |
| Xiaofei | 1:13c4bf8989d5 | 11 | |
| Xiaofei | 1:13c4bf8989d5 | 12 | DigitalIn _LEFT_ENCODER(p19); |
| Xiaofei | 1:13c4bf8989d5 | 13 | DigitalIn _RIGHT_ENCODER(p20); |
| Xiaofei | 0:4bc34a5fcc29 | 14 | |
| Xiaofei | 3:97a5a3744481 | 15 | Command::Command():_SPEED(0),_IS_NEGATIVE(0) |
| Xiaofei | 1:13c4bf8989d5 | 16 | {} |
| Xiaofei | 1:13c4bf8989d5 | 17 | |
| Xiaofei | 3:97a5a3744481 | 18 | void Command::setSpeed(const std::int8_t& sp, const std::int8_t& is_negative) |
| Xiaofei | 1:13c4bf8989d5 | 19 | { |
| Xiaofei | 1:13c4bf8989d5 | 20 | _STBY = 1; |
| Xiaofei | 3:97a5a3744481 | 21 | _SPEED = sp * 0.33; |
| Xiaofei | 3:97a5a3744481 | 22 | _IS_NEGATIVE = is_negative; |
| Xiaofei | 1:13c4bf8989d5 | 23 | } |
| Xiaofei | 0:4bc34a5fcc29 | 24 | |
| Xiaofei | 0:4bc34a5fcc29 | 25 | void LedCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 26 | { |
| Xiaofei | 1:13c4bf8989d5 | 27 | _LED1 = !_LED1; |
| Xiaofei | 0:4bc34a5fcc29 | 28 | } |
| Xiaofei | 0:4bc34a5fcc29 | 29 | |
| Xiaofei | 0:4bc34a5fcc29 | 30 | void TurnLeftCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 31 | { |
| Xiaofei | 1:13c4bf8989d5 | 32 | _STBY = 1; |
| Xiaofei | 3:97a5a3744481 | 33 | if(_IS_NEGATIVE) |
| Xiaofei | 3:97a5a3744481 | 34 | { |
| Xiaofei | 3:97a5a3744481 | 35 | _RIGHT_WHEEL.speed(-_SPEED); |
| Xiaofei | 5:0032e2f8949c | 36 | _LEFT_WHEEL.speed(-_SPEED*0.72); |
| Xiaofei | 3:97a5a3744481 | 37 | } |
| Xiaofei | 3:97a5a3744481 | 38 | else |
| Xiaofei | 3:97a5a3744481 | 39 | { |
| Xiaofei | 3:97a5a3744481 | 40 | _RIGHT_WHEEL.speed(_SPEED); |
| Xiaofei | 5:0032e2f8949c | 41 | _LEFT_WHEEL.speed(_SPEED*0.72); |
| Xiaofei | 3:97a5a3744481 | 42 | } |
| Xiaofei | 0:4bc34a5fcc29 | 43 | } |
| Xiaofei | 0:4bc34a5fcc29 | 44 | |
| Xiaofei | 0:4bc34a5fcc29 | 45 | void TurnRightCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 46 | { |
| Xiaofei | 1:13c4bf8989d5 | 47 | _STBY = 1; |
| Xiaofei | 3:97a5a3744481 | 48 | if(_IS_NEGATIVE) |
| Xiaofei | 3:97a5a3744481 | 49 | { |
| Xiaofei | 5:0032e2f8949c | 50 | _RIGHT_WHEEL.speed(-_SPEED*0.72); |
| Xiaofei | 3:97a5a3744481 | 51 | _LEFT_WHEEL.speed(-_SPEED); |
| Xiaofei | 3:97a5a3744481 | 52 | } |
| Xiaofei | 3:97a5a3744481 | 53 | else |
| Xiaofei | 3:97a5a3744481 | 54 | { |
| Xiaofei | 5:0032e2f8949c | 55 | _RIGHT_WHEEL.speed(_SPEED*0.72); |
| Xiaofei | 3:97a5a3744481 | 56 | _LEFT_WHEEL.speed(_SPEED); |
| Xiaofei | 3:97a5a3744481 | 57 | } |
| Xiaofei | 0:4bc34a5fcc29 | 58 | } |
| Xiaofei | 0:4bc34a5fcc29 | 59 | |
| Xiaofei | 4:3fbe2d75f7eb | 60 | void StopCommand :: execute() |
| Xiaofei | 4:3fbe2d75f7eb | 61 | { |
| Xiaofei | 4:3fbe2d75f7eb | 62 | _RIGHT_WHEEL.speed(0.0); |
| Xiaofei | 4:3fbe2d75f7eb | 63 | _LEFT_WHEEL.speed(0.0); |
| Xiaofei | 4:3fbe2d75f7eb | 64 | _STBY = 0; |
| Xiaofei | 0:4bc34a5fcc29 | 65 | } |
| Xiaofei | 0:4bc34a5fcc29 | 66 |