交流ロボコン

Dependencies:   FEP

Dependents:   kourobo_Controller_FEP199_198 kourobo_controller_FEP209_208 kourobo2019 kouroboA_2019 ... more

Files at this revision

API Documentation at this revision

Comitter:
number_key
Date:
Mon Jan 15 09:21:59 2018 +0000
Commit message:
compactControllerReciever

Changed in this revision

FEP.lib Show annotated file Show diff for this revision Revisions of this file
controller.cpp Show annotated file Show diff for this revision Revisions of this file
controller.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 875532e626ee FEP.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FEP.lib	Mon Jan 15 09:21:59 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/NHK-Robocon2016_Nagaoka_B_Team/code/FEP/#2d308c99c668
diff -r 000000000000 -r 875532e626ee controller.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/controller.cpp	Mon Jan 15 09:21:59 2018 +0000
@@ -0,0 +1,80 @@
+#include "controller.h"
+
+Controller::Controller(PinName FEPtx, PinName FEPrx, int addr) :
+    FEP(FEPtx, FEPrx, addr),
+    data(),
+    fepTemp(0),
+    button1(),
+    button2(),
+    stick(),
+    radian(),
+    norm()
+{
+}
+
+bool Controller::receiveState()
+{
+    fepTemp = FEP::read(data, DATA_SIZE);
+    if(fepTemp == FEP_SUCCESS) {
+        for(int i = 0; i < 7; i++) {
+            button1[i] = data[4] % 2;
+            data[4] /= 2;
+        }
+        for(int i=0; i<6; i++) {
+            button2[i] = data[5] % 2;
+            data[5] /= 2;
+        }
+        for(int i = 0; i < 4; i++) {
+            stick[i] = -((double)(data[i] / STICK_DIVIDE) * 2.0 - 1.0);
+        }
+        setStick();
+    } else if(fepTemp == FEP_NO_RESPONSE) {
+        return 0;
+    } else {
+        return 0;
+    }
+    return 1;
+}
+
+void Controller::setStick()
+{
+    for(int i = 0; i < 4; i++) {
+        if(stick[i] < STICK_NEWTRAL && stick[i] > -STICK_NEWTRAL) stick[i] = 0;
+    }
+
+    radian[0] = atan2(stick[1], -stick[0]);
+    radian[1] = atan2(stick[3], -stick[2]);
+
+    norm[0] = hypot(stick[0], stick[1]);
+    norm[1] = hypot(stick[2], stick[3]);
+
+    if(norm[0] < STICK_NEWTRAL) norm[0] = 0;
+    if(norm[1] < STICK_NEWTRAL) norm[1] = 0;
+    if(norm[0] > STICK_NORM_MAX) norm[0] = STICK_NORM_MAX;
+    if(norm[1] > STICK_NORM_MAX) norm[1] = STICK_NORM_MAX;
+}
+
+bool Controller::getButton1(int number) const
+{
+    return button1[number];
+}
+
+bool Controller::getButton2(int number) const
+{
+    return button2[number];
+}
+
+float Controller::getStick(int number) const
+{
+    return stick[number];
+}
+
+float Controller::getRadian(int number) const
+{
+    return radian[number];
+}
+
+float Controller::getNorm(int number) const
+{
+    return norm[number];
+}
diff -r 000000000000 -r 875532e626ee controller.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/controller.h	Mon Jan 15 09:21:59 2018 +0000
@@ -0,0 +1,111 @@
+/**
+* @file controller.h
+* @brief FEPを使ったコントローラ受信部
+*
+* Example :
+* @code
+* #include "mbed.h"
+* #include "controller.h"
+*
+* Controller pad(PA_9, PA_10, 200);
+* Serial pc(USBTX, USBRX, 115200);
+*
+* int main()
+* {
+*     while(1) {
+*         if(pad.receiveState()) {
+*             for(int i = 0; i < 7; i++) pc.printf("%d, ", pad.getButton1(i));
+*             for(int i = 0; i < 6; i++) pc.printf("%d, ", pad.getButton2(i));
+*             pc.printf("\r\n");
+*         } else {
+*             pc.printf("ERROR\n\r");
+*         }
+*     }
+* }
+* @endcode
+*/
+#ifndef CONTROLLER_H
+#define CONTROLLER_H
+
+#include "mbed.h"
+#include <math.h>
+
+#include "FEP.h"
+
+// const double M_PI =     3.141592653589793;
+const int ADDR =        203;
+const bool FEP_SUCCESS =0;
+const int DATA_SIZE =   6;
+const float STICK_DIVIDE =  255.0;
+const float STICK_NEWTRAL = 0.1;
+const float STICK_NORM_MAX =1.0;
+
+/**
+* @brief FEPを使ったコントローラのクラス
+*/
+class Controller : public FEP
+{
+public :
+    /**
+     * @brief コンストラクタ
+     * @param FEPtx FEPtx
+     * @param FEPrx FEPrx
+     * @param addr  address
+     */
+    Controller(PinName FEPtx, PinName FEPrx,int addr);
+
+    /**
+    * @brief メンバ変数にボタンのステートを格納
+    */
+    bool receiveState();
+
+    /**
+     * ボタン1の状態を取得
+     * @param  number button number
+     * @return        status
+     */
+    bool getButton1(int number) const;
+
+    /**
+     * ボタン2の状態を取得
+     * @param  number button number
+     * @return        status
+     */
+    bool getButton2(int number) const;
+
+    /**
+     * スティックの値を取得
+     * @param  number sticknumber(x, y, x, y)
+     * @return        stick value
+     */
+    float getStick(int number) const;
+
+    /**
+     * スチィックの角度を取得
+     * @param  number left...0 right...1
+     * @return        radian
+     */
+    float getRadian(int number) const;
+
+    /**
+     * スティックの距離を取得
+     * @param  number left..0 right..1
+     * @return        norm
+     */
+    float getNorm(int number) const;
+
+private :
+    void setStick();
+
+    char data[6];
+    uint8_t fepTemp;
+
+protected :
+    bool button1[7];
+    bool button2[6];
+    double stick[4];
+    double radian[2];
+    double norm[2];
+};
+
+#endif//CONTROLLER_H