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.
main.cpp@2:144ca2f5d850, 2015-02-14 (annotated)
- Committer:
- YosukeK
- Date:
- Sat Feb 14 11:32:14 2015 +0000
- Revision:
- 2:144ca2f5d850
- Parent:
- 1:aed433d882c9
????????????????????
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| YosukeK | 2:144ca2f5d850 | 1 | #include <mbed.h> |
| YosukeK | 0:32eb0835b5a3 | 2 | #include "Roomba.h" |
| YosukeK | 2:144ca2f5d850 | 3 | |
| YosukeK | 2:144ca2f5d850 | 4 | /** |
| YosukeK | 2:144ca2f5d850 | 5 | * データクラス |
| YosukeK | 2:144ca2f5d850 | 6 | * 基底のクラス定義のみ |
| YosukeK | 2:144ca2f5d850 | 7 | */ |
| YosukeK | 2:144ca2f5d850 | 8 | class Data { |
| YosukeK | 2:144ca2f5d850 | 9 | private: |
| YosukeK | 2:144ca2f5d850 | 10 | public: |
| YosukeK | 2:144ca2f5d850 | 11 | //virtual Data() |
| YosukeK | 2:144ca2f5d850 | 12 | //virtual ~Data() {} |
| YosukeK | 2:144ca2f5d850 | 13 | }; |
| YosukeK | 2:144ca2f5d850 | 14 | |
| YosukeK | 2:144ca2f5d850 | 15 | |
| YosukeK | 2:144ca2f5d850 | 16 | /** |
| YosukeK | 2:144ca2f5d850 | 17 | * 送信データクラス |
| YosukeK | 2:144ca2f5d850 | 18 | * データクラスから派生 |
| YosukeK | 2:144ca2f5d850 | 19 | */ |
| YosukeK | 2:144ca2f5d850 | 20 | class SendData : public Data{ |
| YosukeK | 2:144ca2f5d850 | 21 | static const int LENGHT = 2; |
| YosukeK | 2:144ca2f5d850 | 22 | |
| YosukeK | 2:144ca2f5d850 | 23 | private: |
| YosukeK | 2:144ca2f5d850 | 24 | int wheelVelocity[LENGHT]; |
| YosukeK | 2:144ca2f5d850 | 25 | |
| YosukeK | 2:144ca2f5d850 | 26 | public: |
| YosukeK | 2:144ca2f5d850 | 27 | SendData() { |
| YosukeK | 2:144ca2f5d850 | 28 | } |
| YosukeK | 2:144ca2f5d850 | 29 | |
| YosukeK | 2:144ca2f5d850 | 30 | SendData(int left, int right) { |
| YosukeK | 2:144ca2f5d850 | 31 | wheelVelocity[0] = left; |
| YosukeK | 2:144ca2f5d850 | 32 | wheelVelocity[1] = right; |
| YosukeK | 2:144ca2f5d850 | 33 | } |
| YosukeK | 2:144ca2f5d850 | 34 | |
| YosukeK | 2:144ca2f5d850 | 35 | int get(int index) { |
| YosukeK | 2:144ca2f5d850 | 36 | int result = -1; |
| YosukeK | 2:144ca2f5d850 | 37 | |
| YosukeK | 2:144ca2f5d850 | 38 | if (0 <= index && index < LENGHT) { |
| YosukeK | 2:144ca2f5d850 | 39 | result = wheelVelocity[index]; |
| YosukeK | 2:144ca2f5d850 | 40 | } |
| YosukeK | 2:144ca2f5d850 | 41 | return result; |
| YosukeK | 2:144ca2f5d850 | 42 | } |
| YosukeK | 2:144ca2f5d850 | 43 | }; |
| YosukeK | 2:144ca2f5d850 | 44 | |
| YosukeK | 2:144ca2f5d850 | 45 | |
| YosukeK | 2:144ca2f5d850 | 46 | /** |
| YosukeK | 2:144ca2f5d850 | 47 | * ルンバ制御クラスのインターフェース |
| YosukeK | 2:144ca2f5d850 | 48 | * 下記メソッドを継承先が実装すること |
| YosukeK | 2:144ca2f5d850 | 49 | */ |
| YosukeK | 2:144ca2f5d850 | 50 | class IRoomba { |
| YosukeK | 2:144ca2f5d850 | 51 | public: |
| YosukeK | 2:144ca2f5d850 | 52 | //virtual ~IRoomba(){};//なぜ宣言を書いたのか、コメントにした理由とともに忘れた |
| YosukeK | 2:144ca2f5d850 | 53 | //virtual void set(Data data) = 0; |
| YosukeK | 2:144ca2f5d850 | 54 | virtual bool send() = 0; |
| YosukeK | 2:144ca2f5d850 | 55 | }; |
| YosukeK | 2:144ca2f5d850 | 56 | |
| YosukeK | 0:32eb0835b5a3 | 57 | |
| YosukeK | 0:32eb0835b5a3 | 58 | /* |
| YosukeK | 2:144ca2f5d850 | 59 | * ルンバ制御クラスの実装クラス |
| YosukeK | 0:32eb0835b5a3 | 60 | */ |
| YosukeK | 2:144ca2f5d850 | 61 | class ImplRoomba : protected Roomba, protected IRoomba { |
| YosukeK | 2:144ca2f5d850 | 62 | static const int COUNT_TIME_OUT = 10; |
| YosukeK | 2:144ca2f5d850 | 63 | |
| YosukeK | 2:144ca2f5d850 | 64 | typedef Roomba base; |
| YosukeK | 2:144ca2f5d850 | 65 | |
| YosukeK | 2:144ca2f5d850 | 66 | private: |
| YosukeK | 2:144ca2f5d850 | 67 | int count; //タイムアウト検出カウンタ(カウント値が一定数を超えればタイムアウト) |
| YosukeK | 2:144ca2f5d850 | 68 | bool isTimedOut;//タイムアウト検出フラグ |
| YosukeK | 2:144ca2f5d850 | 69 | DigitalOut _led;//notify()で使用するLED:ルンバ状態を通知する |
| YosukeK | 2:144ca2f5d850 | 70 | |
| YosukeK | 2:144ca2f5d850 | 71 | SendData *_data;//送信データ |
| YosukeK | 2:144ca2f5d850 | 72 | |
| YosukeK | 2:144ca2f5d850 | 73 | public: |
| YosukeK | 2:144ca2f5d850 | 74 | ImplRoomba(PinName tx, PinName rx, PinName led) : Roomba(tx, rx), _led(led), _data() { |
| YosukeK | 2:144ca2f5d850 | 75 | count = 0; |
| YosukeK | 2:144ca2f5d850 | 76 | isTimedOut = false; |
| YosukeK | 2:144ca2f5d850 | 77 | _led = 0; |
| YosukeK | 0:32eb0835b5a3 | 78 | |
| YosukeK | 2:144ca2f5d850 | 79 | //beseの持つSerialインスタンスにコールバックメソッドを接続する |
| YosukeK | 2:144ca2f5d850 | 80 | base::_s.attach(this, &ImplRoomba::serialReceiveCallback, Serial::RxIrq);//! |
| YosukeK | 2:144ca2f5d850 | 81 | } |
| YosukeK | 2:144ca2f5d850 | 82 | |
| YosukeK | 2:144ca2f5d850 | 83 | |
| YosukeK | 2:144ca2f5d850 | 84 | /* |
| YosukeK | 2:144ca2f5d850 | 85 | * @bref データ設定メソッド |
| YosukeK | 2:144ca2f5d850 | 86 | */ |
| YosukeK | 2:144ca2f5d850 | 87 | virtual void ImplRoomba::set(SendData data) { |
| YosukeK | 2:144ca2f5d850 | 88 | //Object of abstruct class type "ImplRoomba" is not allowed "ImplRoomba roomba(...);" |
| YosukeK | 2:144ca2f5d850 | 89 | _data = &data; |
| YosukeK | 2:144ca2f5d850 | 90 | |
| YosukeK | 2:144ca2f5d850 | 91 | base::_s.printf("%d", (*_data).get(1)); |
| YosukeK | 2:144ca2f5d850 | 92 | |
| YosukeK | 2:144ca2f5d850 | 93 | } |
| YosukeK | 2:144ca2f5d850 | 94 | |
| YosukeK | 2:144ca2f5d850 | 95 | |
| YosukeK | 2:144ca2f5d850 | 96 | /* |
| YosukeK | 2:144ca2f5d850 | 97 | * @bref 送信メソッド |
| YosukeK | 2:144ca2f5d850 | 98 | */ |
| YosukeK | 2:144ca2f5d850 | 99 | virtual bool ImplRoomba::send() { |
| YosukeK | 2:144ca2f5d850 | 100 | |
| YosukeK | 2:144ca2f5d850 | 101 | base::start(); |
| YosukeK | 2:144ca2f5d850 | 102 | |
| YosukeK | 2:144ca2f5d850 | 103 | base::mode(Roomba::Mode::Full); |
| YosukeK | 2:144ca2f5d850 | 104 | |
| YosukeK | 2:144ca2f5d850 | 105 | int leftWheelVelocity = (*_data).get(0); |
| YosukeK | 2:144ca2f5d850 | 106 | int rightWheerVelocity = (*_data).get(1); |
| YosukeK | 2:144ca2f5d850 | 107 | base::drive(leftWheelVelocity, rightWheerVelocity); |
| YosukeK | 2:144ca2f5d850 | 108 | |
| YosukeK | 2:144ca2f5d850 | 109 | return true; |
| YosukeK | 2:144ca2f5d850 | 110 | } |
| YosukeK | 2:144ca2f5d850 | 111 | |
| YosukeK | 2:144ca2f5d850 | 112 | |
| YosukeK | 2:144ca2f5d850 | 113 | /* |
| YosukeK | 2:144ca2f5d850 | 114 | * @bref 通信状態通知メソッド |
| YosukeK | 2:144ca2f5d850 | 115 | */ |
| YosukeK | 2:144ca2f5d850 | 116 | virtual void ImplRoomba::notify() { |
| YosukeK | 2:144ca2f5d850 | 117 | |
| YosukeK | 2:144ca2f5d850 | 118 | if (isTimedOut == true) { |
| YosukeK | 2:144ca2f5d850 | 119 | _led = 1; |
| YosukeK | 2:144ca2f5d850 | 120 | } else { |
| YosukeK | 2:144ca2f5d850 | 121 | _led = 0; |
| YosukeK | 2:144ca2f5d850 | 122 | } |
| YosukeK | 2:144ca2f5d850 | 123 | } |
| YosukeK | 2:144ca2f5d850 | 124 | |
| YosukeK | 2:144ca2f5d850 | 125 | |
| YosukeK | 2:144ca2f5d850 | 126 | /** |
| YosukeK | 2:144ca2f5d850 | 127 | * @bref 周期タイマのコールバックメソッド |
| YosukeK | 2:144ca2f5d850 | 128 | */ |
| YosukeK | 2:144ca2f5d850 | 129 | virtual void ImplRoomba::periodicCallback() { |
| YosukeK | 2:144ca2f5d850 | 130 | |
| YosukeK | 2:144ca2f5d850 | 131 | count = count + 1; |
| YosukeK | 2:144ca2f5d850 | 132 | |
| YosukeK | 2:144ca2f5d850 | 133 | if (count > COUNT_TIME_OUT) { |
| YosukeK | 2:144ca2f5d850 | 134 | if (isTimedOut == false) { |
| YosukeK | 2:144ca2f5d850 | 135 | isTimedOut = true; |
| YosukeK | 2:144ca2f5d850 | 136 | ImplRoomba::notify(); |
| YosukeK | 2:144ca2f5d850 | 137 | } |
| YosukeK | 2:144ca2f5d850 | 138 | } |
| YosukeK | 2:144ca2f5d850 | 139 | |
| YosukeK | 2:144ca2f5d850 | 140 | ImplRoomba::send(); |
| YosukeK | 2:144ca2f5d850 | 141 | } |
| YosukeK | 2:144ca2f5d850 | 142 | |
| YosukeK | 2:144ca2f5d850 | 143 | /** |
| YosukeK | 2:144ca2f5d850 | 144 | * @bref シリアル受信時コールバックメソッド |
| YosukeK | 2:144ca2f5d850 | 145 | */ |
| YosukeK | 2:144ca2f5d850 | 146 | virtual void ImplRoomba::serialReceiveCallback() { |
| YosukeK | 2:144ca2f5d850 | 147 | count = 0; |
| YosukeK | 2:144ca2f5d850 | 148 | |
| YosukeK | 2:144ca2f5d850 | 149 | _led = 1; |
| YosukeK | 2:144ca2f5d850 | 150 | base::_s.printf("hogehoge"); |
| YosukeK | 2:144ca2f5d850 | 151 | |
| YosukeK | 2:144ca2f5d850 | 152 | if (isTimedOut) { |
| YosukeK | 2:144ca2f5d850 | 153 | isTimedOut = false; |
| YosukeK | 2:144ca2f5d850 | 154 | ImplRoomba::notify(); |
| YosukeK | 2:144ca2f5d850 | 155 | } |
| YosukeK | 2:144ca2f5d850 | 156 | } |
| YosukeK | 2:144ca2f5d850 | 157 | |
| YosukeK | 2:144ca2f5d850 | 158 | }; |
| YosukeK | 0:32eb0835b5a3 | 159 | |
| YosukeK | 0:32eb0835b5a3 | 160 | |
| YosukeK | 2:144ca2f5d850 | 161 | ImplRoomba roomba(SERIAL_TX, SERIAL_RX, LED1); |
| YosukeK | 2:144ca2f5d850 | 162 | //Ticker ticker; |
| YosukeK | 2:144ca2f5d850 | 163 | //Serial pc(SERIAL_TX, SERIAL_RX); |
| YosukeK | 0:32eb0835b5a3 | 164 | |
| YosukeK | 2:144ca2f5d850 | 165 | DigitalOut debugLed(LED1); |
| YosukeK | 0:32eb0835b5a3 | 166 | |
| YosukeK | 2:144ca2f5d850 | 167 | int main() { |
| YosukeK | 2:144ca2f5d850 | 168 | int leftWheelVelocity = 0; |
| YosukeK | 2:144ca2f5d850 | 169 | int rightWheelVelocity = 0; |
| YosukeK | 2:144ca2f5d850 | 170 | |
| YosukeK | 2:144ca2f5d850 | 171 | while (true) { |
| YosukeK | 0:32eb0835b5a3 | 172 | |
| YosukeK | 2:144ca2f5d850 | 173 | if (1) { |
| YosukeK | 2:144ca2f5d850 | 174 | //ルンバの走行値を更新 |
| YosukeK | 2:144ca2f5d850 | 175 | leftWheelVelocity = -200; |
| YosukeK | 2:144ca2f5d850 | 176 | rightWheelVelocity = 500; |
| YosukeK | 2:144ca2f5d850 | 177 | |
| YosukeK | 2:144ca2f5d850 | 178 | SendData::SendData *data = new SendData(leftWheelVelocity, rightWheelVelocity); |
| YosukeK | 2:144ca2f5d850 | 179 | roomba.set(*data); |
| YosukeK | 2:144ca2f5d850 | 180 | delete data; |
| YosukeK | 2:144ca2f5d850 | 181 | } |
| YosukeK | 2:144ca2f5d850 | 182 | |
| YosukeK | 2:144ca2f5d850 | 183 | //ルンバへ定期的に走行値を送信 |
| YosukeK | 2:144ca2f5d850 | 184 | roomba.periodicCallback(); |
| YosukeK | 2:144ca2f5d850 | 185 | |
| YosukeK | 2:144ca2f5d850 | 186 | wait_ms(1000); |
| YosukeK | 0:32eb0835b5a3 | 187 | } |
| YosukeK | 0:32eb0835b5a3 | 188 | } |