![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
torque sensor thing
main.cpp@1:6b03278584b2, 2021-06-21 (annotated)
- Committer:
- adimmit
- Date:
- Mon Jun 21 19:56:09 2021 +0000
- Revision:
- 1:6b03278584b2
- Parent:
- 0:f4f2a7b3364c
- Child:
- 2:ab65342daec8
torque sense starting;
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 | 1:6b03278584b2 | 17 | float TORQUE_SCALING = 0.0550; //V/A |
adimmit | 0:f4f2a7b3364c | 18 | |
adimmit | 0:f4f2a7b3364c | 19 | //setup analog inputs |
adimmit | 0:f4f2a7b3364c | 20 | AnalogIn hv_current(PC_0); |
adimmit | 1:6b03278584b2 | 21 | |
adimmit | 0:f4f2a7b3364c | 22 | //setup CAN |
adimmit | 0:f4f2a7b3364c | 23 | CAN can(PA_11, PA_12, 1000000); // CAN Rx pin name, CAN Tx pin name |
adimmit | 1:6b03278584b2 | 24 | CANMessage tauMsg; |
adimmit | 0:f4f2a7b3364c | 25 | int ledState; |
adimmit | 0:f4f2a7b3364c | 26 | Ticker sendCAN; |
adimmit | 0:f4f2a7b3364c | 27 | int counter = 0; |
adimmit | 0:f4f2a7b3364c | 28 | volatile bool msgAvailable = false; |
adimmit | 0:f4f2a7b3364c | 29 | Ticker loop; |
adimmit | 0:f4f2a7b3364c | 30 | |
adimmit | 1:6b03278584b2 | 31 | void pack_cmd(CANMessage * msg, float f1){ |
adimmit | 0:f4f2a7b3364c | 32 | /// convert floats to unsigned ints /// |
adimmit | 0:f4f2a7b3364c | 33 | uint16_t f1_int = float_to_uint(f1, MIN, MAX, 16); |
adimmit | 1:6b03278584b2 | 34 | //uint16_t f2_int = float_to_uint(f2, MIN, MAX, 16); |
adimmit | 0:f4f2a7b3364c | 35 | /// pack ints into the can buffer /// |
adimmit | 0:f4f2a7b3364c | 36 | msg->data[0] = f1_int>>8; |
adimmit | 0:f4f2a7b3364c | 37 | msg->data[1] = f1_int&0xFF; |
adimmit | 1:6b03278584b2 | 38 | /* |
adimmit | 0:f4f2a7b3364c | 39 | msg->data[2] = f2_int>>8; |
adimmit | 0:f4f2a7b3364c | 40 | msg->data[3] = f2_int&0xFF; |
adimmit | 0:f4f2a7b3364c | 41 | msg->data[4] = 0x00; |
adimmit | 0:f4f2a7b3364c | 42 | msg->data[5] = 0x00; |
adimmit | 0:f4f2a7b3364c | 43 | msg->data[6] = 0x00; |
adimmit | 0:f4f2a7b3364c | 44 | msg->data[7] = 0x00; |
adimmit | 0:f4f2a7b3364c | 45 | */ |
adimmit | 0:f4f2a7b3364c | 46 | } |
adimmit | 0:f4f2a7b3364c | 47 | |
adimmit | 0:f4f2a7b3364c | 48 | int main() |
adimmit | 0:f4f2a7b3364c | 49 | { |
adimmit | 0:f4f2a7b3364c | 50 | wait(1); |
adimmit | 0:f4f2a7b3364c | 51 | pc.baud(115200); |
adimmit | 0:f4f2a7b3364c | 52 | wait(.01); |
adimmit | 1:6b03278584b2 | 53 | pc.printf("\n\r\n\r Biomimetics Torque Sense Board\n\r\n\r"); |
adimmit | 0:f4f2a7b3364c | 54 | wait(.01); |
adimmit | 0:f4f2a7b3364c | 55 | |
adimmit | 1:6b03278584b2 | 56 | tauMsg.len = 2; //transmit 4 bytes |
adimmit | 1:6b03278584b2 | 57 | tauMsg.id = 0x2; //higher priority |
adimmit | 0:f4f2a7b3364c | 58 | |
adimmit | 0:f4f2a7b3364c | 59 | while(1){ |
adimmit | 0:f4f2a7b3364c | 60 | //wait(1); //GET RID OF THIS WAIT LATER |
adimmit | 0:f4f2a7b3364c | 61 | float hv_cur = (hv_current.read()*SYSTEM_VOLTAGE-VCC/2.0) / HV_CURRENT_SCALING; //PUBLISH |
adimmit | 0:f4f2a7b3364c | 62 | float lv_cur = (lv_current.read()*SYSTEM_VOLTAGE-VCC/2.0) / LV_CURRENT_SCALING; //PUBLISH |
adimmit | 0:f4f2a7b3364c | 63 | float vol = (voltage_sense.read()*SYSTEM_VOLTAGE)*VOL_SENSE_SCALING; //INACCURATE ON THE HARDWARE SIDE!!! //PUBLISH |
adimmit | 0:f4f2a7b3364c | 64 | |
adimmit | 0:f4f2a7b3364c | 65 | float V_OUT_EXT = (ext_current_A.read()*SYSTEM_VOLTAGE); |
adimmit | 0:f4f2a7b3364c | 66 | float V_REF_EXT = (ext_current_B.read()*SYSTEM_VOLTAGE); |
adimmit | 0:f4f2a7b3364c | 67 | |
adimmit | 0:f4f2a7b3364c | 68 | float ext_cur = (V_OUT_EXT*SYSTEM_VOLTAGE-V_REF_EXT*SYSTEM_VOLTAGE) / EXT_CURRENT_SCALING; //PUBLISH |
adimmit | 0:f4f2a7b3364c | 69 | |
adimmit | 0:f4f2a7b3364c | 70 | //now we have to publish the four floats to the CAN BUS |
adimmit | 1:6b03278584b2 | 71 | pack_cmd(&tauMsg, hv_cur); |
adimmit | 1:6b03278584b2 | 72 | can.write(tauMsg); |
adimmit | 0:f4f2a7b3364c | 73 | wait(.00002); |
adimmit | 0:f4f2a7b3364c | 74 | } |
adimmit | 0:f4f2a7b3364c | 75 | } |