![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
今までのジョイスティックの入力を段階分けしていた方式から、アナログ値をできる限りそのまま使用する方式に変更しました。
Fork of ControlMain2017 by
main.cpp@13:79eb74eb86c4, 2017-02-18 (annotated)
- Committer:
- YusukeWakuta
- Date:
- Sat Feb 18 18:32:23 2017 +0000
- Revision:
- 13:79eb74eb86c4
- Parent:
- 10:b7159feb11fd
- Child:
- 14:ffe9460922cb
???????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YusukeWakuta | 13:79eb74eb86c4 | 1 | //中央 |
YusukeWakuta | 0:b1290ca6c4a2 | 2 | |
YusukeWakuta | 0:b1290ca6c4a2 | 3 | #include "mbed.h" |
tsumagari | 10:b7159feb11fd | 4 | #define WAIT_LOOP_TIME 0.001 |
tsumagari | 9:c32999b4d2f9 | 5 | #define YOKUTAN_DATAS_NUM 7 |
tsumagari | 10:b7159feb11fd | 6 | #define INPUT_DATAS_NUM 5 |
YusukeWakuta | 0:b1290ca6c4a2 | 7 | #define SEND_DATAS_CAN_ID 100 |
tsumagari | 8:dbc8c87dac78 | 8 | #define SEND_DATAS_TIME 0.5 |
tsumagari | 8:dbc8c87dac78 | 9 | #define THRESHOLD_OH_VALUE 0.14 |
YusukeWakuta | 0:b1290ca6c4a2 | 10 | |
tsumagari | 9:c32999b4d2f9 | 11 | //-----------------------------------(resetInterrupt def) |
tsumagari | 9:c32999b4d2f9 | 12 | extern "C" void mbed_reset(); |
YusukeWakuta | 13:79eb74eb86c4 | 13 | InterruptIn resetPin(p22); |
tsumagari | 9:c32999b4d2f9 | 14 | Timer resetTimeCount; |
tsumagari | 9:c32999b4d2f9 | 15 | void resetInterrupt(){ |
tsumagari | 9:c32999b4d2f9 | 16 | while(resetPin){ |
tsumagari | 9:c32999b4d2f9 | 17 | resetTimeCount.start(); |
tsumagari | 9:c32999b4d2f9 | 18 | if(resetTimeCount.read()>3) mbed_reset(); |
tsumagari | 9:c32999b4d2f9 | 19 | } |
tsumagari | 9:c32999b4d2f9 | 20 | resetTimeCount.reset(); |
tsumagari | 9:c32999b4d2f9 | 21 | } |
tsumagari | 9:c32999b4d2f9 | 22 | //------------------------------------------------------- |
tsumagari | 9:c32999b4d2f9 | 23 | |
taurin | 6:7484315ba88a | 24 | CAN can_R(p30,p29); |
taurin | 6:7484315ba88a | 25 | CAN can_L(p9,p10); |
YusukeWakuta | 13:79eb74eb86c4 | 26 | Serial toKeikiSerial(p27,p28); |
tsumagari | 7:59ddbe239835 | 27 | Serial pc(USBTX,USBRX); |
tsumagari | 10:b7159feb11fd | 28 | AnalogIn rollPin(p15); |
YusukeWakuta | 13:79eb74eb86c4 | 29 | AnalogIn pichPin(p20); |
YusukeWakuta | 13:79eb74eb86c4 | 30 | DigitalIn drug_R(p14); |
taurin | 6:7484315ba88a | 31 | DigitalIn drug_L(p19); |
YusukeWakuta | 0:b1290ca6c4a2 | 32 | DigitalOut myled1(LED1); |
YusukeWakuta | 0:b1290ca6c4a2 | 33 | DigitalOut myled2(LED2); |
tsumagari | 9:c32999b4d2f9 | 34 | //Ticker sendDatasTicker; |
YusukeWakuta | 0:b1290ca6c4a2 | 35 | |
taurin | 4:4a13dd263a7b | 36 | char yokutanDatas_R[YOKUTAN_DATAS_NUM]; |
taurin | 4:4a13dd263a7b | 37 | char yokutanDatas_L[YOKUTAN_DATAS_NUM]; |
tsumagari | 10:b7159feb11fd | 38 | char inputDatas_R[INPUT_DATAS_NUM]; |
tsumagari | 10:b7159feb11fd | 39 | char inputDatas_L[INPUT_DATAS_NUM]; |
taurin | 4:4a13dd263a7b | 40 | bool stateP = true; |
taurin | 4:4a13dd263a7b | 41 | bool stateQ = true; |
taurin | 4:4a13dd263a7b | 42 | bool stateP_old = true; |
taurin | 4:4a13dd263a7b | 43 | bool stateQ_old = true; |
YusukeWakuta | 0:b1290ca6c4a2 | 44 | |
YusukeWakuta | 3:0e66ce2ab2fb | 45 | CANMessage recmsg_R; |
YusukeWakuta | 3:0e66ce2ab2fb | 46 | CANMessage recmsg_L; |
YusukeWakuta | 0:b1290ca6c4a2 | 47 | |
tsumagari | 10:b7159feb11fd | 48 | float get_analogin_ave(AnalogIn pin, int n){ |
tsumagari | 10:b7159feb11fd | 49 | float val = 0; |
tsumagari | 10:b7159feb11fd | 50 | for(int i = 0; i<n; i++){ |
tsumagari | 10:b7159feb11fd | 51 | val += pin.read(); |
tsumagari | 10:b7159feb11fd | 52 | }return val/n; |
tsumagari | 10:b7159feb11fd | 53 | } |
tsumagari | 10:b7159feb11fd | 54 | |
taurin | 4:4a13dd263a7b | 55 | void InputControlValues(){ |
tsumagari | 10:b7159feb11fd | 56 | *(float *)inputDatas_R = get_analogin_ave(pichPin,8) + get_analogin_ave(rollPin,8); |
tsumagari | 10:b7159feb11fd | 57 | *(float *)inputDatas_L = get_analogin_ave(pichPin,8) - get_analogin_ave(rollPin,8); |
tsumagari | 10:b7159feb11fd | 58 | pc.printf("input_R:%f input_L:%f\n\r",*(float *)inputDatas_R,*(float *)inputDatas_L); |
tsumagari | 10:b7159feb11fd | 59 | inputDatas_R[4] = (char)drug_R; |
tsumagari | 10:b7159feb11fd | 60 | inputDatas_L[4] = (char)drug_L; |
YusukeWakuta | 0:b1290ca6c4a2 | 61 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 62 | |
taurin | 4:4a13dd263a7b | 63 | void toString_R(){ |
taurin | 4:4a13dd263a7b | 64 | pc.printf("R:"); |
tsumagari | 10:b7159feb11fd | 65 | for(int i = 0; i < INPUT_DATAS_NUM; i++){ |
taurin | 4:4a13dd263a7b | 66 | pc.printf("%d:%i ",i,inputDatas_R[i]); |
taurin | 4:4a13dd263a7b | 67 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 68 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 69 | |
taurin | 4:4a13dd263a7b | 70 | void toString_L(){ |
taurin | 4:4a13dd263a7b | 71 | pc.printf("L:"); |
tsumagari | 10:b7159feb11fd | 72 | for(int i = 0; i < INPUT_DATAS_NUM; i++){ |
taurin | 4:4a13dd263a7b | 73 | pc.printf("%d:%i ",i,inputDatas_L[i]); |
taurin | 4:4a13dd263a7b | 74 | } |
taurin | 4:4a13dd263a7b | 75 | pc.printf("\n\r"); |
YusukeWakuta | 0:b1290ca6c4a2 | 76 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 77 | |
taurin | 4:4a13dd263a7b | 78 | void SendDatas(){ |
tsumagari | 10:b7159feb11fd | 79 | can_R.write(CANMessage(SEND_DATAS_CAN_ID, inputDatas_R, INPUT_DATAS_NUM)); |
tsumagari | 10:b7159feb11fd | 80 | can_L.write(CANMessage(SEND_DATAS_CAN_ID, inputDatas_L, INPUT_DATAS_NUM)); |
tsumagari | 8:dbc8c87dac78 | 81 | toKeikiSerial.putc(';'); |
YusukeWakuta | 0:b1290ca6c4a2 | 82 | for(int i = 0; i < YOKUTAN_DATAS_NUM; i++){ |
YusukeWakuta | 0:b1290ca6c4a2 | 83 | toKeikiSerial.putc(yokutanDatas_R[i]); |
YusukeWakuta | 0:b1290ca6c4a2 | 84 | toKeikiSerial.putc(yokutanDatas_L[i]); |
YusukeWakuta | 0:b1290ca6c4a2 | 85 | } |
tsumagari | 10:b7159feb11fd | 86 | for(int i = 0; i < INPUT_DATAS_NUM; i++){ |
taurin | 4:4a13dd263a7b | 87 | toKeikiSerial.putc(inputDatas_R[i]); |
taurin | 4:4a13dd263a7b | 88 | toKeikiSerial.putc(inputDatas_L[i]); |
taurin | 4:4a13dd263a7b | 89 | } |
tsumagari | 8:dbc8c87dac78 | 90 | // toKeikiSerial.putc(cadence); |
taurin | 4:4a13dd263a7b | 91 | pc.printf("test\n\r"); |
YusukeWakuta | 0:b1290ca6c4a2 | 92 | } |
tsumagari | 8:dbc8c87dac78 | 93 | |
YusukeWakuta | 0:b1290ca6c4a2 | 94 | void init(){ |
tsumagari | 9:c32999b4d2f9 | 95 | //--------------------------------------(resetInterrupt init) |
tsumagari | 9:c32999b4d2f9 | 96 | resetPin.rise(resetInterrupt); |
tsumagari | 9:c32999b4d2f9 | 97 | resetPin.mode(PullDown); |
tsumagari | 9:c32999b4d2f9 | 98 | //----------------------------------------------------------- |
tsumagari | 9:c32999b4d2f9 | 99 | // sendDatasTicker.attach(SendDatas,SEND_DATAS_TIME); |
tsumagari | 10:b7159feb11fd | 100 | for(int i = 0; i < INPUT_DATAS_NUM; i++){ |
taurin | 2:9dc7d5f1e910 | 101 | inputDatas_R[i] = 0; |
taurin | 2:9dc7d5f1e910 | 102 | inputDatas_L[i] = 0; |
taurin | 2:9dc7d5f1e910 | 103 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 104 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 105 | |
YusukeWakuta | 0:b1290ca6c4a2 | 106 | void receiveDatas(){ |
YusukeWakuta | 0:b1290ca6c4a2 | 107 | if(can_R.read(recmsg_R)){ |
YusukeWakuta | 0:b1290ca6c4a2 | 108 | for(int i = 0; i < recmsg_R.len; i++){ |
YusukeWakuta | 0:b1290ca6c4a2 | 109 | yokutanDatas_R[i] = recmsg_R.data[i]; |
YusukeWakuta | 0:b1290ca6c4a2 | 110 | } |
taurin | 4:4a13dd263a7b | 111 | myled1 = !myled1; |
YusukeWakuta | 0:b1290ca6c4a2 | 112 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 113 | if(can_L.read(recmsg_L)){ |
YusukeWakuta | 0:b1290ca6c4a2 | 114 | for(int i = 0; i < recmsg_L.len; i++){ |
YusukeWakuta | 0:b1290ca6c4a2 | 115 | yokutanDatas_L[i] = recmsg_L.data[i]; |
YusukeWakuta | 0:b1290ca6c4a2 | 116 | } |
taurin | 4:4a13dd263a7b | 117 | myled2 = !myled2; |
YusukeWakuta | 0:b1290ca6c4a2 | 118 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 119 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 120 | |
YusukeWakuta | 0:b1290ca6c4a2 | 121 | int main(){ |
YusukeWakuta | 0:b1290ca6c4a2 | 122 | init(); |
YusukeWakuta | 0:b1290ca6c4a2 | 123 | while(1){ |
tsumagari | 8:dbc8c87dac78 | 124 | InputControlValues(); |
tsumagari | 10:b7159feb11fd | 125 | wait_us(5); |
YusukeWakuta | 0:b1290ca6c4a2 | 126 | receiveDatas(); |
tsumagari | 9:c32999b4d2f9 | 127 | SendDatas(); |
tsumagari | 10:b7159feb11fd | 128 | // toString_R(); |
taurin | 4:4a13dd263a7b | 129 | // toString_L(); |
YusukeWakuta | 0:b1290ca6c4a2 | 130 | wait(WAIT_LOOP_TIME); |
YusukeWakuta | 0:b1290ca6c4a2 | 131 | } |
YusukeWakuta | 0:b1290ca6c4a2 | 132 | } |