史也 菅
/
can_nodo
way
main.cpp@0:f3625799a474, 2021-09-01 (annotated)
- Committer:
- kurobikari
- Date:
- Wed Sep 01 02:47:08 2021 +0000
- Revision:
- 0:f3625799a474
- Child:
- 1:85bc939a3d22
can
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kurobikari | 0:f3625799a474 | 1 | #define LAP 1 |
kurobikari | 0:f3625799a474 | 2 | #define SMB 0 |
kurobikari | 0:f3625799a474 | 3 | #define EncoderMAX 2 |
kurobikari | 0:f3625799a474 | 4 | |
kurobikari | 0:f3625799a474 | 5 | #include "mbed.h" |
kurobikari | 0:f3625799a474 | 6 | #include "CANMsg.h" |
kurobikari | 0:f3625799a474 | 7 | |
kurobikari | 0:f3625799a474 | 8 | /*************クラス宣言*************/ |
kurobikari | 0:f3625799a474 | 9 | |
kurobikari | 0:f3625799a474 | 10 | /* CANのピン設定 (RD,TD,周波数) */ |
kurobikari | 0:f3625799a474 | 11 | CAN can(PA_11, PA_12,500000);// CAN Rx pin name, CAN Tx pin name |
kurobikari | 0:f3625799a474 | 12 | /* CAN受信クラス宣言 */ |
kurobikari | 0:f3625799a474 | 13 | CANMsg rxMsg; |
kurobikari | 0:f3625799a474 | 14 | /* CAN送信クラス宣言 */ |
kurobikari | 0:f3625799a474 | 15 | CANMsg txMsg; |
kurobikari | 0:f3625799a474 | 16 | /* MDCのOnボードled */ |
kurobikari | 0:f3625799a474 | 17 | DigitalOut led[2]= {PA_8,PA_9}; |
kurobikari | 0:f3625799a474 | 18 | /* 割り込み用クラス */ |
kurobikari | 0:f3625799a474 | 19 | Ticker flipper; |
kurobikari | 0:f3625799a474 | 20 | /* 割り込み用クラス */ |
kurobikari | 0:f3625799a474 | 21 | Ticker interrupt; |
kurobikari | 0:f3625799a474 | 22 | /* I2C障害時間カウント */ |
kurobikari | 0:f3625799a474 | 23 | Timer LostSignal; |
kurobikari | 0:f3625799a474 | 24 | |
kurobikari | 0:f3625799a474 | 25 | /*************関数宣言*************/ |
kurobikari | 0:f3625799a474 | 26 | |
kurobikari | 0:f3625799a474 | 27 | /* タイマ呼び出し用 */ |
kurobikari | 0:f3625799a474 | 28 | void IT_CallBack(void); |
kurobikari | 0:f3625799a474 | 29 | /* 送信用割り込み関数 */ |
kurobikari | 0:f3625799a474 | 30 | void sent(void); |
kurobikari | 0:f3625799a474 | 31 | /* MDの状態決める関数 */ |
kurobikari | 0:f3625799a474 | 32 | void Md_Status(void); |
kurobikari | 0:f3625799a474 | 33 | /* CAN受信割り込み */ |
kurobikari | 0:f3625799a474 | 34 | void onCanReceived(void); |
kurobikari | 0:f3625799a474 | 35 | |
kurobikari | 0:f3625799a474 | 36 | /*************ピン宣言*************/ |
kurobikari | 0:f3625799a474 | 37 | |
kurobikari | 0:f3625799a474 | 38 | /* エンコーダピン設定 */ |
kurobikari | 0:f3625799a474 | 39 | DigitalOut CS(PA_3); |
kurobikari | 0:f3625799a474 | 40 | DigitalOut CL(PA_4); |
kurobikari | 0:f3625799a474 | 41 | DigitalIn DO[] = {PA_5,PB_0}; |
kurobikari | 0:f3625799a474 | 42 | /* MD側出力ピン設定 */ |
kurobikari | 0:f3625799a474 | 43 | PwmOut PWM[2] = {PA_7,PA_6}; |
kurobikari | 0:f3625799a474 | 44 | /* いねーぶるピン設定 */ |
kurobikari | 0:f3625799a474 | 45 | DigitalOut Enable[2] = {PA_0,PB_4}; |
kurobikari | 0:f3625799a474 | 46 | /* INのようなOUTピン設定 */ |
kurobikari | 0:f3625799a474 | 47 | DigitalOut IN[4] = {PA_1,PA_2,PB_3,PA_15}; |
kurobikari | 0:f3625799a474 | 48 | /* アドレスピン設定 */ |
kurobikari | 0:f3625799a474 | 49 | DigitalIn add[3] = {PB_7,PB_6,PB_5}; |
kurobikari | 0:f3625799a474 | 50 | |
kurobikari | 0:f3625799a474 | 51 | |
kurobikari | 0:f3625799a474 | 52 | /*************変数宣言*************/ |
kurobikari | 0:f3625799a474 | 53 | |
kurobikari | 0:f3625799a474 | 54 | /* MDデータ保存用構造体 */ |
kurobikari | 0:f3625799a474 | 55 | struct m_d { |
kurobikari | 0:f3625799a474 | 56 | bool mode; |
kurobikari | 0:f3625799a474 | 57 | bool enable; |
kurobikari | 0:f3625799a474 | 58 | int pwm; |
kurobikari | 0:f3625799a474 | 59 | int code; |
kurobikari | 0:f3625799a474 | 60 | }; |
kurobikari | 0:f3625799a474 | 61 | /* 受信データ一時保存/保存 */ |
kurobikari | 0:f3625799a474 | 62 | char ReadData = 0; |
kurobikari | 0:f3625799a474 | 63 | /* mdデータ保存 */ |
kurobikari | 0:f3625799a474 | 64 | char MdData = 0; |
kurobikari | 0:f3625799a474 | 65 | /* モータの状態一時保存/保存 */ |
kurobikari | 0:f3625799a474 | 66 | struct m_d mdc[2]= {0}; |
kurobikari | 0:f3625799a474 | 67 | /* エンコーダ */ |
kurobikari | 0:f3625799a474 | 68 | static int data[EncoderMAX] = {0}; |
kurobikari | 0:f3625799a474 | 69 | /* エンコーダのデータ入力 */ |
kurobikari | 0:f3625799a474 | 70 | int32_t enc_data[2]= {0}; |
kurobikari | 0:f3625799a474 | 71 | /* 直読みエンコーダ角度保存(degree) */ |
kurobikari | 0:f3625799a474 | 72 | double EncoderDeg[EncoderMAX] = {0}; |
kurobikari | 0:f3625799a474 | 73 | /* アドレス初期化 */ |
kurobikari | 0:f3625799a474 | 74 | int RX_ID = 50; |
kurobikari | 0:f3625799a474 | 75 | int TX_ID = 0; |
kurobikari | 0:f3625799a474 | 76 | |
kurobikari | 0:f3625799a474 | 77 | int main(void) |
kurobikari | 0:f3625799a474 | 78 | { |
kurobikari | 0:f3625799a474 | 79 | /* アドレス代入 */ |
kurobikari | 0:f3625799a474 | 80 | TX_ID += !add[2]<<2|!add[1]<<1|!add[0]; |
kurobikari | 0:f3625799a474 | 81 | RX_ID += !add[2]<<2|!add[1]<<1|!add[0]; |
kurobikari | 0:f3625799a474 | 82 | |
kurobikari | 0:f3625799a474 | 83 | /* can受信割り込み */ |
kurobikari | 0:f3625799a474 | 84 | can.attach(onCanReceived); |
kurobikari | 0:f3625799a474 | 85 | |
kurobikari | 0:f3625799a474 | 86 | /* pwm周期 */ |
kurobikari | 0:f3625799a474 | 87 | PWM[0].period_us(50); |
kurobikari | 0:f3625799a474 | 88 | PWM[1].period_us(50); |
kurobikari | 0:f3625799a474 | 89 | |
kurobikari | 0:f3625799a474 | 90 | /* 通信キレ感知ダイマースタート */ |
kurobikari | 0:f3625799a474 | 91 | LostSignal.reset(); |
kurobikari | 0:f3625799a474 | 92 | LostSignal.start(); |
kurobikari | 0:f3625799a474 | 93 | |
kurobikari | 0:f3625799a474 | 94 | /* エンコーダタイマ割り込み */ |
kurobikari | 0:f3625799a474 | 95 | flipper.attach_us(&IT_CallBack, 100); |
kurobikari | 0:f3625799a474 | 96 | /* 送信用タイマ割り込み */ |
kurobikari | 0:f3625799a474 | 97 | interrupt.attach_us(&sent, 2600); |
kurobikari | 0:f3625799a474 | 98 | |
kurobikari | 0:f3625799a474 | 99 | while(1) { |
kurobikari | 0:f3625799a474 | 100 | |
kurobikari | 0:f3625799a474 | 101 | for(int i=0; i<2; i++) { |
kurobikari | 0:f3625799a474 | 102 | /* SMBの時 */ |
kurobikari | 0:f3625799a474 | 103 | if(mdc[i].mode==0) { |
kurobikari | 0:f3625799a474 | 104 | PWM[i] = mdc[i].pwm /100.0; |
kurobikari | 0:f3625799a474 | 105 | |
kurobikari | 0:f3625799a474 | 106 | if(mdc[i].code==1) { |
kurobikari | 0:f3625799a474 | 107 | MdData |= (1<<!i*3+1); |
kurobikari | 0:f3625799a474 | 108 | MdData &= ~(1<<!i*3+2); |
kurobikari | 0:f3625799a474 | 109 | } else { |
kurobikari | 0:f3625799a474 | 110 | MdData |= (1<<!i*3+2); |
kurobikari | 0:f3625799a474 | 111 | MdData &= ~(1<<!i*3+1); |
kurobikari | 0:f3625799a474 | 112 | } |
kurobikari | 0:f3625799a474 | 113 | } |
kurobikari | 0:f3625799a474 | 114 | /* LAPの時 */ |
kurobikari | 0:f3625799a474 | 115 | else { |
kurobikari | 0:f3625799a474 | 116 | PWM[i] = (mdc[i].pwm*mdc[i].code+100) /200.0; |
kurobikari | 0:f3625799a474 | 117 | MdData |= 0b111<<!i*3; |
kurobikari | 0:f3625799a474 | 118 | } |
kurobikari | 0:f3625799a474 | 119 | } |
kurobikari | 0:f3625799a474 | 120 | |
kurobikari | 0:f3625799a474 | 121 | /* INの状態を決める */ |
kurobikari | 0:f3625799a474 | 122 | Md_Status(); |
kurobikari | 0:f3625799a474 | 123 | |
kurobikari | 0:f3625799a474 | 124 | /* 信号ロスト1000msで停止 */ |
kurobikari | 0:f3625799a474 | 125 | if (LostSignal.read_ms() > 1000) { |
kurobikari | 0:f3625799a474 | 126 | Enable[0] = 0; |
kurobikari | 0:f3625799a474 | 127 | Enable[1] = 0; |
kurobikari | 0:f3625799a474 | 128 | led[0]=0; |
kurobikari | 0:f3625799a474 | 129 | } else led[0]=1; |
kurobikari | 0:f3625799a474 | 130 | } |
kurobikari | 0:f3625799a474 | 131 | } |
kurobikari | 0:f3625799a474 | 132 | |
kurobikari | 0:f3625799a474 | 133 | /*************受信割り込み関数************/ |
kurobikari | 0:f3625799a474 | 134 | void onCanReceived(void) |
kurobikari | 0:f3625799a474 | 135 | { |
kurobikari | 0:f3625799a474 | 136 | can.read(rxMsg); |
kurobikari | 0:f3625799a474 | 137 | |
kurobikari | 0:f3625799a474 | 138 | if (rxMsg.id == RX_ID) { |
kurobikari | 0:f3625799a474 | 139 | |
kurobikari | 0:f3625799a474 | 140 | /* LAP or SMB を決めるデータ */ |
kurobikari | 0:f3625799a474 | 141 | ReadData = rxMsg.data[0]; |
kurobikari | 0:f3625799a474 | 142 | /* LAP or SMB 保存 */ |
kurobikari | 0:f3625799a474 | 143 | mdc[0].mode = ReadData&1; |
kurobikari | 0:f3625799a474 | 144 | mdc[1].mode = (ReadData>>1)&1; |
kurobikari | 0:f3625799a474 | 145 | /* PWM1 */ |
kurobikari | 0:f3625799a474 | 146 | mdc[0].code = (rxMsg.data[1]&(1<<7)) != 0 ? -1 : 1;//負のビット検知 |
kurobikari | 0:f3625799a474 | 147 | mdc[0].pwm = rxMsg.data[1]&~(1<<7); |
kurobikari | 0:f3625799a474 | 148 | /* PWM2 */ |
kurobikari | 0:f3625799a474 | 149 | mdc[1].code = (rxMsg.data[2]&(1<<7)) != 0 ? -1 : 1;//負のビット検知 |
kurobikari | 0:f3625799a474 | 150 | mdc[1].pwm = rxMsg.data[2]&~(1<<7); |
kurobikari | 0:f3625799a474 | 151 | /* 信号ロストタイマリセット */ |
kurobikari | 0:f3625799a474 | 152 | LostSignal.reset(); |
kurobikari | 0:f3625799a474 | 153 | LostSignal.start(); |
kurobikari | 0:f3625799a474 | 154 | } |
kurobikari | 0:f3625799a474 | 155 | } |
kurobikari | 0:f3625799a474 | 156 | |
kurobikari | 0:f3625799a474 | 157 | /*************エンコーダ取得タイマ割り込み************/ |
kurobikari | 0:f3625799a474 | 158 | void IT_CallBack(void) |
kurobikari | 0:f3625799a474 | 159 | { |
kurobikari | 0:f3625799a474 | 160 | static int cnt = 0; |
kurobikari | 0:f3625799a474 | 161 | |
kurobikari | 0:f3625799a474 | 162 | switch(cnt) { |
kurobikari | 0:f3625799a474 | 163 | /* 最初の処理 */ |
kurobikari | 0:f3625799a474 | 164 | case 0: |
kurobikari | 0:f3625799a474 | 165 | data[0] = 0; |
kurobikari | 0:f3625799a474 | 166 | data[1] = 0; |
kurobikari | 0:f3625799a474 | 167 | CS = 0; |
kurobikari | 0:f3625799a474 | 168 | CL = 1; |
kurobikari | 0:f3625799a474 | 169 | break; |
kurobikari | 0:f3625799a474 | 170 | /* 最後の処理 */ |
kurobikari | 0:f3625799a474 | 171 | case 25: |
kurobikari | 0:f3625799a474 | 172 | CS=1; |
kurobikari | 0:f3625799a474 | 173 | /* 通常の処理 */ |
kurobikari | 0:f3625799a474 | 174 | default: |
kurobikari | 0:f3625799a474 | 175 | CL=!CL; |
kurobikari | 0:f3625799a474 | 176 | /* 最初でも最後でもなく奇数回で最初以外の時読み取り処理 */ |
kurobikari | 0:f3625799a474 | 177 | if(cnt != 1 && cnt % 2) { |
kurobikari | 0:f3625799a474 | 178 | data[0] |= (DO[0]==1); |
kurobikari | 0:f3625799a474 | 179 | data[1] |= (DO[1]==1); |
kurobikari | 0:f3625799a474 | 180 | |
kurobikari | 0:f3625799a474 | 181 | data[0] = data[0] << 1; |
kurobikari | 0:f3625799a474 | 182 | data[1] = data[1] << 1; |
kurobikari | 0:f3625799a474 | 183 | } |
kurobikari | 0:f3625799a474 | 184 | break; |
kurobikari | 0:f3625799a474 | 185 | } |
kurobikari | 0:f3625799a474 | 186 | cnt++; |
kurobikari | 0:f3625799a474 | 187 | cnt%=26; |
kurobikari | 0:f3625799a474 | 188 | } |
kurobikari | 0:f3625799a474 | 189 | |
kurobikari | 0:f3625799a474 | 190 | /*************MDのステータスを決める君***********/ |
kurobikari | 0:f3625799a474 | 191 | void Md_Status(void) |
kurobikari | 0:f3625799a474 | 192 | { |
kurobikari | 0:f3625799a474 | 193 | /* PWMが0でないまたはSMBでないとき */ |
kurobikari | 0:f3625799a474 | 194 | if(mdc[0].pwm!=0||mdc[0].mode) { |
kurobikari | 0:f3625799a474 | 195 | IN[3] = (MdData&(1<<5)) !=0 ? 1 : 0;//1-2 |
kurobikari | 0:f3625799a474 | 196 | IN[2] = (MdData&(1<<4)) !=0 ? 1 : 0;//1-1 |
kurobikari | 0:f3625799a474 | 197 | } else { |
kurobikari | 0:f3625799a474 | 198 | IN[3] = 0;//1-2 |
kurobikari | 0:f3625799a474 | 199 | IN[2] = 0;//1-1 |
kurobikari | 0:f3625799a474 | 200 | } |
kurobikari | 0:f3625799a474 | 201 | /* PWMが0でないまたはSMBでないとき */ |
kurobikari | 0:f3625799a474 | 202 | if(mdc[1].pwm!=0||mdc[1].mode) { |
kurobikari | 0:f3625799a474 | 203 | IN[1] = (MdData&(1<<2)) !=0 ? 1 : 0;//2-2 |
kurobikari | 0:f3625799a474 | 204 | IN[0] = (MdData&(1<<1)) !=0 ? 1 : 0;//2-1 |
kurobikari | 0:f3625799a474 | 205 | } else { |
kurobikari | 0:f3625799a474 | 206 | IN[1] = 0;//2-2 |
kurobikari | 0:f3625799a474 | 207 | IN[0] = 0;//2-1 |
kurobikari | 0:f3625799a474 | 208 | } |
kurobikari | 0:f3625799a474 | 209 | |
kurobikari | 0:f3625799a474 | 210 | Enable[0] = 1; |
kurobikari | 0:f3625799a474 | 211 | Enable[1] = 1; |
kurobikari | 0:f3625799a474 | 212 | } |
kurobikari | 0:f3625799a474 | 213 | |
kurobikari | 0:f3625799a474 | 214 | /*************メインマイコンにデータ送信用関数************/ |
kurobikari | 0:f3625799a474 | 215 | void sent(void) |
kurobikari | 0:f3625799a474 | 216 | { |
kurobikari | 0:f3625799a474 | 217 | txMsg.len=3; |
kurobikari | 0:f3625799a474 | 218 | txMsg.id = TX_ID; |
kurobikari | 0:f3625799a474 | 219 | static int cnt =0; |
kurobikari | 0:f3625799a474 | 220 | cnt++; |
kurobikari | 0:f3625799a474 | 221 | cnt%=1000; |
kurobikari | 0:f3625799a474 | 222 | //uint8_t Enc_Data[2]; |
kurobikari | 0:f3625799a474 | 223 | |
kurobikari | 0:f3625799a474 | 224 | for(int i=0; i<3; i++) |
kurobikari | 0:f3625799a474 | 225 | txMsg.data[i]=((data[0]<<12|data[1])>>i*8)&0xFF; |
kurobikari | 0:f3625799a474 | 226 | can.write(txMsg); |
kurobikari | 0:f3625799a474 | 227 | } |