![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
power board firmware for dyno
main.cpp@3:609e4be772e0, 2021-09-06 (annotated)
- Committer:
- adimmit
- Date:
- Mon Sep 06 20:01:24 2021 +0000
- Revision:
- 3:609e4be772e0
- Parent:
- 2:5ce2ad0782de
fixed voltage sensor;
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 SYSTEM_VOLTAGE = 3.3; //volts |
adimmit | 0:f4f2a7b3364c | 20 | float VCC = 5.0; //volts |
adimmit | 0:f4f2a7b3364c | 21 | float EXT_CURRENT_SCALING = 0.008; //Sensitivity = 8mv/A external sensor |
adimmit | 0:f4f2a7b3364c | 22 | |
adimmit | 0:f4f2a7b3364c | 23 | //setup analog inputs |
adimmit | 0:f4f2a7b3364c | 24 | AnalogIn hv_current(PC_0); |
adimmit | 0:f4f2a7b3364c | 25 | AnalogIn lv_current(PC_1); |
adimmit | 0:f4f2a7b3364c | 26 | AnalogIn voltage_sense(PC_2); |
adimmit | 0:f4f2a7b3364c | 27 | AnalogIn ext_current_A(PC_3); //V_OUT (for now) |
adimmit | 0:f4f2a7b3364c | 28 | AnalogIn ext_current_B(PC_4); //V_REF (for now) |
adimmit | 0:f4f2a7b3364c | 29 | //setup CAN |
adimmit | 0:f4f2a7b3364c | 30 | CAN can(PA_11, PA_12, 1000000); // CAN Rx pin name, CAN Tx pin name |
adimmit | 0:f4f2a7b3364c | 31 | CANMessage currMsg, statMsg; |
adimmit | 0:f4f2a7b3364c | 32 | int ledState; |
adimmit | 0:f4f2a7b3364c | 33 | Ticker sendCAN; |
adimmit | 0:f4f2a7b3364c | 34 | int counter = 0; |
adimmit | 0:f4f2a7b3364c | 35 | volatile bool msgAvailable = false; |
adimmit | 0:f4f2a7b3364c | 36 | Ticker loop; |
adimmit | 0:f4f2a7b3364c | 37 | |
adimmit | 0:f4f2a7b3364c | 38 | void pack_cmd(CANMessage * msg, float f1, float f2){ |
adimmit | 0:f4f2a7b3364c | 39 | /// convert floats to unsigned ints /// |
adimmit | 0:f4f2a7b3364c | 40 | uint16_t f1_int = float_to_uint(f1, MIN, MAX, 16); |
adimmit | 0:f4f2a7b3364c | 41 | uint16_t f2_int = float_to_uint(f2, MIN, MAX, 16); |
adimmit | 0:f4f2a7b3364c | 42 | /// pack ints into the can buffer /// |
adimmit | 0:f4f2a7b3364c | 43 | msg->data[0] = f1_int>>8; |
adimmit | 0:f4f2a7b3364c | 44 | msg->data[1] = f1_int&0xFF; |
adimmit | 0:f4f2a7b3364c | 45 | msg->data[2] = f2_int>>8; |
adimmit | 0:f4f2a7b3364c | 46 | msg->data[3] = f2_int&0xFF; |
adimmit | 0:f4f2a7b3364c | 47 | } |
adimmit | 0:f4f2a7b3364c | 48 | |
adimmit | 0:f4f2a7b3364c | 49 | int main() |
adimmit | 0:f4f2a7b3364c | 50 | { |
adimmit | 0:f4f2a7b3364c | 51 | wait(1); |
adimmit | 0:f4f2a7b3364c | 52 | pc.baud(115200); |
adimmit | 0:f4f2a7b3364c | 53 | wait(.01); |
adimmit | 0:f4f2a7b3364c | 54 | pc.printf("\n\r\n\r Biomimetics Power Board\n\r\n\r"); |
adimmit | 0:f4f2a7b3364c | 55 | wait(.01); |
adimmit | 0:f4f2a7b3364c | 56 | |
adimmit | 0:f4f2a7b3364c | 57 | currMsg.len = 4; //transmit 4 bytes |
adimmit | 0:f4f2a7b3364c | 58 | statMsg.len = 4; //transmit 4 bytes |
adimmit | 0:f4f2a7b3364c | 59 | |
adimmit | 0:f4f2a7b3364c | 60 | currMsg.id = 0x1; //higher priority |
adimmit | 2:5ce2ad0782de | 61 | statMsg.id = 0x2; //lower priority |
adimmit | 0:f4f2a7b3364c | 62 | |
adimmit | 0:f4f2a7b3364c | 63 | while(1){ |
adimmit | 2:5ce2ad0782de | 64 | wait(0.0003); //MAKE SURE IT's 1-3k rate, higher we will fill the CAN buffer |
adimmit | 2:5ce2ad0782de | 65 | float hv_cur = float((hv_current.read()*SYSTEM_VOLTAGE-VCC/2.0) / HV_CURRENT_SCALING); //PUBLISH |
adimmit | 2:5ce2ad0782de | 66 | float lv_cur = float((lv_current.read()*SYSTEM_VOLTAGE-VCC/2.0) / LV_CURRENT_SCALING); //PUBLISH |
adimmit | 3:609e4be772e0 | 67 | float vol = (voltage_sense.read()*SYSTEM_VOLTAGE); //*67.232-96.149; //PUBLISH |
adimmit | 0:f4f2a7b3364c | 68 | |
adimmit | 0:f4f2a7b3364c | 69 | float V_OUT_EXT = (ext_current_A.read()*SYSTEM_VOLTAGE); |
adimmit | 0:f4f2a7b3364c | 70 | float V_REF_EXT = (ext_current_B.read()*SYSTEM_VOLTAGE); |
adimmit | 0:f4f2a7b3364c | 71 | |
adimmit | 2:5ce2ad0782de | 72 | float ext_cur = (V_OUT_EXT-V_REF_EXT) / EXT_CURRENT_SCALING; //PUBLISH |
adimmit | 0:f4f2a7b3364c | 73 | |
adimmit | 3:609e4be772e0 | 74 | //pc.printf("Volts: %f\n", vol); |
adimmit | 3:609e4be772e0 | 75 | |
adimmit | 0:f4f2a7b3364c | 76 | //now we have to publish the four floats to the CAN BUS |
adimmit | 0:f4f2a7b3364c | 77 | pack_cmd(&currMsg, hv_cur, ext_cur); |
adimmit | 0:f4f2a7b3364c | 78 | pack_cmd(&statMsg, vol, lv_cur); |
adimmit | 0:f4f2a7b3364c | 79 | can.write(currMsg); |
adimmit | 0:f4f2a7b3364c | 80 | wait(.00002); |
adimmit | 0:f4f2a7b3364c | 81 | can.write(statMsg); |
adimmit | 0:f4f2a7b3364c | 82 | } |
adimmit | 0:f4f2a7b3364c | 83 | } |