+3度のやつ
Dependencies: ADXL345_I2C Control_Yokutan_CANver1 mbed
Fork of Souda_Yokutan_ver4 by
main.cpp@14:1f6dd929d7de, 2016-02-27 (annotated)
- Committer:
- YusukeWakuta
- Date:
- Sat Feb 27 11:36:09 2016 +0000
- Revision:
- 14:1f6dd929d7de
- Parent:
- 13:5e3b4120dbbf
- Child:
- 15:1db5ee4fe7ce
??????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
taurin | 0:e052602db102 | 1 | //翼端can program |
taurin | 0:e052602db102 | 2 | #include "mbed.h" |
taurin | 0:e052602db102 | 3 | #include "ADXL345_I2C.h" |
taurin | 0:e052602db102 | 4 | #include "INA226.hpp" |
taurin | 4:450cafd95ac3 | 5 | |
taurin | 4:450cafd95ac3 | 6 | #define TO_SEND_DATAS_NUM 4 |
taurin | 2:7fcb4f970a02 | 7 | #define INIT_SERVO_PERIOD_MS 20 |
taurin | 4:450cafd95ac3 | 8 | #define WAIT_LOOP_TIME 0.02 |
taurin | 0:e052602db102 | 9 | #define CONTROL_VALUES_NUM 2 |
taurin | 0:e052602db102 | 10 | #define TO_SEND_CAN_ID 100 |
taurin | 4:450cafd95ac3 | 11 | #define ADXL_MEAN_NUM 10 |
taurin | 4:450cafd95ac3 | 12 | #define SEND_DATAS_LOOP_TIME 0.1 |
taurin | 12:fd9d241843f4 | 13 | |
YusukeWakuta | 14:1f6dd929d7de | 14 | #define ERURON_MOVE_DEG_INI_R 55 |
YusukeWakuta | 14:1f6dd929d7de | 15 | #define DRUG_MOVE_DEG_INI_R 96 |
YusukeWakuta | 14:1f6dd929d7de | 16 | #define ERURON_TRIM_INI_R 41 |
YusukeWakuta | 14:1f6dd929d7de | 17 | #define DRUG_TRIM_INI_R 3 |
taurin | 12:fd9d241843f4 | 18 | |
YusukeWakuta | 14:1f6dd929d7de | 19 | #define ERURON_MOVE_DEG_INI_L 29 |
YusukeWakuta | 14:1f6dd929d7de | 20 | #define DRUG_MOVE_DEG_INI_L 87 |
YusukeWakuta | 14:1f6dd929d7de | 21 | #define ERURON_TRIM_INI_L 79 |
YusukeWakuta | 14:1f6dd929d7de | 22 | #define DRUG_TRIM_INI_L 92 |
taurin | 2:7fcb4f970a02 | 23 | |
taurin | 0:e052602db102 | 24 | CAN can(p30,p29); |
taurin | 0:e052602db102 | 25 | CANMessage recmsg; |
taurin | 0:e052602db102 | 26 | Serial pc(USBTX,USBRX); |
taurin | 0:e052602db102 | 27 | ADXL345_I2C accelerometer(p9, p10); |
taurin | 0:e052602db102 | 28 | I2C ina226_i2c(p28,p27); |
taurin | 0:e052602db102 | 29 | INA226 VCmonitor(ina226_i2c); |
taurin | 13:5e3b4120dbbf | 30 | PwmOut drugServo(p22); |
taurin | 13:5e3b4120dbbf | 31 | PwmOut eruronServo(p23); |
taurin | 1:9cc932a16d17 | 32 | DigitalOut led1(LED1); |
taurin | 4:450cafd95ac3 | 33 | AnalogIn drugAna(p20); |
taurin | 4:450cafd95ac3 | 34 | AnalogIn eruronAna(p19); |
taurin | 12:fd9d241843f4 | 35 | DigitalIn LRstatePin(p14); |
taurin | 4:450cafd95ac3 | 36 | DigitalIn setTrimPin(p15); |
taurin | 12:fd9d241843f4 | 37 | DigitalIn EDstatePin(p16); |
taurin | 4:450cafd95ac3 | 38 | DigitalIn checkMaxDegPin(p17); |
taurin | 4:450cafd95ac3 | 39 | DigitalOut debugLED(LED2); |
taurin | 4:450cafd95ac3 | 40 | DigitalOut led3(LED3); |
taurin | 4:450cafd95ac3 | 41 | DigitalOut led4(LED4); |
taurin | 4:450cafd95ac3 | 42 | Ticker sendDatasTicker; |
taurin | 4:450cafd95ac3 | 43 | Ticker toStringTicker; |
taurin | 13:5e3b4120dbbf | 44 | // |
taurin | 0:e052602db102 | 45 | char toSendDatas[TO_SEND_DATAS_NUM]; |
taurin | 1:9cc932a16d17 | 46 | char controlValues[CONTROL_VALUES_NUM];//0:eruruon,1:drug |
taurin | 0:e052602db102 | 47 | |
taurin | 12:fd9d241843f4 | 48 | float eruronTrim; |
taurin | 12:fd9d241843f4 | 49 | float drugTrim; |
taurin | 12:fd9d241843f4 | 50 | float eruronMoveDeg; |
taurin | 12:fd9d241843f4 | 51 | float drugMoveDeg; |
taurin | 0:e052602db102 | 52 | unsigned short ina_val; |
taurin | 0:e052602db102 | 53 | double V,C; |
taurin | 0:e052602db102 | 54 | bool SERVO_FLAG; |
taurin | 0:e052602db102 | 55 | bool ADXL_FLAG; |
taurin | 0:e052602db102 | 56 | bool INA_FLAG; |
taurin | 0:e052602db102 | 57 | |
taurin | 0:e052602db102 | 58 | int acc[3] = {0,0,0}; |
taurin | 4:450cafd95ac3 | 59 | char acc_mean[3][ADXL_MEAN_NUM]; |
taurin | 4:450cafd95ac3 | 60 | int adxl_mean_counter = 0; |
taurin | 4:450cafd95ac3 | 61 | |
taurin | 4:450cafd95ac3 | 62 | void toString(); |
taurin | 0:e052602db102 | 63 | |
taurin | 0:e052602db102 | 64 | bool servoInit(){ |
taurin | 4:450cafd95ac3 | 65 | drugServo.period_ms(INIT_SERVO_PERIOD_MS); |
taurin | 4:450cafd95ac3 | 66 | eruronServo.period_ms(INIT_SERVO_PERIOD_MS); |
taurin | 0:e052602db102 | 67 | return true; |
taurin | 0:e052602db102 | 68 | } |
taurin | 0:e052602db102 | 69 | |
taurin | 0:e052602db102 | 70 | bool adxlInit(){ |
taurin | 0:e052602db102 | 71 | accelerometer.setPowerControl(0x00); |
taurin | 0:e052602db102 | 72 | accelerometer.setDataFormatControl(0x0B); |
taurin | 0:e052602db102 | 73 | accelerometer.setDataRate(ADXL345_3200HZ); |
taurin | 0:e052602db102 | 74 | accelerometer.setPowerControl(0x08); |
taurin | 0:e052602db102 | 75 | return true; |
taurin | 0:e052602db102 | 76 | } |
taurin | 0:e052602db102 | 77 | |
taurin | 4:450cafd95ac3 | 78 | void sendDatas(){ |
taurin | 4:450cafd95ac3 | 79 | if(can.write(CANMessage(TO_SEND_CAN_ID, toSendDatas, TO_SEND_DATAS_NUM))){ |
taurin | 4:450cafd95ac3 | 80 | } |
taurin | 4:450cafd95ac3 | 81 | } |
taurin | 4:450cafd95ac3 | 82 | |
taurin | 0:e052602db102 | 83 | bool inaInit(){ |
taurin | 0:e052602db102 | 84 | if(!VCmonitor.isExist()){ |
taurin | 0:e052602db102 | 85 | pc.printf("VCmonitor NOT FOUND\n"); |
taurin | 0:e052602db102 | 86 | return false; |
taurin | 0:e052602db102 | 87 | } |
taurin | 0:e052602db102 | 88 | ina_val = 0; |
taurin | 0:e052602db102 | 89 | if(VCmonitor.rawRead(0x00,&ina_val) != 0){ |
taurin | 0:e052602db102 | 90 | pc.printf("VCmonitor READ ERROR\n"); |
taurin | 0:e052602db102 | 91 | return false; |
taurin | 0:e052602db102 | 92 | } |
taurin | 0:e052602db102 | 93 | VCmonitor.setCurrentCalibration(); |
taurin | 0:e052602db102 | 94 | return true; |
taurin | 0:e052602db102 | 95 | } |
taurin | 0:e052602db102 | 96 | |
taurin | 0:e052602db102 | 97 | void init(){ |
taurin | 12:fd9d241843f4 | 98 | if(LRstatePin){ |
taurin | 13:5e3b4120dbbf | 99 | eruronTrim = ERURON_TRIM_INI_L; |
taurin | 13:5e3b4120dbbf | 100 | drugTrim = DRUG_TRIM_INI_L; |
taurin | 13:5e3b4120dbbf | 101 | eruronMoveDeg = ERURON_MOVE_DEG_INI_L; |
taurin | 13:5e3b4120dbbf | 102 | drugMoveDeg = DRUG_MOVE_DEG_INI_L; |
taurin | 13:5e3b4120dbbf | 103 | } |
taurin | 13:5e3b4120dbbf | 104 | else{ |
taurin | 12:fd9d241843f4 | 105 | eruronTrim = ERURON_TRIM_INI_R; |
taurin | 12:fd9d241843f4 | 106 | drugTrim = DRUG_TRIM_INI_R; |
taurin | 12:fd9d241843f4 | 107 | eruronMoveDeg = ERURON_MOVE_DEG_INI_R; |
taurin | 13:5e3b4120dbbf | 108 | drugMoveDeg =DRUG_MOVE_DEG_INI_R; |
taurin | 12:fd9d241843f4 | 109 | } |
taurin | 0:e052602db102 | 110 | SERVO_FLAG = servoInit(); |
taurin | 0:e052602db102 | 111 | ADXL_FLAG = adxlInit(); |
taurin | 0:e052602db102 | 112 | INA_FLAG = inaInit(); |
taurin | 4:450cafd95ac3 | 113 | sendDatasTicker.attach(&sendDatas,SEND_DATAS_LOOP_TIME); |
taurin | 4:450cafd95ac3 | 114 | // toStringTicker.attach(&toString,0.5); |
taurin | 0:e052602db102 | 115 | } |
taurin | 0:e052602db102 | 116 | |
taurin | 0:e052602db102 | 117 | void updateDatas(){ |
taurin | 0:e052602db102 | 118 | if(ADXL_FLAG){ |
taurin | 0:e052602db102 | 119 | accelerometer.getOutput(acc); |
taurin | 0:e052602db102 | 120 | } |
taurin | 0:e052602db102 | 121 | if(INA_FLAG){ |
taurin | 0:e052602db102 | 122 | int tmp = VCmonitor.getVoltage(&V); |
taurin | 0:e052602db102 | 123 | tmp = VCmonitor.getCurrent(&C); |
taurin | 0:e052602db102 | 124 | } |
taurin | 0:e052602db102 | 125 | for(int i = 0; i < 3; i++){ |
taurin | 0:e052602db102 | 126 | toSendDatas[i] = acc[i]; |
taurin | 0:e052602db102 | 127 | } |
taurin | 0:e052602db102 | 128 | toSendDatas[3] = (char)(V/100); |
taurin | 0:e052602db102 | 129 | } |
taurin | 0:e052602db102 | 130 | |
taurin | 0:e052602db102 | 131 | void receiveDatas(){ |
taurin | 0:e052602db102 | 132 | if(can.read(recmsg)){ |
taurin | 2:7fcb4f970a02 | 133 | for(int i = 0; i < CONTROL_VALUES_NUM; i++){ |
YusukeWakuta | 3:4417217b4f66 | 134 | controlValues[i] = recmsg.data[i]; |
taurin | 0:e052602db102 | 135 | } |
taurin | 1:9cc932a16d17 | 136 | led1 = !led1; |
taurin | 0:e052602db102 | 137 | } |
taurin | 0:e052602db102 | 138 | } |
taurin | 0:e052602db102 | 139 | |
taurin | 0:e052602db102 | 140 | void toString(){ |
taurin | 4:450cafd95ac3 | 141 | for(int i = 0; i <CONTROL_VALUES_NUM; i++){ |
taurin | 4:450cafd95ac3 | 142 | pc.printf("%d, ",controlValues[i]); |
taurin | 4:450cafd95ac3 | 143 | } |
taurin | 4:450cafd95ac3 | 144 | pc.printf("\n\r"); |
taurin | 0:e052602db102 | 145 | } |
taurin | 0:e052602db102 | 146 | |
taurin | 1:9cc932a16d17 | 147 | double calcPulse(int deg){ |
taurin | 4:450cafd95ac3 | 148 | return (0.0006+(deg/180.0)*(0.00235-0.00045)); |
taurin | 4:450cafd95ac3 | 149 | |
taurin | 1:9cc932a16d17 | 150 | } |
taurin | 1:9cc932a16d17 | 151 | |
taurin | 1:9cc932a16d17 | 152 | void WriteServo(){ |
taurin | 12:fd9d241843f4 | 153 | //if(debugServoPin){ |
taurin | 12:fd9d241843f4 | 154 | // led3 = 1; |
taurin | 12:fd9d241843f4 | 155 | // float a = eruronAna.read()*180; |
taurin | 12:fd9d241843f4 | 156 | // float b = drugAna.read()*180; |
taurin | 12:fd9d241843f4 | 157 | // eruronServo.pulsewidth(calcPulse(eruronAna.read()*180)); |
taurin | 12:fd9d241843f4 | 158 | // drugServo.pulsewidth(calcPulse(drugAna.read()*180)); |
taurin | 12:fd9d241843f4 | 159 | // pc.printf("eruron:%f drug:%f\n\r",a,b); |
taurin | 12:fd9d241843f4 | 160 | // } |
taurin | 12:fd9d241843f4 | 161 | // else{ |
taurin | 12:fd9d241843f4 | 162 | // led3 = 0; |
taurin | 4:450cafd95ac3 | 163 | eruronServo.pulsewidth(calcPulse(eruronTrim+eruronMoveDeg*(controlValues[0]-1))); |
taurin | 4:450cafd95ac3 | 164 | drugServo.pulsewidth(calcPulse(drugTrim+drugMoveDeg*controlValues[1])); |
taurin | 12:fd9d241843f4 | 165 | //} |
taurin | 4:450cafd95ac3 | 166 | } |
taurin | 4:450cafd95ac3 | 167 | |
taurin | 4:450cafd95ac3 | 168 | void setTrim(){ |
taurin | 4:450cafd95ac3 | 169 | debugLED = 1; |
taurin | 12:fd9d241843f4 | 170 | if(EDstatePin){ |
taurin | 4:450cafd95ac3 | 171 | eruronTrim = eruronAna.read()*180; |
taurin | 12:fd9d241843f4 | 172 | eruronServo.pulsewidth(calcPulse(eruronTrim)); |
taurin | 12:fd9d241843f4 | 173 | } |
taurin | 12:fd9d241843f4 | 174 | else{ |
taurin | 4:450cafd95ac3 | 175 | drugTrim = drugAna.read()*180; |
taurin | 4:450cafd95ac3 | 176 | drugServo.pulsewidth(calcPulse(drugTrim)); |
taurin | 12:fd9d241843f4 | 177 | } |
YusukeWakuta | 14:1f6dd929d7de | 178 | //pc.printf("eruronTrim:%f drugTrim:%f\n\r",eruronTrim,drugTrim); |
YusukeWakuta | 14:1f6dd929d7de | 179 | pc.printf("eruronTrim:%f drugTrim:%f ",eruronTrim,drugTrim); |
YusukeWakuta | 14:1f6dd929d7de | 180 | pc.printf("eMD:%f dMD:%f\n\r",eruronMoveDeg,drugMoveDeg); |
taurin | 4:450cafd95ac3 | 181 | } |
taurin | 4:450cafd95ac3 | 182 | |
taurin | 4:450cafd95ac3 | 183 | void checkMaxDeg(){ |
taurin | 4:450cafd95ac3 | 184 | led4 = 1; |
taurin | 4:450cafd95ac3 | 185 | float eruronTemp = eruronAna.read()*180; |
taurin | 4:450cafd95ac3 | 186 | float drugTemp = drugAna.read()*180; |
taurin | 12:fd9d241843f4 | 187 | if(EDstatePin){ |
taurin | 4:450cafd95ac3 | 188 | eruronServo.pulsewidth(calcPulse(eruronTemp)); |
taurin | 12:fd9d241843f4 | 189 | eruronMoveDeg = eruronTemp-eruronTrim; |
taurin | 12:fd9d241843f4 | 190 | } |
taurin | 12:fd9d241843f4 | 191 | else{ |
taurin | 4:450cafd95ac3 | 192 | drugServo.pulsewidth(calcPulse(drugTemp)); |
taurin | 4:450cafd95ac3 | 193 | drugMoveDeg = drugTemp-drugTrim; |
taurin | 12:fd9d241843f4 | 194 | } |
YusukeWakuta | 14:1f6dd929d7de | 195 | // pc.printf("eMD:%f dMD:%f\n\r",eruronMoveDeg,drugMoveDeg); |
YusukeWakuta | 14:1f6dd929d7de | 196 | pc.printf("eruronTrim:%f drugTrim:%f ",eruronTrim,drugTrim); |
YusukeWakuta | 14:1f6dd929d7de | 197 | pc.printf("eMD:%f dMD:%f\n\r",eruronMoveDeg,drugMoveDeg); |
taurin | 4:450cafd95ac3 | 198 | wait_us(10); |
taurin | 1:9cc932a16d17 | 199 | } |
taurin | 0:e052602db102 | 200 | |
taurin | 0:e052602db102 | 201 | int main(){ |
taurin | 0:e052602db102 | 202 | init(); |
taurin | 0:e052602db102 | 203 | while(1){ |
taurin | 4:450cafd95ac3 | 204 | while(setTrimPin){ |
taurin | 4:450cafd95ac3 | 205 | setTrim(); |
taurin | 4:450cafd95ac3 | 206 | } |
taurin | 4:450cafd95ac3 | 207 | while(checkMaxDegPin){ |
taurin | 4:450cafd95ac3 | 208 | checkMaxDeg(); |
taurin | 4:450cafd95ac3 | 209 | } |
YusukeWakuta | 14:1f6dd929d7de | 210 | // pc.printf("eT:%f\n\r",eruronTrim); |
YusukeWakuta | 14:1f6dd929d7de | 211 | pc.printf("eruronTrim:%f drugTrim:%f ",eruronTrim,drugTrim); |
YusukeWakuta | 14:1f6dd929d7de | 212 | pc.printf("eMD:%f dMD:%f\n\r",eruronMoveDeg,drugMoveDeg); |
taurin | 4:450cafd95ac3 | 213 | led4 = 0; |
YusukeWakuta | 14:1f6dd929d7de | 214 | |
taurin | 4:450cafd95ac3 | 215 | debugLED = 0; |
taurin | 0:e052602db102 | 216 | receiveDatas(); |
taurin | 1:9cc932a16d17 | 217 | WriteServo(); |
taurin | 4:450cafd95ac3 | 218 | updateDatas(); |
taurin | 2:7fcb4f970a02 | 219 | wait(WAIT_LOOP_TIME); |
taurin | 0:e052602db102 | 220 | } |
taurin | 0:e052602db102 | 221 | } |