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
00001 #include <mbed.h> 00002 #include "Roomba.h" 00003 00004 /** 00005 * データクラス 00006 * 基底のクラス定義のみ 00007 */ 00008 class Data { 00009 private: 00010 public: 00011 //virtual Data() 00012 //virtual ~Data() {} 00013 }; 00014 00015 00016 /** 00017 * 送信データクラス 00018 * データクラスから派生 00019 */ 00020 class SendData : public Data{ 00021 static const int LENGHT = 2; 00022 00023 private: 00024 int wheelVelocity[LENGHT]; 00025 00026 public: 00027 SendData() { 00028 } 00029 00030 SendData(int left, int right) { 00031 wheelVelocity[0] = left; 00032 wheelVelocity[1] = right; 00033 } 00034 00035 int get(int index) { 00036 int result = -1; 00037 00038 if (0 <= index && index < LENGHT) { 00039 result = wheelVelocity[index]; 00040 } 00041 return result; 00042 } 00043 }; 00044 00045 00046 /** 00047 * ルンバ制御クラスのインターフェース 00048 * 下記メソッドを継承先が実装すること 00049 */ 00050 class IRoomba { 00051 public: 00052 //virtual ~IRoomba(){};//なぜ宣言を書いたのか、コメントにした理由とともに忘れた 00053 //virtual void set(Data data) = 0; 00054 virtual bool send() = 0; 00055 }; 00056 00057 00058 /* 00059 * ルンバ制御クラスの実装クラス 00060 */ 00061 class ImplRoomba : protected Roomba, protected IRoomba { 00062 static const int COUNT_TIME_OUT = 10; 00063 00064 typedef Roomba base; 00065 00066 private: 00067 int count; //タイムアウト検出カウンタ(カウント値が一定数を超えればタイムアウト) 00068 bool isTimedOut;//タイムアウト検出フラグ 00069 DigitalOut _led;//notify()で使用するLED:ルンバ状態を通知する 00070 00071 SendData *_data;//送信データ 00072 00073 public: 00074 ImplRoomba(PinName tx, PinName rx, PinName led) : Roomba(tx, rx), _led(led), _data() { 00075 count = 0; 00076 isTimedOut = false; 00077 _led = 0; 00078 00079 //beseの持つSerialインスタンスにコールバックメソッドを接続する 00080 base::_s.attach(this, &ImplRoomba::serialReceiveCallback, Serial::RxIrq);//! 00081 } 00082 00083 00084 /* 00085 * @bref データ設定メソッド 00086 */ 00087 virtual void ImplRoomba::set(SendData data) { 00088 //Object of abstruct class type "ImplRoomba" is not allowed "ImplRoomba roomba(...);" 00089 _data = &data; 00090 00091 base::_s.printf("%d", (*_data).get(1)); 00092 00093 } 00094 00095 00096 /* 00097 * @bref 送信メソッド 00098 */ 00099 virtual bool ImplRoomba::send() { 00100 00101 base::start(); 00102 00103 base::mode(Roomba::Mode::Full); 00104 00105 int leftWheelVelocity = (*_data).get(0); 00106 int rightWheerVelocity = (*_data).get(1); 00107 base::drive (leftWheelVelocity, rightWheerVelocity); 00108 00109 return true; 00110 } 00111 00112 00113 /* 00114 * @bref 通信状態通知メソッド 00115 */ 00116 virtual void ImplRoomba::notify() { 00117 00118 if (isTimedOut == true) { 00119 _led = 1; 00120 } else { 00121 _led = 0; 00122 } 00123 } 00124 00125 00126 /** 00127 * @bref 周期タイマのコールバックメソッド 00128 */ 00129 virtual void ImplRoomba::periodicCallback() { 00130 00131 count = count + 1; 00132 00133 if (count > COUNT_TIME_OUT) { 00134 if (isTimedOut == false) { 00135 isTimedOut = true; 00136 ImplRoomba::notify(); 00137 } 00138 } 00139 00140 ImplRoomba::send(); 00141 } 00142 00143 /** 00144 * @bref シリアル受信時コールバックメソッド 00145 */ 00146 virtual void ImplRoomba::serialReceiveCallback() { 00147 count = 0; 00148 00149 _led = 1; 00150 base::_s.printf("hogehoge"); 00151 00152 if (isTimedOut) { 00153 isTimedOut = false; 00154 ImplRoomba::notify(); 00155 } 00156 } 00157 00158 }; 00159 00160 00161 ImplRoomba roomba(SERIAL_TX, SERIAL_RX, LED1); 00162 //Ticker ticker; 00163 //Serial pc(SERIAL_TX, SERIAL_RX); 00164 00165 DigitalOut debugLed(LED1); 00166 00167 int main() { 00168 int leftWheelVelocity = 0; 00169 int rightWheelVelocity = 0; 00170 00171 while (true) { 00172 00173 if (1) { 00174 //ルンバの走行値を更新 00175 leftWheelVelocity = -200; 00176 rightWheelVelocity = 500; 00177 00178 SendData::SendData *data = new SendData(leftWheelVelocity, rightWheelVelocity); 00179 roomba.set(*data); 00180 delete data; 00181 } 00182 00183 //ルンバへ定期的に走行値を送信 00184 roomba.periodicCallback(); 00185 00186 wait_ms(1000); 00187 } 00188 }
Generated on Sat Jul 16 2022 09:17:49 by
1.7.2