Aditya Mehrotra / Mbed 2 deprecated dyno_current_sense_f44

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "math_ops.h"
00003 
00004 #define MIN -400.0f
00005 #define MAX 400.0f
00006 
00007 /*
00008    This basic example just shows how to read the ADC internal channels raw values.
00009    Please look in the corresponding device reference manual for a complete
00010    description of how to make a temperature sensor, VBat or Vref measurement.
00011 */
00012 
00013 //setup SERIAL
00014 Serial       pc(PA_2, PA_3);
00015 
00016 //setup scaling
00017 float TORQUE_SCALING = 0.0550; //V/A
00018 
00019 //setup analog inputs
00020 AnalogIn hv_current(PC_0);
00021 
00022 //setup CAN
00023 CAN          can(PA_11, PA_12, 1000000);  // CAN Rx pin name, CAN Tx pin name
00024 CANMessage   tauMsg;
00025 int                     ledState;
00026 Ticker                  sendCAN;
00027 int                     counter = 0;
00028 volatile bool           msgAvailable = false;
00029 Ticker loop;
00030 
00031 void pack_cmd(CANMessage * msg, float f1){
00032      /// convert floats to unsigned ints ///
00033      uint16_t f1_int = float_to_uint(f1, MIN, MAX, 16);            
00034      //uint16_t f2_int = float_to_uint(f2, MIN, MAX, 16);
00035      /// pack ints into the can buffer ///
00036      msg->data[0] = f1_int>>8;                                       
00037      msg->data[1] = f1_int&0xFF;
00038      /*
00039      msg->data[2] = f2_int>>8;
00040      msg->data[3] = f2_int&0xFF;
00041      msg->data[4] = 0x00;
00042      msg->data[5] = 0x00;
00043      msg->data[6] = 0x00;
00044      msg->data[7] = 0x00;
00045      */
00046      }
00047 
00048 int main()
00049 {
00050     wait(1);
00051     pc.baud(115200);
00052     wait(.01);
00053     pc.printf("\n\r\n\r Biomimetics Torque Sense Board\n\r\n\r");
00054     wait(.01);
00055     
00056     tauMsg.len = 2;                        //transmit 4 bytes
00057     tauMsg.id = 0x2;                       //higher priority                
00058     
00059     while(1){
00060         //wait(1); //GET RID OF THIS WAIT LATER
00061         float hv_cur = (hv_current.read()*SYSTEM_VOLTAGE-VCC/2.0) / HV_CURRENT_SCALING;                                     //PUBLISH
00062         float lv_cur = (lv_current.read()*SYSTEM_VOLTAGE-VCC/2.0) / LV_CURRENT_SCALING;                                     //PUBLISH
00063         float vol = (voltage_sense.read()*SYSTEM_VOLTAGE)*VOL_SENSE_SCALING; //INACCURATE ON THE HARDWARE SIDE!!!           //PUBLISH
00064         
00065         float V_OUT_EXT = (ext_current_A.read()*SYSTEM_VOLTAGE);
00066         float V_REF_EXT = (ext_current_B.read()*SYSTEM_VOLTAGE);
00067         
00068         float ext_cur = (V_OUT_EXT*SYSTEM_VOLTAGE-V_REF_EXT*SYSTEM_VOLTAGE) / EXT_CURRENT_SCALING;                            //PUBLISH
00069         
00070         //now we have to publish the four floats to the CAN BUS
00071         pack_cmd(&tauMsg, hv_cur);
00072         can.write(tauMsg);
00073         wait(.00002);
00074     }
00075 }