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@10:6e32b53f04c3, 2015-11-29 (annotated)
- Committer:
- Xiaofei
- Date:
- Sun Nov 29 00:02:54 2015 +0000
- Revision:
- 10:6e32b53f04c3
- Parent:
- 6:bbaabcc5206d
- Child:
- 11:6c56d8ca6e99
C
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 | 6:bbaabcc5206d | 36 | _LEFT_WHEEL.speed(-_SPEED*0.65); |
| Xiaofei | 3:97a5a3744481 | 37 | } |
| Xiaofei | 3:97a5a3744481 | 38 | else |
| Xiaofei | 3:97a5a3744481 | 39 | { |
| Xiaofei | 3:97a5a3744481 | 40 | _RIGHT_WHEEL.speed(_SPEED); |
| Xiaofei | 6:bbaabcc5206d | 41 | _LEFT_WHEEL.speed(_SPEED*0.65); |
| 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 | 6:bbaabcc5206d | 50 | _RIGHT_WHEEL.speed(-_SPEED*0.65); |
| Xiaofei | 3:97a5a3744481 | 51 | _LEFT_WHEEL.speed(-_SPEED); |
| Xiaofei | 3:97a5a3744481 | 52 | } |
| Xiaofei | 3:97a5a3744481 | 53 | else |
| Xiaofei | 3:97a5a3744481 | 54 | { |
| Xiaofei | 6:bbaabcc5206d | 55 | _RIGHT_WHEEL.speed(_SPEED*0.65); |
| Xiaofei | 3:97a5a3744481 | 56 | _LEFT_WHEEL.speed(_SPEED); |
| Xiaofei | 3:97a5a3744481 | 57 | } |
| Xiaofei | 0:4bc34a5fcc29 | 58 | } |
| Xiaofei | 0:4bc34a5fcc29 | 59 | |
| Xiaofei | 10:6e32b53f04c3 | 60 | void GoStraightCommand :: execute() |
| Xiaofei | 10:6e32b53f04c3 | 61 | { |
| Xiaofei | 10:6e32b53f04c3 | 62 | _STBY = 1; |
| Xiaofei | 10:6e32b53f04c3 | 63 | if(_IS_NEGATIVE) |
| Xiaofei | 10:6e32b53f04c3 | 64 | { |
| Xiaofei | 10:6e32b53f04c3 | 65 | _RIGHT_WHEEL.speed(-_SPEED); |
| Xiaofei | 10:6e32b53f04c3 | 66 | _LEFT_WHEEL.speed(-_SPEED); |
| Xiaofei | 10:6e32b53f04c3 | 67 | } |
| Xiaofei | 10:6e32b53f04c3 | 68 | else |
| Xiaofei | 10:6e32b53f04c3 | 69 | { |
| Xiaofei | 10:6e32b53f04c3 | 70 | _RIGHT_WHEEL.speed(_SPEED); |
| Xiaofei | 10:6e32b53f04c3 | 71 | _LEFT_WHEEL.speed(_SPEED); |
| Xiaofei | 10:6e32b53f04c3 | 72 | } |
| Xiaofei | 10:6e32b53f04c3 | 73 | } |
| Xiaofei | 10:6e32b53f04c3 | 74 | |
| Xiaofei | 4:3fbe2d75f7eb | 75 | void StopCommand :: execute() |
| Xiaofei | 4:3fbe2d75f7eb | 76 | { |
| Xiaofei | 4:3fbe2d75f7eb | 77 | _RIGHT_WHEEL.speed(0.0); |
| Xiaofei | 4:3fbe2d75f7eb | 78 | _LEFT_WHEEL.speed(0.0); |
| Xiaofei | 4:3fbe2d75f7eb | 79 | _STBY = 0; |
| Xiaofei | 0:4bc34a5fcc29 | 80 | } |
| Xiaofei | 0:4bc34a5fcc29 | 81 |