![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
torque sensor thing
main.cpp@0:f4f2a7b3364c, 2021-06-21 (annotated)
- Committer:
- adimmit
- Date:
- Mon Jun 21 19:50:05 2021 +0000
- Revision:
- 0:f4f2a7b3364c
- Child:
- 1:6b03278584b2
mostly done - just need to check values
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adimmit | 0:f4f2a7b3364c | 1 | #include "mbed.h" |
adimmit | 0:f4f2a7b3364c | 2 | #include "math_ops.h" |
adimmit | 0:f4f2a7b3364c | 3 | |
adimmit | 0:f4f2a7b3364c | 4 | #define MIN -400.0f |
adimmit | 0:f4f2a7b3364c | 5 | #define MAX 400.0f |
adimmit | 0:f4f2a7b3364c | 6 | |
adimmit | 0:f4f2a7b3364c | 7 | /* |
adimmit | 0:f4f2a7b3364c | 8 | This basic example just shows how to read the ADC internal channels raw values. |
adimmit | 0:f4f2a7b3364c | 9 | Please look in the corresponding device reference manual for a complete |
adimmit | 0:f4f2a7b3364c | 10 | description of how to make a temperature sensor, VBat or Vref measurement. |
adimmit | 0:f4f2a7b3364c | 11 | */ |
adimmit | 0:f4f2a7b3364c | 12 | |
adimmit | 0:f4f2a7b3364c | 13 | //setup SERIAL |
adimmit | 0:f4f2a7b3364c | 14 | Serial pc(PA_2, PA_3); |
adimmit | 0:f4f2a7b3364c | 15 | |
adimmit | 0:f4f2a7b3364c | 16 | //setup scaling |
adimmit | 0:f4f2a7b3364c | 17 | float HV_CURRENT_SCALING = 0.0400; //V/A |
adimmit | 0:f4f2a7b3364c | 18 | float LV_CURRENT_SCALING = 0.0550; //V/A |
adimmit | 0:f4f2a7b3364c | 19 | float VOL_SENSE_SCALING = (1000000.0+30000.0)/30000.0; //V per Volt |
adimmit | 0:f4f2a7b3364c | 20 | float SYSTEM_VOLTAGE = 3.3; //volts |
adimmit | 0:f4f2a7b3364c | 21 | float VCC = 5.0; //volts |
adimmit | 0:f4f2a7b3364c | 22 | float EXT_CURRENT_SCALING = 0.008; //Sensitivity = 8mv/A external sensor |
adimmit | 0:f4f2a7b3364c | 23 | |
adimmit | 0:f4f2a7b3364c | 24 | //setup analog inputs |
adimmit | 0:f4f2a7b3364c | 25 | AnalogIn hv_current(PC_0); |
adimmit | 0:f4f2a7b3364c | 26 | AnalogIn lv_current(PC_1); |
adimmit | 0:f4f2a7b3364c | 27 | AnalogIn voltage_sense(PC_2); |
adimmit | 0:f4f2a7b3364c | 28 | AnalogIn ext_current_A(PC_3); //V_OUT (for now) |
adimmit | 0:f4f2a7b3364c | 29 | AnalogIn ext_current_B(PC_4); //V_REF (for now) |
adimmit | 0:f4f2a7b3364c | 30 | //setup CAN |
adimmit | 0:f4f2a7b3364c | 31 | CAN can(PA_11, PA_12, 1000000); // CAN Rx pin name, CAN Tx pin name |
adimmit | 0:f4f2a7b3364c | 32 | CANMessage currMsg, statMsg; |
adimmit | 0:f4f2a7b3364c | 33 | int ledState; |
adimmit | 0:f4f2a7b3364c | 34 | Ticker sendCAN; |
adimmit | 0:f4f2a7b3364c | 35 | int counter = 0; |
adimmit | 0:f4f2a7b3364c | 36 | volatile bool msgAvailable = false; |
adimmit | 0:f4f2a7b3364c | 37 | Ticker loop; |
adimmit | 0:f4f2a7b3364c | 38 | |
adimmit | 0:f4f2a7b3364c | 39 | void pack_cmd(CANMessage * msg, float f1, float f2){ |
adimmit | 0:f4f2a7b3364c | 40 | /// convert floats to unsigned ints /// |
adimmit | 0:f4f2a7b3364c | 41 | uint16_t f1_int = float_to_uint(f1, MIN, MAX, 16); |
adimmit | 0:f4f2a7b3364c | 42 | uint16_t f2_int = float_to_uint(f2, MIN, MAX, 16); |
adimmit | 0:f4f2a7b3364c | 43 | /// pack ints into the can buffer /// |
adimmit | 0:f4f2a7b3364c | 44 | msg->data[0] = f1_int>>8; |
adimmit | 0:f4f2a7b3364c | 45 | msg->data[1] = f1_int&0xFF; |
adimmit | 0:f4f2a7b3364c | 46 | msg->data[2] = f2_int>>8; |
adimmit | 0:f4f2a7b3364c | 47 | msg->data[3] = f2_int&0xFF; |
adimmit | 0:f4f2a7b3364c | 48 | /* |
adimmit | 0:f4f2a7b3364c | 49 | msg->data[4] = 0x00; |
adimmit | 0:f4f2a7b3364c | 50 | msg->data[5] = 0x00; |
adimmit | 0:f4f2a7b3364c | 51 | msg->data[6] = 0x00; |
adimmit | 0:f4f2a7b3364c | 52 | msg->data[7] = 0x00; |
adimmit | 0:f4f2a7b3364c | 53 | */ |
adimmit | 0:f4f2a7b3364c | 54 | } |
adimmit | 0:f4f2a7b3364c | 55 | |
adimmit | 0:f4f2a7b3364c | 56 | int main() |
adimmit | 0:f4f2a7b3364c | 57 | { |
adimmit | 0:f4f2a7b3364c | 58 | wait(1); |
adimmit | 0:f4f2a7b3364c | 59 | pc.baud(115200); |
adimmit | 0:f4f2a7b3364c | 60 | wait(.01); |
adimmit | 0:f4f2a7b3364c | 61 | pc.printf("\n\r\n\r Biomimetics Power Board\n\r\n\r"); |
adimmit | 0:f4f2a7b3364c | 62 | wait(.01); |
adimmit | 0:f4f2a7b3364c | 63 | |
adimmit | 0:f4f2a7b3364c | 64 | currMsg.len = 4; //transmit 4 bytes |
adimmit | 0:f4f2a7b3364c | 65 | statMsg.len = 4; //transmit 4 bytes |
adimmit | 0:f4f2a7b3364c | 66 | |
adimmit | 0:f4f2a7b3364c | 67 | currMsg.id = 0x1; //higher priority |
adimmit | 0:f4f2a7b3364c | 68 | statMsg.id = 0x2; //lower |
adimmit | 0:f4f2a7b3364c | 69 | |
adimmit | 0:f4f2a7b3364c | 70 | while(1){ |
adimmit | 0:f4f2a7b3364c | 71 | //wait(1); //GET RID OF THIS WAIT LATER |
adimmit | 0:f4f2a7b3364c | 72 | float hv_cur = (hv_current.read()*SYSTEM_VOLTAGE-VCC/2.0) / HV_CURRENT_SCALING; //PUBLISH |
adimmit | 0:f4f2a7b3364c | 73 | float lv_cur = (lv_current.read()*SYSTEM_VOLTAGE-VCC/2.0) / LV_CURRENT_SCALING; //PUBLISH |
adimmit | 0:f4f2a7b3364c | 74 | float vol = (voltage_sense.read()*SYSTEM_VOLTAGE)*VOL_SENSE_SCALING; //INACCURATE ON THE HARDWARE SIDE!!! //PUBLISH |
adimmit | 0:f4f2a7b3364c | 75 | |
adimmit | 0:f4f2a7b3364c | 76 | float V_OUT_EXT = (ext_current_A.read()*SYSTEM_VOLTAGE); |
adimmit | 0:f4f2a7b3364c | 77 | float V_REF_EXT = (ext_current_B.read()*SYSTEM_VOLTAGE); |
adimmit | 0:f4f2a7b3364c | 78 | |
adimmit | 0:f4f2a7b3364c | 79 | float ext_cur = (V_OUT_EXT*SYSTEM_VOLTAGE-V_REF_EXT*SYSTEM_VOLTAGE) / EXT_CURRENT_SCALING; //PUBLISH |
adimmit | 0:f4f2a7b3364c | 80 | |
adimmit | 0:f4f2a7b3364c | 81 | //now we have to publish the four floats to the CAN BUS |
adimmit | 0:f4f2a7b3364c | 82 | pack_cmd(&currMsg, hv_cur, ext_cur); |
adimmit | 0:f4f2a7b3364c | 83 | pack_cmd(&statMsg, vol, lv_cur); |
adimmit | 0:f4f2a7b3364c | 84 | can.write(currMsg); |
adimmit | 0:f4f2a7b3364c | 85 | wait(.00002); |
adimmit | 0:f4f2a7b3364c | 86 | can.write(statMsg); |
adimmit | 0:f4f2a7b3364c | 87 | } |
adimmit | 0:f4f2a7b3364c | 88 | } |