Yosuke Kirihata / Mbed 2 deprecated Nucleo_roomba

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
YosukeK
Date:
Sat Feb 14 11:32:14 2015 +0000
Parent:
1:aed433d882c9
Commit message:
????????????????????

Changed in this revision

Data.cpp Show annotated file Show diff for this revision Revisions of this file
Data.h Show annotated file Show diff for this revision Revisions of this file
EventArg.cpp Show diff for this revision Revisions of this file
EventArg.h Show diff for this revision Revisions of this file
Roomba.cpp Show annotated file Show diff for this revision Revisions of this file
Roomba.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Data.cpp	Sat Feb 14 11:32:14 2015 +0000
@@ -0,0 +1,34 @@
+#include <mbed.h>
+#include "Data.h"
+
+/**
+ * 
+ */
+Data::Data() {
+}
+
+Data::~Data() {
+
+}
+
+
+/**
+ *
+ */
+
+//LENGHT = 2;
+
+SendData::SendData(int left, int right) {
+    wheelVelocity[0] = left;
+    wheelVelocity[1] = right;
+}
+
+int get(int index) {
+    int result = -1;
+            
+//    if (0 <= index && index < 2) {
+//        result = wheelVelocity[index];
+//    }
+    return result;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Data.h	Sat Feb 14 11:32:14 2015 +0000
@@ -0,0 +1,43 @@
+#include <mbed.h>
+
+#ifndef __DATA_H_INCLUDED__
+#define __DATA_H_INCLUDED__
+
+/**
+ * データクラス
+ * 基底のクラス定義のみ
+ */
+class Data {
+    public:
+        Data();
+        ~Data();
+        
+    private:
+
+};
+
+#endif
+
+
+#ifndef __SENDDATA_H_INCLUDED__
+#define __SENDDATA_H_INCLUDED__
+
+/**
+ * 送信データクラス
+ * データクラスから派生
+ */
+class SendData : public Data {
+    static const int LENGHT = 2;
+    
+    public:
+        SendData();
+        SendData(int left, int right);
+        
+        int get(int index);
+        
+    private:
+        int wheelVelocity[LENGHT];
+        
+};
+
+#endif
\ No newline at end of file
--- a/EventArg.cpp	Wed Jan 21 16:20:12 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-#include "mbed.h"
-#include "EventArg.h"
-
-/**
- * 
- */
-EventArg::EventArg() {
-    leftWheetVelocity  = 0;
-    rightWheelVelocity = 0;
-}
-
-EventArg::~EventArg() {
-
-}
\ No newline at end of file
--- a/EventArg.h	Wed Jan 21 16:20:12 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-#include "mbed.h"
-
-#ifndef __EVENTARG_H_INCLUDED__
-#define __EVENTARG_H_INCLUDED__
-
-/**
- * 
- */
-class EventArg {
-    public:
-        EventArg();
-        ~EventArg();
-        
-    public:
-        int leftWheetVelocity;
-        int rightWheelVelocity;
-};
-
-#endif
\ No newline at end of file
--- a/Roomba.cpp	Wed Jan 21 16:20:12 2015 +0000
+++ b/Roomba.cpp	Sat Feb 14 11:32:14 2015 +0000
@@ -1,4 +1,4 @@
-#include "mbed.h"
+#include <mbed.h>
 #include "Roomba.h"
 
 /**
@@ -6,87 +6,14 @@
  * @param tx
  * @param rx
  */
-Roomba::Roomba(PinName tx, PinName rx, PinName led):
-    _s(tx, rx), _led(led){
-        count = 0;
-        timedOut = false;
-        _led = 0;
-        //_mode = Roomba::Mode::Off;
-    Roomba::mp = &Roomba::serialReceiveCallback;
-    _s.attach(this, Roomba::mp, Serial::RxIrq);
-}
-
-
-/**
- * @bref コマンド送信インターフェースの実装
- * @param e イベント引数
- * @return true
- */
-bool Roomba::send(EventArg e) {
-    
-    Roomba::start();
-    Roomba::mode(Roomba::Full);//(Roomba::Mode::Full); If write struct Mode and no name enum, Compiler say no sutable constructor conver to enum...
-    Roomba::drive(e.leftWheetVelocity, e.rightWheelVelocity);
-            
-    return true;
-}
-
-/**
- * @bref コマンド送信結果受信時のインターフェースの実装
- * @param result コマンド送信結果
- */
-void Roomba::notify(bool result) {
-    if (result == true) {
-        _led = 1;
-    } else {
-        _led = 0;    
-    }
+Roomba::Roomba(PinName tx, PinName rx):
+    _s(tx, rx) {
+    _mode = Roomba::Mode::Off;
 }
 
 
 /**
- * @bref 周期タイマのコールバックメソッド
- * @param e イベント引数
- */
-void Roomba::periodicCallback(EventArg e) {
-
-    count = count + 1;
-    
-    if (count > 10) {
-        if (timedOut == false) {
-            timedOut = true;
-            Roomba::notify(timedOut);
-        }    
-    }
-    
-    Roomba::send(e);
-    
-}
-
-/**
- * @bref シリアル受信時コールバックメソッド
- */
-void Roomba::serialReceiveCallback() {
-    count = 0;
-    
-    if (timedOut) {
-        timedOut = false;
-        Roomba::notify(timedOut);    
-    }
-}
-
-///**
-// * @bref シリアル受信のアタッチ
-// */
-////継承先から操作できるようにしただけ
-//void Roomba::attachSerialReceiveCallback() {
-//    Roomba::mp = &Roomba::serialReceiveCallback;
-//    _s.attach(this, Roomba::mp, Serial::RxIrq);
-//    //_s.attach(this, &Roomba::notify, Serial::RxIrq);
-//}
-
-/**
- * @bref Start
+ * @bref start
  * @return  true
  */
 bool Roomba::start() {
@@ -102,7 +29,7 @@
  * @param 操作モード
  * @return true
  */
-bool Roomba::mode(Roomba::Mode m) {
+bool Roomba::mode(Roomba::Mode::EMode m) {
 
     _s.putc(132);//Full;
     
@@ -111,36 +38,36 @@
 
 
 /**
- * @bref Start
+ * @bref 
  * @param true
  */
 bool Roomba::drive(int rightWheelVelocity, int leftWheelVelocity) {
     
+    bool ret = false;
     
-    bool ret = false;
+    char command[10];
     
     if ((-500 <= rightWheelVelocity && rightWheelVelocity <= 500)
         && (-500 <= leftWheelVelocity && leftWheelVelocity <= 500)) {
         
-        Roomba::command[0] = 145;
-        Roomba::command[1] = (rightWheelVelocity & 0xFF00) >> 8;  //MSB
-        Roomba::command[2] = rightWheelVelocity & 0xFF;           //LSB
-        Roomba::command[3] = (leftWheelVelocity  & 0xFF00) >> 8;  //MSB
-        Roomba::command[4] = leftWheelVelocity  & 0xFF;             //LSB
-        Roomba::command[5] = '\0';
+        command[0] = 145;
+        command[1] = (rightWheelVelocity & 0xFF00) >> 8;  //MSB
+        command[2] = rightWheelVelocity & 0xFF;           //LSB
+        command[3] = (leftWheelVelocity  & 0xFF00) >> 8;  //MSB
+        command[4] = leftWheelVelocity  & 0xFF;             //LSB
+        command[5] = '\0';
         
-        for (int i = 0; Roomba::command[i] != '\0'; i++) {
-            _s.putc(Roomba::command[i]);
+        for (int i = 0; i < 5; i++) {
+            _s.putc(command[i]);
         }
-        //_led = 0;
         ret = true;
     }
-    //_led = 1;
     return ret;
 }
 
 /**
  * @bref バッテリ状態
+ * 実装未完了
  * @param true
  * @return 
  * @retval 
--- a/Roomba.h	Wed Jan 21 16:20:12 2015 +0000
+++ b/Roomba.h	Sat Feb 14 11:32:14 2015 +0000
@@ -1,69 +1,39 @@
 #include "mbed.h"
-#include "EventArg.h"
+//#include "Data.h"
 
 #ifndef __ROOMBA_H_INCLUDED__
 #define __ROOMBA_H_INCLUDED__
 
-/**
- * ルンバ制御クラスのインターフェース
- * 下記メソッドを継承先が実装すること
- */
-class IRoomba {
-    public:
-        //virtual ~IRoomba(){};//なぜ宣言を書いたのか、コメントにした理由とともに忘れた
-        virtual bool send(EventArg e) = 0;
-        virtual void notify(bool result) = 0;
-};
 
 /**
  * ルンバ制御クラス
  */
-class Roomba : public IRoomba {
+class Roomba {
     public:
-        virtual bool send(EventArg e);
-        virtual void notify(bool result);
-        
-        //Mode.Fullのような書き方(またはそれに近い書き方がしたい)
-        //調べていたら、Roomba::Mode::Fullのように書くことできそうだとわかったので試したが、上手く行かない
-        //上手く行かない理由の考察はできていない
-        //http://stackoverflow.com/questions/8213696/accessing-enum-values-defined-in-a-struct
-        //struct Mode
-        //{
-            enum Mode
+        struct Mode
+        {
+            enum EMode
             {
                 Off,
-                Passive,//??
+                Passive,
                 Safe,
                 Full
             };
-        //};
-    
-    private:
-        Serial _s;
-        Roomba::Mode _mode;// = Roomba::Mode.Off;
-        //EventArg::EventArg _e;
-        DigitalOut _led;//Notify()で使用するLED:ルンバ状態を通知する
-        //void (Roomba::*serialReceiveCallback)(); //= notify;
-        void (Roomba::*mp)();//member function pointer
-        
-        char command[10];
+        };
+
+        Roomba(PinName tx, PinName rx);
         
-        int count;    //タイムアウト検出カウンタ(カウント値が一定数を超えればタイムアウト)
-        bool timedOut;//タイムアウト検出フラグ
-        
-    public:
-        Roomba(PinName tx, PinName rx, PinName led);
-        
-        void attachPeriodicCallback();
-        virtual void periodicCallback(EventArg e);
-        void attachSerialReceiveCallback();
-        void serialReceiveCallback();
-        
-        bool mode(Roomba::Mode m);
+        bool mode(Roomba::Mode::EMode m);
         //Mode getMode();
         bool start();
         bool drive(int rightWheelVelocity, int leftWheelVelocity);
         bool battery(int* batteryCapacity);
+
+    protected:
+        Serial _s;
+    
+    private:
+        Roomba::Mode::EMode _mode;
 };
 
 #endif
\ No newline at end of file
--- a/main.cpp	Wed Jan 21 16:20:12 2015 +0000
+++ b/main.cpp	Sat Feb 14 11:32:14 2015 +0000
@@ -1,106 +1,188 @@
-#include "mbed.h"
+#include <mbed.h>
 #include "Roomba.h"
-#include "EventArg.h"
+
+/**
+ * データクラス
+ * 基底のクラス定義のみ
+ */
+class Data {
+    private:
+    public:
+        //virtual Data()
+        //virtual ~Data() {}
+};
+
+
+/**
+ * 送信データクラス
+ * データクラスから派生
+ */
+class SendData : public Data{
+    static const int LENGHT = 2;
+    
+    private:
+        int wheelVelocity[LENGHT];
+        
+    public:
+        SendData() {
+        }
+    
+        SendData(int left, int right) {
+            wheelVelocity[0] = left;
+            wheelVelocity[1] = right;
+        }
+
+        int get(int index) {
+            int result = -1;
+            
+            if (0 <= index && index < LENGHT) {
+                result = wheelVelocity[index];
+            }
+            return result;
+        }
+};
+
+
+/**
+ * ルンバ制御クラスのインターフェース
+ * 下記メソッドを継承先が実装すること
+ */
+class IRoomba {
+    public:
+        //virtual ~IRoomba(){};//なぜ宣言を書いたのか、コメントにした理由とともに忘れた
+        //virtual void set(Data data) = 0;
+        virtual bool send() = 0;
+};
+
 
 /*
- * ルンバ向けイベント引数クラス
+ * ルンバ制御クラスの実装クラス
  */
-//class RoombaEventArg : public EventArg
-//{
-//    typedef EventArg base;
-//    
-//    public:
-//        //RoombaEventArg(int leftWheetVelocity, int rightWheelVelocity} {
-//            
-//        //}        
-//    
-//        RoombaEventArg() {
-//            leftWheetVelocity  = 0;
-//            rightWheelVelocity = 0;
-//        };
-//    
-//    public:
-//        int leftWheetVelocity;
-//        int rightWheelVelocity;
-//};
+class ImplRoomba : protected Roomba, protected IRoomba {
+    static const int COUNT_TIME_OUT = 10;
+    
+    typedef Roomba base;
+    
+    private:
+        int count;    //タイムアウト検出カウンタ(カウント値が一定数を超えればタイムアウト)
+        bool isTimedOut;//タイムアウト検出フラグ
+        DigitalOut _led;//notify()で使用するLED:ルンバ状態を通知する
+        
+        SendData *_data;//送信データ
+    
+    public:
+        ImplRoomba(PinName tx, PinName rx, PinName led) : Roomba(tx, rx), _led(led), _data() {
+            count = 0;
+            isTimedOut = false;
+            _led = 0;
 
-//
-///*
-// * ルンバ制御クラスの実装クラス
-// */
-//class ImplRoomba : protected Roomba {
-//    typedef Roomba base;
-//    
-//    private:
-//        DigitalOut _led;//Notify()で使用するLED:ルンバ状態を通知する
-//    
-//    public:
-//        ImplRoomba(PinName tx, PinName rx, PinName led) :Roomba(tx, rx), _led(led){
-//            _led = 0;
-//            Roomba::attachSerialReceiveCallback();
-//        }
-//        
-//        /*
-//         * @bref 送信メソッド
-//         */
-//        virtual bool ImplRoomba::send(RoombaEventArg e) {
-//            _led != _led;
-//            base::start();
-//            base::mode(Roomba::Full);//(Roomba::Mode::Full); If write struct Mode and no name enum, Compiler say no sutable constructor conver to enum...
-//            base::drive(e.leftWheetVelocity, e.rightWheelVelocity);
-//            
-//            return true;
-//        }
-//        
-//        /*
-//         * @bref 状態通知メソッド
-//         */        
-//        virtual void ImplRoomba::notify(bool result) {
-//            if (result == false) {
-//                _led = 1;
-//            } else {
-//                _led = 0;    
-//            }
-//        }
-//        
-//        /*
-//         * @bref 周期タイマコールバック
-//         */
-//        virtual void ImplRoomba::periodicCallback(RoombaEventArg e) {
-//            base::periodicCallback(e);
-//        }
-//};
+            //beseの持つSerialインスタンスにコールバックメソッドを接続する
+            base::_s.attach(this, &ImplRoomba::serialReceiveCallback, Serial::RxIrq);//!
+        }
+        
+        
+        /*
+         * @bref データ設定メソッド
+         */
+        virtual void ImplRoomba::set(SendData data) {
+           //Object of abstruct class type "ImplRoomba" is not allowed "ImplRoomba roomba(...);"
+           _data = &data;
+           
+           base::_s.printf("%d", (*_data).get(1));
+           
+        }
+        
+        
+        /*
+         * @bref 送信メソッド
+         */
+        virtual bool ImplRoomba::send() {
+            
+            base::start();
+            
+            base::mode(Roomba::Mode::Full);
+            
+            int leftWheelVelocity = (*_data).get(0);
+            int rightWheerVelocity = (*_data).get(1);
+            base::drive(leftWheelVelocity, rightWheerVelocity);
+            
+            return true;
+        }
+        
+        
+        /*
+         * @bref 通信状態通知メソッド
+         */
+        virtual void ImplRoomba::notify() {
+            
+            if (isTimedOut == true) {
+                _led = 1;
+            } else {
+                _led = 0;
+            }
+        }
+        
+        
+        /**
+         * @bref 周期タイマのコールバックメソッド
+         */
+        virtual void ImplRoomba::periodicCallback() {
+
+            count = count + 1;
+    
+            if (count > COUNT_TIME_OUT) {
+                if (isTimedOut == false) {
+                    isTimedOut = true;
+                    ImplRoomba::notify();
+                }
+            }
+
+            ImplRoomba::send();
+        }
+
+        /**
+         * @bref シリアル受信時コールバックメソッド
+         */
+        virtual void ImplRoomba::serialReceiveCallback() {
+            count = 0;
+            
+            _led = 1;
+            base::_s.printf("hogehoge");
+
+            if (isTimedOut) {
+                isTimedOut = false;
+                ImplRoomba::notify();
+            }
+        }
+        
+};
 
 
-Ticker ticker;
+ImplRoomba roomba(SERIAL_TX, SERIAL_RX, LED1);
+//Ticker ticker;
+//Serial pc(SERIAL_TX, SERIAL_RX);
 
-Serial pc(SERIAL_TX, SERIAL_RX);
+DigitalOut debugLed(LED1);
 
-//匿名クラスでオーバーライドした書き方がしたい
-//Roomba
-Roomba roomba(SERIAL_TX, SERIAL_RX, LED1);/* = new Roomba: IRoomba {
-    bool Roomba::send() {
-        //start, mode, drive
-        return true;
-    }
-    void Roomba::notify() {}
-};*/
+int main() {
+    int leftWheelVelocity = 0;
+    int rightWheelVelocity = 0;
+
+    while (true) {        
 
-//Ticker no kawari.
-int main() {
-    //ticker.attach(roomba.periodicCallback, 0.1);
-    
-    //TODO:Serial receive message.
-
-    int leftWheelVelocity = 500;
-    int rightWheelVelocity = -500;
-
-    EventArg e;//(leftWheelVelocity, rightWheelVelocity);
-    e.leftWheetVelocity = leftWheelVelocity;
-    e.rightWheelVelocity = rightWheelVelocity;
-    
-    while (true) {    
-        roomba.periodicCallback(e);
-        wait_ms(100);
+        if (1) {
+            //ルンバの走行値を更新
+            leftWheelVelocity = -200;
+            rightWheelVelocity = 500;
+            
+            SendData::SendData *data = new SendData(leftWheelVelocity, rightWheelVelocity);
+            roomba.set(*data);
+            delete data;
+        }
+        
+        //ルンバへ定期的に走行値を送信
+        roomba.periodicCallback();
+        
+        wait_ms(1000);
     }
 }
\ No newline at end of file