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.
Revision 2:507b31207449, committed 2022-09-06
- Comitter:
- e5119053f6
- Date:
- Tue Sep 06 06:07:56 2022 +0000
- Parent:
- 1:ab1c94d6f4fb
- Commit message:
- controller
Changed in this revision
--- a/Controller.cpp Fri Dec 17 10:04:13 2021 +0000
+++ b/Controller.cpp Tue Sep 06 06:07:56 2022 +0000
@@ -31,10 +31,8 @@
comCheck = false;
#if CON_TYPE == CON_ADACHI // 安達君開発のコントローラを使う場合の処理(どのコントローラを使うかはdefine.hで設定)
- while (loop_count < 10 && serial.readable())
- {
- if (serial_recieve() == '\n')
- {
+ while (loop_count < 10 && serial.readable()) {
+ if (serial_recieve() == '\n') {
for (int i = 0; i < 8; i++)
receive_data[i] = serial_recieve();
for (int i = 0; i < 8; i++)
@@ -42,8 +40,7 @@
for (int i = 0; i < 7; i++)
checksum ^= receive_data[i];
- if (receive_data[7] == checksum & 0xFF)
- {
+ if (receive_data[7] == checksum & 0xFF) {
comCheck = true;
//pre_conData.ButtonState = conData.ButtonState;
@@ -57,7 +54,7 @@
break;
}
-
+
pre_count_ms = count_ms; //受信時間の更新
}
loop_count++;
@@ -66,17 +63,15 @@
// コントローラデータを取得する部分
static int recv_num = 0;
char c;
- while (serial.readable())
- {
+ while (serial.readable()) {
c = serial.getc();
- if (c == '\n')
- {
- if (recv_num == 10)
- { // チェックサムは無く,9個受信したら値を格納
+ if (c == '\n') {
+ if (recv_num == 10) {
+ // チェックサムは無く,9個受信したら値を格納
for (int i = 0; i < 9; i++)
checksum += (unsigned int)(receive_data[i] - 0x20); // チェックサムの計算
- if ((checksum & 0x3F) == (receive_data[9] - 0x20))
- { // チェックサムの計算が合っていた場合のみ値を格納
+ if ((checksum & 0x3F) == (receive_data[9] - 0x20)) {
+ // チェックサムの計算が合っていた場合のみ値を格納
comCheck = true;
//conData.ButtonState = 0;
@@ -102,27 +97,21 @@
conData.RJoyY = abs(conData.RJoyY - 0xFF);
int buttonPushNum = 0;
- for (int i = 0; i < 16; i++)
- {
+ for (int i = 0; i < 16; i++) {
buttonPushNum += (conData.ButtonState >> i) & 0x0001;
}
- if (buttonPushNum > 5)
- {
+ if (buttonPushNum > 5) {
//conData.ButtonState = pre_conData.ButtonState;
comCheck = false;
- }
- else
- {
+ } else {
conData.ButtonState |= lastButtonState;
}
-
+
pre_count_ms = count_ms; //受信時間の更新
}
}
recv_num = 0;
- }
- else
- {
+ } else {
receive_data[recv_num] = c;
recv_num++;
}
@@ -131,19 +120,21 @@
// コントローラデータを取得する部分
static int recv_num = 0;
char c;
- while (serial.readable())
- {
+ while (serial.readable()) {
c = serial.getc();
- //Serial.print(c);
- if (c == '\n')
- {
- if (recv_num == 10)
- { // データ数はチェックサム含めて10個(0~9)
+ //printf("%x ",c);
+
+ if (c == '\n') {
+ //printf("\t");
+ if (recv_num == 10) {
+ // データ数はチェックサム含めて10個(0~9)
checksum = 0;
- for (int i = 0; i < 9; i++)
- checksum ^= (unsigned int)(receive_data[i] - 0x20); // チェックサムの計算
- if ((checksum & 0x3F) == (receive_data[9] - 0x20))
- { // チェックサムの計算が合っていた場合のみ値を格納
+ //for(int i = 0; i<10; i++)printf("%+x ",receive_data[i]);
+ for (int i = 0; i < 9; i++)checksum ^= (unsigned int)(receive_data[i] - 0x20); // チェックサムの計算
+ //printf(" %+x\n",checksum);
+ if ((checksum & 0x3F) == (receive_data[9] - 0x20)) {
+ // チェックサムの計算が合っていた場合のみ値を格納
+ //printf("checksum ok \n");
comCheck = true;
//conData.ButtonState = 0;
@@ -152,7 +143,7 @@
lastButtonState |= (unsigned int)((receive_data[1] - 0x20) & 0x3F) << 6;
lastButtonState |= (unsigned int)((receive_data[2] - 0x20) & 0x0F) << 12;
- conData.LJoyX |= (unsigned int)(receive_data[3] - 0x20);
+ conData.LJoyX |= (unsigned int)(receive_data[3] - 0x20) & 0x3F;
conData.LJoyX |= (unsigned int)((receive_data[4] - 0x20) & 0x03) << 6;
conData.LJoyX = abs(conData.LJoyX - 0xFF);
@@ -164,23 +155,19 @@
conData.RJoyX |= (unsigned int)((receive_data[6] - 0x20) & 0x3F) << 2;
conData.RJoyX = abs(conData.RJoyX - 0xFF);
- conData.RJoyY |= (unsigned int)(receive_data[7] - 0x20);
+ conData.RJoyY |= (unsigned int)(receive_data[7] - 0x20) & 0x3F;
conData.RJoyY |= (unsigned int)((receive_data[8] - 0x20) & 0x03) << 6;
conData.RJoyY = abs(conData.RJoyY - 0xFF);
// 通信ミスであり得ない数のボタン数押されていた場合に無視する処理
int buttonPushNum = 0;
- for (int i = 0; i < 16; i++)
- {
+ for (int i = 0; i < 16; i++) {
buttonPushNum += (lastButtonState >> i) & 0x0001;
}
- if (buttonPushNum > 5)
- {
+ if (buttonPushNum > 5) {
//conData.ButtonState = pre_conData.ButtonState;
comCheck = false;
- }
- else
- {
+ } else {
conData.ButtonState = lastButtonState & 0xFFFF;
}
@@ -188,20 +175,21 @@
}
}
recv_num = 0;
- }
- else
- {
+ } else {
receive_data[recv_num] = c;
recv_num++;
}
+ //printf("con\n");
}
+ //printf("nuketa\n");
#endif
-
+//printf("g1");
if(!(time_out_ms == -1)) conAvailable = (time_out_ms > (count_ms - pre_count_ms)); //タイムアウトとインターバルの比較
else conAvailable = true;
+ //printf("g2");
if(count_ms > time_out_ms * 1000) count_ms = time_out_ms; //オーバーフロー対策
-
+//printf("h");
return comCheck;
}
@@ -215,13 +203,35 @@
return conAvailable;
}
+bool Controller::clearBuffer(void)
+{
+ if(serial.readable()) serial.getc();
+ return serial.readable();
+}
+
+bool Controller::rate(void)
+{
+ bool _ButtunState = false, _RX = false, _RY = false, _LX = false, _LY = false ;
+ if(comCheck) {
+ _ButtunState = (conData.ButtonState != 0)? true : false;
+ _RX = (conData.RJoyX != 128)? true : false;
+ _RY = (conData.RJoyY != 128)? true : false;
+ _LX = (conData.LJoyX != 128)? true : false;
+ _LY = (conData.LJoyY != 128)? true : false;
+ }
+ //printf("%d,%d,%d,%d,%d\n",_ButtunState,_RX,_RY,_LX,_LY);
+ return _ButtunState | _RX | _RY | _LX | _LY;
+}
+
bool Controller::readButton_bin(unsigned int ButtonNum)
-{ //放しているときは0,押しているときは1
+{
+ //放しているときは0,押しているときは1
return ((conData.ButtonState & (0x0001 << (ButtonNum - 1))) == (0x0001 << (ButtonNum - 1))) ? true : false;
}
int Controller::readButton(unsigned int ButtonNum)
-{ //放しているときは0,押しているときは1,押した瞬間は2,放した瞬間は-1
+{
+ //放しているときは0,押しているときは1,押した瞬間は2,放した瞬間は-1
int result = 0;
if ((conData.ButtonState & (0x0001 << (ButtonNum - 1))) == (0x0001 << (ButtonNum - 1)))
result += 2;
@@ -297,13 +307,10 @@
{
// 立ち上がり,立下りのフラッギング処理 (フラグクリアは別関数で)
unsigned int buttonFlagRise = 0;
- if (pre_conData.ButtonState != conData.ButtonState)
- {
- for (int i = 0; i < 16; i++)
- {
+ if (pre_conData.ButtonState != conData.ButtonState) {
+ for (int i = 0; i < 16; i++) {
int mask = 0x01 << i;
- if ((conData.ButtonState & mask) != (pre_conData.ButtonState & mask))
- {
+ if ((conData.ButtonState & mask) != (pre_conData.ButtonState & mask)) {
if ((conData.ButtonState & mask) == mask)
buttonFlagRise |= (conData.ButtonState & mask);
}
@@ -315,18 +322,14 @@
unsigned int Controller::getButtonFlagFall()
{
unsigned int buttonFlagFall = 0;
- if (pre_conData.ButtonState != conData.ButtonState)
- {
- for (int i = 0; i < 16; i++)
- {
+ if (pre_conData.ButtonState != conData.ButtonState) {
+ for (int i = 0; i < 16; i++) {
int mask = 0x01 << i;
- if ((conData.ButtonState & mask) != (pre_conData.ButtonState & mask))
- {
+ if ((conData.ButtonState & mask) != (pre_conData.ButtonState & mask)) {
if ((pre_conData.ButtonState & mask) == mask)
buttonFlagFall |= (pre_conData.ButtonState & mask);
}
}
}
return buttonFlagFall;
-}
-
+}
\ No newline at end of file
--- a/Controller.h Fri Dec 17 10:04:13 2021 +0000
+++ b/Controller.h Tue Sep 06 06:07:56 2022 +0000
@@ -1,28 +1,38 @@
+/*
+ *https://os.mbed.com/users/kikuchi8810/code/controllerForMbed_test//file/ab1c94d6f4fb/main.cpp/
+ *元データ
+ *作:菊池
+ */
+
+
+
#ifndef CONTROLLER_H
#define CONTROLLER_H
-
+
#include "mbed.h"
#include "RawSerial.h"
#include "define.h"
-
+
struct ControllerData{
unsigned int ButtonState;
uint8_t RJoyX, RJoyY, LJoyX, LJoyY;
};
-
+
class Controller{
public:
Controller(PinName tx, PinName rx, int baudrate);
int count_ms, pre_count_ms; //受信時刻と前回の受信時刻
-
+
bool readButton_bin(unsigned int ButtonNum); //押していない時はfalse(0),押してるときはtrue(1)を返す. ButtonNumはデータの欲しいボタンの名前を
int readButton(unsigned int ButtonNum); //上にプラスして 押した瞬間は2,放した瞬間は-1を返す. define.hを参考に数字を入力しても良い
bool getComCheck(void); //値が更新されたときにtrueを返す.
bool update(); //受信の処理+ボタンの情報の更新.受信割込みで処理
+ bool clearBuffer(void);//buffer消去用.falseが返ってくれば消去完了.
+ bool rate(void);//送信側で入力がある場合にtrue.
void init(int _time_out_ms, int _int_time_ms); //コントローラの通信速度と通信のタイムアウト時間を設定
bool available(void);
-
+
unsigned int getButtonState(); //分解する前のButtonStateの情報をprint 0~255の値をとる
void clearButtonState();
ControllerData getConData();
@@ -36,7 +46,7 @@
double readJoyLY(); // |
// |
// 1.0 ~ -1.0
-
+
// X
uint8_t readJoyRXbyte(); // ^
uint8_t readJoyRYbyte(); // |
@@ -44,21 +54,21 @@
uint8_t readJoyLYbyte(); // |
// |
// 255 ~ 0
-
+
private:
RawSerial serial;
//Timer timer;
-
+
bool comCheck;
ControllerData conData;
ControllerData pre_conData;
unsigned int lastButtonState;
-
+
int time_out_ms;
double int_time_ms;
bool conAvailable;
-
+
uint8_t serial_recieve(){
char temp;
do{
@@ -69,6 +79,5 @@
return temp;
}
};
-
-#endif
-
+
+#endif
\ No newline at end of file
--- a/main.cpp Fri Dec 17 10:04:13 2021 +0000
+++ b/main.cpp Tue Sep 06 06:07:56 2022 +0000
@@ -5,12 +5,12 @@
#define INT_TIME 0.001
#define INT_TIME_MS INT_TIME*1000
-Serial pc(USBTX, USBRX);
-Controller con(P7_4,P7_5, 115200); //TXpin, RXpin, baudrateを設定
-
-DigitalOut led_user(LED_USER);
-DigitalOut led_red(LED_RED);
-DigitalOut pin_emergency(D0); //非常停止信号
+Serial pc(USBTX, USBRX,115200);
+Controller con(p9,p10,115200); //TXpin, RXpin, baudrateを設定
+//Controller con(p13,p14,115200); //TXpin, RXpin, baudrateを設定
+//DigitalOut led_user(LED_USER);
+//DigitalOut led_red(LED_RED);
+//DigitalOut pin_emergency(D0); //非常停止信号
Ticker interrupt;
bool flag_10ms = false;
@@ -35,7 +35,7 @@
int main()
{
- pc.baud(115200);
+ //pc.baud(115200);
/* 第一引数はタイムアウト時間[ms],第二引数はupdate関数の呼び出し周期[ms] */
con.init(1000, INT_TIME_MS*10); //init関数を呼び出さなければタイムアウトの処理は行われない(available関数は常にtrueを返す)
@@ -44,6 +44,7 @@
while(1)
{
+ //pc.printf("");
if(flag_10ms)
{
con.update(); //main関数のflag内で呼び出す.
@@ -55,20 +56,20 @@
uint8_t joyLx = con.readJoyLXbyte();
uint8_t joyLy = con.readJoyLYbyte();
- pc.printf("%d\t", buttonState);
- pc.printf("%d\t", joyRx);
- pc.printf("%d\t", joyRy);
- pc.printf("%d\t", joyLx);
- pc.printf("%d\r\n", joyLy);
+ pc.printf("%u\t", buttonState);
+ pc.printf("%u\t", joyRx);
+ pc.printf("%u\t", joyRy);
+ pc.printf("%u\t", joyLx);
+ pc.printf("%u\r\n", joyLy);
- led_red.write(1);
- pin_emergency.write(1); //非常停止を解除する
+ //led_red.write(1);
+ //pin_emergency.write(1); //非常停止を解除する
}
else
{
pc.printf("disconnected\r\n");
- led_red.write(0);
- pin_emergency.write(0); //非常停止を作動させる
+ //led_red.write(0);
+ //pin_emergency.write(0); //非常停止を作動させる
}
flag_10ms = false;
@@ -76,10 +77,10 @@
if(flag_1s)
{
- led_user.write(!led_user.read());
+ //led_user.write(!led_user.read());
flag_1s = false;
}
-
- thread_sleep_for(1);
+ wait_us(1);
+ //thread_sleep_for(1);
}
}