ken fuji
/
robocon2017mbed_contoroler_L2
修正
Fork of robocon2017mbed_contoroler_L2 by
main.cpp
- Committer:
- kikoaac
- Date:
- 2017-10-19
- Revision:
- 2:5d86b09dd7d8
- Parent:
- 0:84122dbed53a
- Child:
- 3:21fb6b67bb54
File content as of revision 2:5d86b09dd7d8:
#include "mbed.h" #include "Nunchuck.h" #define R 0 #define L 1 #define DEBUG 0 #define DEBUG_R 1 #define CALIB 0 #define SetupCMD 'A' #define Respons 'S' DigitalOut debugLed1(D6); DigitalOut debugLed2(D7); bool send = false; union floatInByte { uint16_t si; unsigned char c[2]; }; class AnalogInLPF : public AnalogIn { private: float alpha; float prevAnalog; float nowAnalog; public : AnalogInLPF(PinName pin,float alpha_) : AnalogIn(pin) { alpha = alpha_; prevAnalog = 0.0; } float read(){ nowAnalog = AnalogIn::read(); nowAnalog = nowAnalog*alpha + (1-alpha)*prevAnalog; prevAnalog = nowAnalog; return nowAnalog; } short read_u16(){ nowAnalog = AnalogIn::read(); nowAnalog = nowAnalog*alpha + (1-alpha)*prevAnalog; nowAnalog = float(short(nowAnalog*0xFFFF)&0xFF00)/0xFFFF; prevAnalog = nowAnalog; return short(nowAnalog*0xFFFF); } }; class InLPF { private: float alpha; float prevAnalog; float nowAnalog; public : InLPF(float alpha_ = 0.2) { alpha = alpha_; prevAnalog = 0.0; } float read(float in){ nowAnalog = in; nowAnalog = nowAnalog*alpha + (1-alpha)*prevAnalog; prevAnalog = nowAnalog; return nowAnalog; } short read_u16(float in){ nowAnalog = in; nowAnalog = nowAnalog*alpha + (1-alpha)*prevAnalog; prevAnalog = nowAnalog; return short(nowAnalog*0xFFFF)&0xFF00; } }; uint16_t map(uint16_t in, uint16_t inMin, uint16_t inMax, uint16_t outMin, uint16_t outMax); #define LP 0.30 AnalogInLPF ArmSense[4] = {AnalogInLPF(A6,LP-0.018),AnalogInLPF(A5,LP),AnalogInLPF(A4,LP-0.018),AnalogInLPF(A3,LP)}; InLPF ArmSense2[4] = {InLPF(LP),InLPF(LP),InLPF(LP),InLPF(LP)}; #if !CALIB //1号機 /* uint16_t MinimumRangeR[4] = {25700,39300,21760,53000}; uint16_t MaxmumRangeR[4] = {44000,52600,54270,27600}; uint16_t MinimumRangeL[4] = {20600,28500,2114,33300}; uint16_t MaxmumRangeL[4] = {44000,43300,58900,6021}; //2号機 */ uint16_t MinimumRangeR[4] = {19900,38656,36096,17152}; uint16_t MaxmumRangeR[4] = {43260,53760,58624,41216}; uint16_t MinimumRangeL[4] = {21500,19400,8448,57088}; uint16_t MaxmumRangeL[4] = {43520,38900,38400,30208}; #endif #if CALIB uint16_t MinimumRangeR[4] = {0,0,0,0}; uint16_t MaxmumRangeR[4] = {0xffff,0xffff,0xffff,0xffff}; uint16_t MinimumRangeL[4] = {0,0,0,0}; uint16_t MaxmumRangeL[4] = {0xffff,0xffff,0xffff,0xffff}; #endif //uint16_t MinimumRangeL[4] = {19000,35000,35600,21000}; //uint16_t MaxmumRangeL[4] = {49000,57000,43000,42000 }; bool ReverseL[4] = {false,true,true,true}; bool ReverseR[4] = {true,false,true,false}; //AnalogIn ArmSense[4] = {AnalogIn(A6),AnalogIn(A5),AnalogIn(A4),AnalogIn(A3)}; Nunchuck ctrl(D4,D5); Serial dev(D1,D0); Serial sbdbt(D13,D12); #define dataNum 12 void waitTime(float ti){ Timer t; t.start(); while(ti > t.read()); t.stop(); return; } Timer timer; char *tmp[2]; char RXData[dataNum] = {'0'}; void RX(){ if(dev.getc() == '0'){ timer.reset(); debugLed1 = true; for(int i = 1 ; i < dataNum;i++){ RXData[i] = dev.getc(); } //if(DEBUG && !DEBUG_R)sbdbt.printf("L:"); for(int i = 0 ;i < 4 ; i++){ floatInByte in;//( (uint16_t)tmp[R][5 + i*2] << 8 ) | (uint16_t)tmp[R][4 + i*2]; in.c[0] = RXData[4 + i*2];//tmp[R][5 + i*2]; in.c[1] = RXData[5 + i*2];//tmp[R][4 + i*2]; uint16_t in_ = ArmSense2[i].read_u16(float(in.si)/0xffff); //uint16_t intt = map(in_,ReverseL[i] == true ? 0xffff - MaxmumRangeL[i] : MinimumRangeL[i],ReverseL[i] == true ? 0xffff - MinimumRangeL[i] : MaxmumRangeL[i],0,65535); uint16_t intt = map(in_,MinimumRangeL[i],MaxmumRangeL[i],0,65535); #if !CALIB intt = ReverseL[i] == true ? 0xffff - intt : intt; #endif floatInByte intt_; intt_.si = intt; //if(DEBUG && !DEBUG_R)sbdbt.printf(" %5d ",intt); //uint16_t intt = map(in_,13107,52428,0,65535); RXData[4 + i*2] = 0;//intt_.c[0];//uint8_t(intt>>8);//マスター片腕 RXData[5 + i*2] = intt_.c[1];//uint8_t(intt&0xff); //マスター片腕 }//if(DEBUG && !DEBUG_R)sbdbt.printf("\n"); timer.reset(); send = true; } } void print(int N,char RXdata[12]){ floatInByte data; for(int i = 0 ; i < 2 ; i ++){ data.c[i] = RXdata[N+i]; } sbdbt.printf("%d ",data.si); } double offset[4] = {0,0,0,-132}; double range[4] = {120,120,90,240}; double range2[4] = {120,120,90,9}; int main() { dev.baud(115200); sbdbt.baud(115200); for(int i = 0 ; i < 2; i++) { tmp[i] = new char[dataNum]; } debugLed1 = true; for(int i = 0 ; i < 50 ; i ++ ){ debugLed2 = !debugLed2; wait(0.1); } dev.attach(RX, Serial::RxIrq); dev.putc('L'); timer.start(); int count = 0; while(1) { //送信データ格納 tmp[R][0] = 'H'; tmp[R][1] = ctrl.analogx();//ヌンチャクアナログX tmp[R][2] = ctrl.analogy();//ヌンチャクアナログy tmp[R][3] = (ctrl.buttonc()<<1)|(ctrl.buttonz());//ヌンチャクzボタンとCボタン for(int i = 0 ;i < 4 ; i++){ floatInByte intt_; //if(i==0 && DEBUG && DEBUG_R)sbdbt.printf("R:"); uint16_t in = ArmSense[i].read_u16(); //if(DEBUG && DEBUG_R)sbdbt.printf(" %5d ",intt); //uint16_t intt = map(in, ReverseR[i] == true ? 0xffff - MaxmumRangeR[i] : MinimumRangeR[i],ReverseR[i] == true ? 0xffff - MinimumRangeR[i] : MaxmumRangeR[i],0,65535); uint16_t intt = map(in, MinimumRangeR[i],MaxmumRangeR[i],0,65535); #if !CALIB intt = ReverseR[i] == true ? 0xffff - intt : intt; #endif intt_.si = intt; //uint16_t intt = map(in_,13107,52428,0,65535); tmp[R][4 + i*2] = 0;//intt_.c[0];//uint8_t(intt>>8);//マスター片腕 tmp[R][5 + i*2] = intt_.c[1];//uint8_t(intt&0xff); //マスター片腕 } //if(DEBUG && DEBUG_R)sbdbt.printf("\n"); tmp[L] = RXData; char** SerialData = tmp; //送信データを送る //SerialData = tmp; if(count > 20 && send == true){ /*for(int j = 0; j < 2 ; j++){ for(int i = 0 ; i < dataNum ; i++){ //if(!DEBUG)sbdbt.printf("%3d ",SerialData[j][i]); //if(!DEBUG)sbdbt.putc(SerialData[j][i]); } }*/ /* sbdbt.printf("R:"); for(int i = 4 ; i < 12 ; i+=2)print(i,SerialData[R]); sbdbt.printf("L:"); for(int i = 4 ; i < 12 ; i+=2)print(i,SerialData[L]); */ for(int j = 0; j < 2 ; j++){ for(int i = 0 ; i < dataNum ; i++){ //sbdbt.printf("%3d ",SerialData[j][i]); if(!DEBUG)sbdbt.putc(SerialData[j][i]); } } count = 0; if(!DEBUG)sbdbt.printf("\n"); } dev.putc('L'); send = false; while(timer.read_ms() >= 2000){ debugLed2 = true; waitTime(0.1); debugLed2 = false; waitTime(0.1); } debugLed1 = false; waitTime(0.0005); count ++; #if DEBUG int RL = R; if(DEBUG_R){sbdbt.printf("R:");RL = R;} if(!DEBUG_R){sbdbt.printf("L:");RL = L;} for(int i = 4 ; i < 12 ; i+=2)print(i,SerialData[RL]); sbdbt.printf("\n"); #endif } } uint16_t map(uint16_t in, uint16_t inMin, uint16_t inMax, uint16_t outMin, uint16_t outMax) { // check it's within the range if (inMin<inMax) { if (in <= inMin) return outMin; if (in >= inMax) return outMax; } else { // cope with input range being backwards. if (in >= inMin) return outMin; if (in <= inMax) return outMax; } // calculate how far into the range we are float scale = float(in-inMin)/float(inMax-inMin); // calculate the output. return uint16_t(outMin + scale*float(outMax-outMin)); }