first rough structure
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "main.h" 00003 #include <stdint.h> 00004 #include <math.h> 00005 00006 /* 00007 * IO pins declaration 00008 */ 00009 AnalogIn Trq_raw(PC_1); 00010 00011 /* 00012 * Mbed Objects declaration 00013 */ 00014 Serial pc(USBTX, USBRX, 115200); 00015 CAN can1(PB_8, PB_9, 1000000); //1Mbps, contain critical torque command message 00016 Ticker ticker1; //100Hz task 00017 CANMessage can_msg_Rx; 00018 CANMessage can_msg_Tx; 00019 00020 00021 //Timers 00022 void timer1_interrupt(void) 00023 { 00024 HSTick += 1; 00025 LSTick += 1; 00026 00027 if (HSTick > 9) // 100Hz 00028 { 00029 HST_EXFL = 1; 00030 HSTick = 0; 00031 } 00032 if (LSTick > 99) // 10Hz 00033 { 00034 LST_EXFL = 1; 00035 LSTick = 0; 00036 } 00037 } 00038 00039 int main() 00040 { 00041 // Initializing 00042 printf("Motor_upper_control starts up\n"); 00043 00044 //Init CAN network 00045 CAN_init(); // Note now in Gloable test mode only for testing 2019/11/17 00046 00047 //attach IRQs after all other things are ready 00048 ticker1.attach_us(&timer1_interrupt, 1000); //1 ms Systick 00049 while(1) { 00050 00051 if(HST_EXFL) { 00052 HST_EXFL = false; 00053 // pc.printf("High speed says hi! \n"); 00054 Tx_High_CAN1(); 00055 } 00056 if(LST_EXFL) { 00057 LST_EXFL = false; 00058 // pc.printf("Low speed says hi! \n"); 00059 printf("Torque read: %.1f ", float(Trq_raw)); 00060 Tx_Low_CAN1(); 00061 } 00062 } 00063 } 00064 00065 void CAN_init(void) 00066 { 00067 //Set CAN system 00068 SET_BIT(CAN1->MCR, CAN_MCR_ABOM); // Enable auto reboot after bus off 00069 // can1.filter(Trq_send_ID,0xFFFF,CANStandard,0); // ID filter listing mode 00070 // can1.filter(Id_cmd_ID,0xFFFF,CANStandard,1); 00071 // can1.filter(Iq_cmd_ID,0xFFFF,CANStandard,2); 00072 // can1.mode(CAN::GlobalTest); // Add only for testing 2019/11/13 00073 can1.attach(&Rx_CAN1, CAN::RxIrq); // CAN1 Recieve Irq 00074 } 00075 00076 void Rx_CAN1(void) 00077 { 00078 // LED = 1; 00079 int16_t tmp; 00080 00081 if(can1.read(can_msg_Rx)) { 00082 // switch(can_msg_Rx.id) { //Filtered input message 00083 // Start of 100Hz msg 00084 // case Some_ID://1 00085 // //HSB from FL motor drive 00086 // Something1 = can_msg_Rx.data[6] & 0x03; //Get DSM_STAT 00087 // tmp = can_msg_Rx.data[5] << 8 | can_msg_Rx.data[4]; 00088 // Something2 = tmp*1.0f; 00089 // tmp = can_msg_Rx.data[3] << 8 | can_msg_Rx.data[2]; 00090 // Something3 = tmp * 0.01f; 00091 // tmp = can_msg_Rx.data[1] << 8 | can_msg_Rx.data[0]; 00092 // Something4 = tmp * 0.01f; 00093 // break; 00094 // } 00095 } 00096 // LED = 0; 00097 } 00098 00099 void Tx_High_CAN1(void) // 100 Hz 00100 { 00101 int16_t tmp; 00102 tmp = (int16_t) (Id_cmd * 100.0f); 00103 temp_msg[0] = tmp; 00104 temp_msg[1] = tmp >> 8U; 00105 temp_msg[2] = 0U; 00106 tmp = (int16_t) (Iq_cmd * 100.0f); 00107 temp_msg[3] = tmp; 00108 temp_msg[4] = tmp >> 8U; 00109 temp_msg[5] = 0U; 00110 temp_msg[6] = 0U; 00111 temp_msg[7] = 0U; 00112 can_msg_Tx = CANMessage(Trq_send_ID,temp_msg,8,CANData,CANStandard); 00113 CANpendTX(); 00114 can1.write(can_msg_Tx); 00115 } 00116 00117 void Tx_Low_CAN1(void) // 10 Hz 00118 { 00119 int16_t tmp; 00120 tmp = (int16_t) (Trq_send * 100.0f); 00121 temp_msg[0] = tmp; 00122 temp_msg[1] = tmp >> 8U; 00123 temp_msg[2] = 0U; 00124 temp_msg[3] = 0U; 00125 temp_msg[4] = 0U; 00126 temp_msg[5] = 0U; 00127 temp_msg[6] = 0U; 00128 temp_msg[7] = 0U; 00129 can_msg_Tx = CANMessage(Trq_send_ID,temp_msg,8,CANData,CANStandard); 00130 CANpendTX(); 00131 can1.write(can_msg_Tx); 00132 } 00133 void CANpendTX(void) 00134 { 00135 //Pend till TX box has empty slot, timeout will generate error 00136 uint32_t timeout = 0; 00137 while(!(CAN1->TSR & CAN_TSR_TME_Msk)) { 00138 //Wait till non empty 00139 timeout += 1; 00140 if(timeout > 10000) { 00141 // Put some timeout error handler 00142 break; 00143 } 00144 } 00145 }
Generated on Tue Jan 10 2023 18:00:09 by
1.7.2