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@11:6c56d8ca6e99, 2015-11-29 (annotated)
- Committer:
- Xiaofei
- Date:
- Sun Nov 29 00:15:41 2015 +0000
- Revision:
- 11:6c56d8ca6e99
- Parent:
- 10:6e32b53f04c3
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 | 11:6c56d8ca6e99 | 18 | #define SENSITIVITY 0.9 |
| Xiaofei | 11:6c56d8ca6e99 | 19 | |
| Xiaofei | 3:97a5a3744481 | 20 | void Command::setSpeed(const std::int8_t& sp, const std::int8_t& is_negative) |
| Xiaofei | 1:13c4bf8989d5 | 21 | { |
| Xiaofei | 1:13c4bf8989d5 | 22 | _STBY = 1; |
| Xiaofei | 3:97a5a3744481 | 23 | _SPEED = sp * 0.33; |
| Xiaofei | 3:97a5a3744481 | 24 | _IS_NEGATIVE = is_negative; |
| Xiaofei | 1:13c4bf8989d5 | 25 | } |
| Xiaofei | 0:4bc34a5fcc29 | 26 | |
| Xiaofei | 0:4bc34a5fcc29 | 27 | void LedCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 28 | { |
| Xiaofei | 1:13c4bf8989d5 | 29 | _LED1 = !_LED1; |
| Xiaofei | 0:4bc34a5fcc29 | 30 | } |
| Xiaofei | 0:4bc34a5fcc29 | 31 | |
| Xiaofei | 0:4bc34a5fcc29 | 32 | void TurnLeftCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 33 | { |
| Xiaofei | 1:13c4bf8989d5 | 34 | _STBY = 1; |
| Xiaofei | 3:97a5a3744481 | 35 | if(_IS_NEGATIVE) |
| Xiaofei | 3:97a5a3744481 | 36 | { |
| Xiaofei | 3:97a5a3744481 | 37 | _RIGHT_WHEEL.speed(-_SPEED); |
| Xiaofei | 11:6c56d8ca6e99 | 38 | _LEFT_WHEEL.speed(-_SPEED*SENSITIVITY); |
| Xiaofei | 3:97a5a3744481 | 39 | } |
| Xiaofei | 3:97a5a3744481 | 40 | else |
| Xiaofei | 3:97a5a3744481 | 41 | { |
| Xiaofei | 3:97a5a3744481 | 42 | _RIGHT_WHEEL.speed(_SPEED); |
| Xiaofei | 11:6c56d8ca6e99 | 43 | _LEFT_WHEEL.speed(_SPEED*SENSITIVITY); |
| Xiaofei | 3:97a5a3744481 | 44 | } |
| Xiaofei | 0:4bc34a5fcc29 | 45 | } |
| Xiaofei | 0:4bc34a5fcc29 | 46 | |
| Xiaofei | 0:4bc34a5fcc29 | 47 | void TurnRightCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 48 | { |
| Xiaofei | 1:13c4bf8989d5 | 49 | _STBY = 1; |
| Xiaofei | 3:97a5a3744481 | 50 | if(_IS_NEGATIVE) |
| Xiaofei | 3:97a5a3744481 | 51 | { |
| Xiaofei | 11:6c56d8ca6e99 | 52 | _RIGHT_WHEEL.speed(-_SPEED*SENSITIVITY); |
| Xiaofei | 3:97a5a3744481 | 53 | _LEFT_WHEEL.speed(-_SPEED); |
| Xiaofei | 3:97a5a3744481 | 54 | } |
| Xiaofei | 3:97a5a3744481 | 55 | else |
| Xiaofei | 3:97a5a3744481 | 56 | { |
| Xiaofei | 11:6c56d8ca6e99 | 57 | _RIGHT_WHEEL.speed(_SPEED*SENSITIVITY); |
| Xiaofei | 3:97a5a3744481 | 58 | _LEFT_WHEEL.speed(_SPEED); |
| Xiaofei | 3:97a5a3744481 | 59 | } |
| Xiaofei | 0:4bc34a5fcc29 | 60 | } |
| Xiaofei | 0:4bc34a5fcc29 | 61 | |
| Xiaofei | 10:6e32b53f04c3 | 62 | void GoStraightCommand :: execute() |
| Xiaofei | 10:6e32b53f04c3 | 63 | { |
| Xiaofei | 10:6e32b53f04c3 | 64 | _STBY = 1; |
| Xiaofei | 10:6e32b53f04c3 | 65 | if(_IS_NEGATIVE) |
| Xiaofei | 10:6e32b53f04c3 | 66 | { |
| Xiaofei | 10:6e32b53f04c3 | 67 | _RIGHT_WHEEL.speed(-_SPEED); |
| Xiaofei | 10:6e32b53f04c3 | 68 | _LEFT_WHEEL.speed(-_SPEED); |
| Xiaofei | 10:6e32b53f04c3 | 69 | } |
| Xiaofei | 10:6e32b53f04c3 | 70 | else |
| Xiaofei | 10:6e32b53f04c3 | 71 | { |
| Xiaofei | 10:6e32b53f04c3 | 72 | _RIGHT_WHEEL.speed(_SPEED); |
| Xiaofei | 10:6e32b53f04c3 | 73 | _LEFT_WHEEL.speed(_SPEED); |
| Xiaofei | 10:6e32b53f04c3 | 74 | } |
| Xiaofei | 10:6e32b53f04c3 | 75 | } |
| Xiaofei | 10:6e32b53f04c3 | 76 | |
| Xiaofei | 4:3fbe2d75f7eb | 77 | void StopCommand :: execute() |
| Xiaofei | 4:3fbe2d75f7eb | 78 | { |
| Xiaofei | 4:3fbe2d75f7eb | 79 | _RIGHT_WHEEL.speed(0.0); |
| Xiaofei | 4:3fbe2d75f7eb | 80 | _LEFT_WHEEL.speed(0.0); |
| Xiaofei | 4:3fbe2d75f7eb | 81 | _STBY = 0; |
| Xiaofei | 0:4bc34a5fcc29 | 82 | } |
| Xiaofei | 0:4bc34a5fcc29 | 83 |