ControlMainでの変更に対応して、新しくレポジトリを作りました

Dependencies:   Control_Yokutan_CANver1 XBusServo mbed mbed-rtos

Fork of ControlYokutan2017 by albatross

Committer:
YusukeWakuta
Date:
Wed Feb 15 13:27:52 2017 +0000
Branch:
XBus???
Revision:
24:d416722b4aad
Parent:
22:b38bc18ec3a1
Child:
25:e8bfb629e1b1
XBus??????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taurin 0:e052602db102 1 //翼端can program
taurin 0:e052602db102 2 #include "mbed.h"
tsumagari 22:b38bc18ec3a1 3 //#include "ADXL345_I2C.h"
tsumagari 22:b38bc18ec3a1 4 #include "MPU6050.h"
taurin 0:e052602db102 5 #include "INA226.hpp"
YusukeWakuta 24:d416722b4aad 6 #include "XBusServo.h"
taurin 4:450cafd95ac3 7
tsumagari 22:b38bc18ec3a1 8 #define TO_SEND_DATAS_NUM 7
taurin 2:7fcb4f970a02 9 #define INIT_SERVO_PERIOD_MS 20
taurin 4:450cafd95ac3 10 #define WAIT_LOOP_TIME 0.02
taurin 0:e052602db102 11 #define CONTROL_VALUES_NUM 2
taurin 0:e052602db102 12 #define TO_SEND_CAN_ID 100
taurin 4:450cafd95ac3 13 #define ADXL_MEAN_NUM 10
taurin 4:450cafd95ac3 14 #define SEND_DATAS_LOOP_TIME 0.1
taurin 16:82310bf7c326 15 #define RECEIVE_DATAS_LOOP_TIME 0.05
taurin 12:fd9d241843f4 16
YusukeWakuta 21:8a621c81507d 17 #define ERURON_MOVE_DEG_INI_R 10 // もともと10
YusukeWakuta 18:253f0526b8b3 18 #define DRUG_MOVE_DEG_INI_R 76
YusukeWakuta 20:a5a85df4de13 19 #define ERURON_TRIM_INI_R 97 //元々94 
YusukeWakuta 18:253f0526b8b3 20 #define DRUG_TRIM_INI_R 33
taurin 12:fd9d241843f4 21
YusukeWakuta 21:8a621c81507d 22 #define ERURON_MOVE_DEG_INI_L -8 //もともと-7
YusukeWakuta 17:d91ce54dd09a 23 #define DRUG_MOVE_DEG_INI_L -80
YusukeWakuta 20:a5a85df4de13 24 #define ERURON_TRIM_INI_L 113 //元々95
YusukeWakuta 17:d91ce54dd09a 25 #define DRUG_TRIM_INI_L 110
taurin 2:7fcb4f970a02 26
YusukeWakuta 24:d416722b4aad 27 #define kXBusTx p9
YusukeWakuta 24:d416722b4aad 28 #define kMaxServoNum 1 // 1 - 50
YusukeWakuta 24:d416722b4aad 29 #define kMaxServoPause (sizeof(motionData) / sizeof(pauseRec))
YusukeWakuta 24:d416722b4aad 30 #define kMotionInterval 10 // flame / sec
YusukeWakuta 24:d416722b4aad 31 #define kMotionMinMark 0x1249
YusukeWakuta 24:d416722b4aad 32 #define kMotionEndMark 0xED86
YusukeWakuta 24:d416722b4aad 33
taurin 0:e052602db102 34 CAN can(p30,p29);
taurin 0:e052602db102 35 CANMessage recmsg;
taurin 0:e052602db102 36 Serial pc(USBTX,USBRX);
tsumagari 22:b38bc18ec3a1 37 //ADXL345_I2C accelerometer(p9, p10);
tsumagari 22:b38bc18ec3a1 38 MPU6050 mpu(p9,p10);
taurin 0:e052602db102 39 I2C ina226_i2c(p28,p27);
taurin 0:e052602db102 40 INA226 VCmonitor(ina226_i2c);
taurin 13:5e3b4120dbbf 41 PwmOut drugServo(p22);
taurin 13:5e3b4120dbbf 42 PwmOut eruronServo(p23);
taurin 1:9cc932a16d17 43 DigitalOut led1(LED1);
taurin 4:450cafd95ac3 44 AnalogIn drugAna(p20);
taurin 4:450cafd95ac3 45 AnalogIn eruronAna(p19);
taurin 12:fd9d241843f4 46 DigitalIn LRstatePin(p14);
taurin 4:450cafd95ac3 47 DigitalIn setTrimPin(p15);
taurin 12:fd9d241843f4 48 DigitalIn EDstatePin(p16);
taurin 4:450cafd95ac3 49 DigitalIn checkMaxDegPin(p17);
taurin 4:450cafd95ac3 50 DigitalOut debugLED(LED2);
taurin 4:450cafd95ac3 51 DigitalOut led3(LED3);
taurin 4:450cafd95ac3 52 DigitalOut led4(LED4);
taurin 4:450cafd95ac3 53 Ticker sendDatasTicker;
tsumagari 22:b38bc18ec3a1 54 //Ticker toStringTicker;
taurin 16:82310bf7c326 55 Ticker receiveDatasTicker;
taurin 16:82310bf7c326 56
taurin 0:e052602db102 57 char toSendDatas[TO_SEND_DATAS_NUM];
taurin 1:9cc932a16d17 58 char controlValues[CONTROL_VALUES_NUM];//0:eruruon,1:drug
taurin 0:e052602db102 59
taurin 12:fd9d241843f4 60 float eruronTrim;
taurin 12:fd9d241843f4 61 float drugTrim;
taurin 12:fd9d241843f4 62 float eruronMoveDeg;
taurin 12:fd9d241843f4 63 float drugMoveDeg;
taurin 0:e052602db102 64 unsigned short ina_val;
taurin 0:e052602db102 65 double V,C;
taurin 0:e052602db102 66 bool SERVO_FLAG;
taurin 0:e052602db102 67 bool ADXL_FLAG;
taurin 0:e052602db102 68 bool INA_FLAG;
tsumagari 22:b38bc18ec3a1 69 bool MPU_FLAG;
taurin 0:e052602db102 70
tsumagari 22:b38bc18ec3a1 71 //int16_t acc[3] = {0,0,0};
tsumagari 22:b38bc18ec3a1 72 char gyro_c[6] = {0,0,0,0,0,0};
tsumagari 22:b38bc18ec3a1 73 //char acc_mean[3][ADXL_MEAN_NUM];
tsumagari 22:b38bc18ec3a1 74 //int adxl_mean_counter = 0;
taurin 4:450cafd95ac3 75
taurin 4:450cafd95ac3 76 void toString();
taurin 16:82310bf7c326 77 void receiveDatas();
taurin 16:82310bf7c326 78 void WriteServo();
taurin 0:e052602db102 79
YusukeWakuta 24:d416722b4aad 80 static const uint8_t servoChannel = 0x01;
YusukeWakuta 24:d416722b4aad 81
YusukeWakuta 24:d416722b4aad 82 XBusServo gXBus(kXBusTx, NC, NC, kMaxServoNum);
YusukeWakuta 24:d416722b4aad 83 Ticker gTimer;
YusukeWakuta 24:d416722b4aad 84
taurin 0:e052602db102 85 bool servoInit(){
taurin 4:450cafd95ac3 86 drugServo.period_ms(INIT_SERVO_PERIOD_MS);
taurin 4:450cafd95ac3 87 eruronServo.period_ms(INIT_SERVO_PERIOD_MS);
taurin 0:e052602db102 88 return true;
taurin 0:e052602db102 89 }
taurin 0:e052602db102 90
YusukeWakuta 24:d416722b4aad 91 //=============================================================
YusukeWakuta 24:d416722b4aad 92 // XbusIntervalHandler()
YusukeWakuta 24:d416722b4aad 93 // play motion !
YusukeWakuta 24:d416722b4aad 94 //=============================================================
YusukeWakuta 24:d416722b4aad 95 void XbusIntervalHandler()
YusukeWakuta 24:d416722b4aad 96 {
YusukeWakuta 24:d416722b4aad 97 uint16_t value;
YusukeWakuta 24:d416722b4aad 98 uint16_t diff = kMotionEndMark - kMotionMinMark;
YusukeWakuta 24:d416722b4aad 99 value = (uint16_t)(diff * analog.read()) + kMotionMinMark;
YusukeWakuta 24:d416722b4aad 100 gXBus.setServo(servoChannel, value);
YusukeWakuta 24:d416722b4aad 101 gXBus.sendChannelDataPacket();
YusukeWakuta 24:d416722b4aad 102 }
YusukeWakuta 24:d416722b4aad 103
tsumagari 22:b38bc18ec3a1 104 //bool adxlInit(){
tsumagari 22:b38bc18ec3a1 105 // accelerometer.setPowerControl(0x00);
tsumagari 22:b38bc18ec3a1 106 // accelerometer.setDataFormatControl(0x0B);
tsumagari 22:b38bc18ec3a1 107 // accelerometer.setDataRate(ADXL345_3200HZ);
tsumagari 22:b38bc18ec3a1 108 // accelerometer.setPowerControl(0x08);
tsumagari 22:b38bc18ec3a1 109 // return true;
tsumagari 22:b38bc18ec3a1 110 //}
taurin 0:e052602db102 111
taurin 4:450cafd95ac3 112 void sendDatas(){
taurin 4:450cafd95ac3 113 if(can.write(CANMessage(TO_SEND_CAN_ID, toSendDatas, TO_SEND_DATAS_NUM))){
taurin 4:450cafd95ac3 114 }
taurin 4:450cafd95ac3 115 }
taurin 4:450cafd95ac3 116
taurin 0:e052602db102 117 bool inaInit(){
taurin 0:e052602db102 118 if(!VCmonitor.isExist()){
taurin 0:e052602db102 119 pc.printf("VCmonitor NOT FOUND\n");
taurin 0:e052602db102 120 return false;
taurin 0:e052602db102 121 }
taurin 0:e052602db102 122 ina_val = 0;
taurin 0:e052602db102 123 if(VCmonitor.rawRead(0x00,&ina_val) != 0){
taurin 0:e052602db102 124 pc.printf("VCmonitor READ ERROR\n");
taurin 0:e052602db102 125 return false;
taurin 0:e052602db102 126 }
taurin 0:e052602db102 127 VCmonitor.setCurrentCalibration();
taurin 0:e052602db102 128 return true;
taurin 0:e052602db102 129 }
taurin 0:e052602db102 130
YusukeWakuta 24:d416722b4aad 131 XBusError init(){
YusukeWakuta 24:d416722b4aad 132
YusukeWakuta 24:d416722b4aad 133 XBusError result;
YusukeWakuta 24:d416722b4aad 134
taurin 15:1db5ee4fe7ce 135 if(!LRstatePin){
taurin 13:5e3b4120dbbf 136 eruronTrim = ERURON_TRIM_INI_L;
taurin 13:5e3b4120dbbf 137 drugTrim = DRUG_TRIM_INI_L;
taurin 13:5e3b4120dbbf 138 eruronMoveDeg = ERURON_MOVE_DEG_INI_L;
taurin 13:5e3b4120dbbf 139 drugMoveDeg = DRUG_MOVE_DEG_INI_L;
taurin 13:5e3b4120dbbf 140 }
taurin 13:5e3b4120dbbf 141 else{
taurin 12:fd9d241843f4 142 eruronTrim = ERURON_TRIM_INI_R;
taurin 12:fd9d241843f4 143 drugTrim = DRUG_TRIM_INI_R;
taurin 12:fd9d241843f4 144 eruronMoveDeg = ERURON_MOVE_DEG_INI_R;
taurin 13:5e3b4120dbbf 145 drugMoveDeg =DRUG_MOVE_DEG_INI_R;
taurin 12:fd9d241843f4 146 }
taurin 0:e052602db102 147 SERVO_FLAG = servoInit();
tsumagari 22:b38bc18ec3a1 148 // ADXL_FLAG = adxlInit();
tsumagari 22:b38bc18ec3a1 149 MPU_FLAG = mpu.testConnection();
taurin 0:e052602db102 150 INA_FLAG = inaInit();
taurin 4:450cafd95ac3 151 sendDatasTicker.attach(&sendDatas,SEND_DATAS_LOOP_TIME);
taurin 16:82310bf7c326 152 // toStringTicker.attach(&toString,0.5);
taurin 16:82310bf7c326 153 receiveDatasTicker.attach(&receiveDatas,RECEIVE_DATAS_LOOP_TIME);
YusukeWakuta 24:d416722b4aad 154
YusukeWakuta 24:d416722b4aad 155 //XBus
YusukeWakuta 24:d416722b4aad 156 // initialize XBus
YusukeWakuta 24:d416722b4aad 157 result = gXBus.start();
YusukeWakuta 24:d416722b4aad 158 if (result != kXBusError_NoError) {
YusukeWakuta 24:d416722b4aad 159 gXBus.stop();
YusukeWakuta 24:d416722b4aad 160 return result;
YusukeWakuta 24:d416722b4aad 161 }
YusukeWakuta 24:d416722b4aad 162 // initialize XBus Servos
YusukeWakuta 24:d416722b4aad 163 result = gXBus.addServo(servoChannel, kXbusServoNeutral);
YusukeWakuta 24:d416722b4aad 164 if (result != kXBusError_NoError) {
YusukeWakuta 24:d416722b4aad 165 gXBus.stop();
YusukeWakuta 24:d416722b4aad 166 return result;
YusukeWakuta 24:d416722b4aad 167 }
YusukeWakuta 24:d416722b4aad 168
YusukeWakuta 24:d416722b4aad 169 return kXBusError_NoError;
taurin 0:e052602db102 170 }
taurin 0:e052602db102 171
taurin 0:e052602db102 172 void updateDatas(){
tsumagari 22:b38bc18ec3a1 173 // if(ADXL_FLAG){
tsumagari 22:b38bc18ec3a1 174 //// accelerometer.getOutput(acc);
tsumagari 22:b38bc18ec3a1 175 // }
taurin 0:e052602db102 176 if(INA_FLAG){
taurin 0:e052602db102 177 int tmp = VCmonitor.getVoltage(&V);
taurin 0:e052602db102 178 tmp = VCmonitor.getCurrent(&C);
taurin 0:e052602db102 179 }
tsumagari 22:b38bc18ec3a1 180 if(MPU_FLAG){
tsumagari 22:b38bc18ec3a1 181 mpu.read(MPU6050_GYRO_XOUT_H_REG, gyro_c, 6);
taurin 0:e052602db102 182 }
tsumagari 22:b38bc18ec3a1 183 for(int i = 0; i < TO_SEND_DATAS_NUM - 1; i++){
tsumagari 22:b38bc18ec3a1 184 // toSendDatas[i] = acc[i];
tsumagari 22:b38bc18ec3a1 185 toSendDatas[i] = gyro_c[i];
tsumagari 22:b38bc18ec3a1 186 }
tsumagari 22:b38bc18ec3a1 187 // toSendDatas[TO_SEND_DATAS_NUM - 1] = (char)(V/100);
tsumagari 22:b38bc18ec3a1 188 toSendDatas[TO_SEND_DATAS_NUM - 1] = (char)77;
taurin 0:e052602db102 189 }
taurin 0:e052602db102 190
taurin 0:e052602db102 191 void receiveDatas(){
taurin 0:e052602db102 192 if(can.read(recmsg)){
taurin 2:7fcb4f970a02 193 for(int i = 0; i < CONTROL_VALUES_NUM; i++){
YusukeWakuta 3:4417217b4f66 194 controlValues[i] = recmsg.data[i];
taurin 0:e052602db102 195 }
taurin 1:9cc932a16d17 196 led1 = !led1;
taurin 16:82310bf7c326 197 //WriteServo();
taurin 0:e052602db102 198 }
taurin 0:e052602db102 199 }
taurin 0:e052602db102 200
taurin 0:e052602db102 201 void toString(){
taurin 4:450cafd95ac3 202 for(int i = 0; i <CONTROL_VALUES_NUM; i++){
taurin 4:450cafd95ac3 203 pc.printf("%d, ",controlValues[i]);
taurin 4:450cafd95ac3 204 }
taurin 4:450cafd95ac3 205 pc.printf("\n\r");
taurin 0:e052602db102 206 }
taurin 0:e052602db102 207
taurin 1:9cc932a16d17 208 double calcPulse(int deg){
taurin 4:450cafd95ac3 209 return (0.0006+(deg/180.0)*(0.00235-0.00045));
taurin 4:450cafd95ac3 210
taurin 1:9cc932a16d17 211 }
taurin 1:9cc932a16d17 212
taurin 1:9cc932a16d17 213 void WriteServo(){
taurin 12:fd9d241843f4 214 //if(debugServoPin){
taurin 12:fd9d241843f4 215 // led3 = 1;
taurin 12:fd9d241843f4 216 // float a = eruronAna.read()*180;
taurin 12:fd9d241843f4 217 // float b = drugAna.read()*180;
taurin 12:fd9d241843f4 218 // eruronServo.pulsewidth(calcPulse(eruronAna.read()*180));
taurin 12:fd9d241843f4 219 // drugServo.pulsewidth(calcPulse(drugAna.read()*180));
taurin 12:fd9d241843f4 220 // pc.printf("eruron:%f drug:%f\n\r",a,b);
taurin 12:fd9d241843f4 221 // }
taurin 12:fd9d241843f4 222 // else{
taurin 12:fd9d241843f4 223 // led3 = 0;
taurin 4:450cafd95ac3 224 eruronServo.pulsewidth(calcPulse(eruronTrim+eruronMoveDeg*(controlValues[0]-1)));
taurin 4:450cafd95ac3 225 drugServo.pulsewidth(calcPulse(drugTrim+drugMoveDeg*controlValues[1]));
taurin 12:fd9d241843f4 226 //}
taurin 4:450cafd95ac3 227 }
taurin 4:450cafd95ac3 228
taurin 4:450cafd95ac3 229 void setTrim(){
taurin 4:450cafd95ac3 230 debugLED = 1;
taurin 12:fd9d241843f4 231 if(EDstatePin){
taurin 4:450cafd95ac3 232 eruronTrim = eruronAna.read()*180;
taurin 12:fd9d241843f4 233 eruronServo.pulsewidth(calcPulse(eruronTrim));
taurin 12:fd9d241843f4 234 }
taurin 12:fd9d241843f4 235 else{
taurin 4:450cafd95ac3 236 drugTrim = drugAna.read()*180;
taurin 4:450cafd95ac3 237 drugServo.pulsewidth(calcPulse(drugTrim));
taurin 12:fd9d241843f4 238 }
YusukeWakuta 14:1f6dd929d7de 239 //pc.printf("eruronTrim:%f drugTrim:%f\n\r",eruronTrim,drugTrim);
YusukeWakuta 14:1f6dd929d7de 240 pc.printf("eruronTrim:%f drugTrim:%f ",eruronTrim,drugTrim);
YusukeWakuta 14:1f6dd929d7de 241 pc.printf("eMD:%f dMD:%f\n\r",eruronMoveDeg,drugMoveDeg);
taurin 4:450cafd95ac3 242 }
taurin 4:450cafd95ac3 243
taurin 4:450cafd95ac3 244 void checkMaxDeg(){
taurin 4:450cafd95ac3 245 led4 = 1;
taurin 4:450cafd95ac3 246 float eruronTemp = eruronAna.read()*180;
taurin 4:450cafd95ac3 247 float drugTemp = drugAna.read()*180;
taurin 12:fd9d241843f4 248 if(EDstatePin){
taurin 4:450cafd95ac3 249 eruronServo.pulsewidth(calcPulse(eruronTemp));
taurin 12:fd9d241843f4 250 eruronMoveDeg = eruronTemp-eruronTrim;
taurin 12:fd9d241843f4 251 }
taurin 12:fd9d241843f4 252 else{
taurin 4:450cafd95ac3 253 drugServo.pulsewidth(calcPulse(drugTemp));
taurin 4:450cafd95ac3 254 drugMoveDeg = drugTemp-drugTrim;
taurin 12:fd9d241843f4 255 }
YusukeWakuta 14:1f6dd929d7de 256 // pc.printf("eMD:%f dMD:%f\n\r",eruronMoveDeg,drugMoveDeg);
YusukeWakuta 14:1f6dd929d7de 257 pc.printf("eruronTrim:%f drugTrim:%f ",eruronTrim,drugTrim);
YusukeWakuta 14:1f6dd929d7de 258 pc.printf("eMD:%f dMD:%f\n\r",eruronMoveDeg,drugMoveDeg);
taurin 4:450cafd95ac3 259 wait_us(10);
taurin 1:9cc932a16d17 260 }
taurin 0:e052602db102 261
taurin 0:e052602db102 262 int main(){
taurin 0:e052602db102 263 init();
YusukeWakuta 24:d416722b4aad 264 XBusError result;
YusukeWakuta 24:d416722b4aad 265
YusukeWakuta 24:d416722b4aad 266 // start motion
YusukeWakuta 24:d416722b4aad 267 gTimer.attach_us(&XbusIntervalHandler, 1000000 / kMotionInterval);
YusukeWakuta 24:d416722b4aad 268
taurin 0:e052602db102 269 while(1){
taurin 4:450cafd95ac3 270 while(setTrimPin){
taurin 4:450cafd95ac3 271 setTrim();
taurin 4:450cafd95ac3 272 }
taurin 4:450cafd95ac3 273 while(checkMaxDegPin){
taurin 4:450cafd95ac3 274 checkMaxDeg();
taurin 4:450cafd95ac3 275 }
YusukeWakuta 14:1f6dd929d7de 276 // pc.printf("eT:%f\n\r",eruronTrim);
YusukeWakuta 14:1f6dd929d7de 277 pc.printf("eruronTrim:%f drugTrim:%f ",eruronTrim,drugTrim);
YusukeWakuta 14:1f6dd929d7de 278 pc.printf("eMD:%f dMD:%f\n\r",eruronMoveDeg,drugMoveDeg);
taurin 4:450cafd95ac3 279 led4 = 0;
YusukeWakuta 14:1f6dd929d7de 280
taurin 4:450cafd95ac3 281 debugLED = 0;
taurin 16:82310bf7c326 282 //receiveDatas();
tsumagari 22:b38bc18ec3a1 283 // sendDatas();
taurin 1:9cc932a16d17 284 WriteServo();
taurin 4:450cafd95ac3 285 updateDatas();
tsumagari 22:b38bc18ec3a1 286 // pc.printf("%6d,%6d,%6d\n\r",gyro[0],gyro[1],gyro[2]);
tsumagari 22:b38bc18ec3a1 287 led3 = !led3;
taurin 2:7fcb4f970a02 288 wait(WAIT_LOOP_TIME);
taurin 0:e052602db102 289 }
taurin 0:e052602db102 290 }