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@2:3d1c13d63966, 2015-11-28 (annotated)
- Committer:
- Xiaofei
- Date:
- Sat Nov 28 00:57:26 2015 +0000
- Revision:
- 2:3d1c13d63966
- Parent:
- 1:13c4bf8989d5
- Child:
- 3:97a5a3744481
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 | 2:3d1c13d63966 | 6 | Motor _RIGHT_WHEEL(p25, p6, p5); // Motor A pwm, fwd, rev |
| Xiaofei | 2:3d1c13d63966 | 7 | Motor _LEFT_WHEEL(p26, p7, p8); // Motor B pwm, fwd, rev |
| Xiaofei | 2:3d1c13d63966 | 8 | DigitalOut _STBY(p11); // 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 | 1:13c4bf8989d5 | 15 | Command::Command():_LEFT_SPEED(0),_RIGHT_SPEED(0) |
| Xiaofei | 1:13c4bf8989d5 | 16 | {} |
| Xiaofei | 1:13c4bf8989d5 | 17 | |
| Xiaofei | 1:13c4bf8989d5 | 18 | void Command::setSpeed(const std::int8_t& l_sp,const std::int8_t& r_sp) |
| Xiaofei | 1:13c4bf8989d5 | 19 | { |
| Xiaofei | 1:13c4bf8989d5 | 20 | _STBY = 1; |
| Xiaofei | 1:13c4bf8989d5 | 21 | _LEFT_SPEED = l_sp; |
| Xiaofei | 1:13c4bf8989d5 | 22 | _RIGHT_SPEED = r_sp; |
| 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 | 1:13c4bf8989d5 | 33 | _RIGHT_WHEEL.speed(_RIGHT_SPEED); |
| Xiaofei | 1:13c4bf8989d5 | 34 | _LEFT_WHEEL.speed(_LEFT_SPEED); |
| Xiaofei | 0:4bc34a5fcc29 | 35 | } |
| Xiaofei | 0:4bc34a5fcc29 | 36 | |
| Xiaofei | 0:4bc34a5fcc29 | 37 | void TurnRightCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 38 | { |
| Xiaofei | 1:13c4bf8989d5 | 39 | _STBY = 1; |
| Xiaofei | 1:13c4bf8989d5 | 40 | _RIGHT_WHEEL.speed(_RIGHT_SPEED); |
| Xiaofei | 1:13c4bf8989d5 | 41 | _LEFT_WHEEL.speed(_LEFT_SPEED); |
| Xiaofei | 0:4bc34a5fcc29 | 42 | } |
| Xiaofei | 0:4bc34a5fcc29 | 43 | |
| Xiaofei | 0:4bc34a5fcc29 | 44 | void MoveForwardCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 45 | { |
| Xiaofei | 1:13c4bf8989d5 | 46 | _STBY = 1; |
| Xiaofei | 1:13c4bf8989d5 | 47 | _RIGHT_WHEEL.speed(0.5); |
| Xiaofei | 1:13c4bf8989d5 | 48 | _LEFT_WHEEL.speed(0.5); |
| Xiaofei | 0:4bc34a5fcc29 | 49 | } |
| Xiaofei | 0:4bc34a5fcc29 | 50 | |
| Xiaofei | 0:4bc34a5fcc29 | 51 | void MoveBackwardCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 52 | { |
| Xiaofei | 1:13c4bf8989d5 | 53 | |
| Xiaofei | 1:13c4bf8989d5 | 54 | _STBY = 1; |
| Xiaofei | 1:13c4bf8989d5 | 55 | _RIGHT_WHEEL.speed(_RIGHT_SPEED); |
| Xiaofei | 1:13c4bf8989d5 | 56 | _LEFT_WHEEL.speed(_LEFT_SPEED); |
| Xiaofei | 0:4bc34a5fcc29 | 57 | } |
| Xiaofei | 0:4bc34a5fcc29 | 58 | |
| Xiaofei | 0:4bc34a5fcc29 | 59 | void StopCommand :: execute() |
| Xiaofei | 0:4bc34a5fcc29 | 60 | { |
| Xiaofei | 1:13c4bf8989d5 | 61 | _RIGHT_WHEEL.speed(_RIGHT_SPEED); |
| Xiaofei | 1:13c4bf8989d5 | 62 | _LEFT_WHEEL.speed(_LEFT_SPEED); |
| Xiaofei | 0:4bc34a5fcc29 | 63 | } |
| Xiaofei | 0:4bc34a5fcc29 | 64 |