Proto Drive / Mbed 2 deprecated Protodrive

Dependencies:   RPCInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers measurement.h Source File

measurement.h

00001 /***********************************************************/
00002 /*Constants                                                */
00003 /***********************************************************/
00004 
00005 #define ISENSOR_SLOPE 23.9 //slope of the current sensor in [A/V]
00006 #define Y_INTERCEPT -60.1563 //sensor voltage at 0 current
00007 #define NUM_HALF_CYCLES 4 //set the number of ticks over which to measure speed
00008 #define WAIT_BEFORE_SPEEDSTOP 7.5 //amount of time to wait before declaring that the motor is not moving. 7.5s corresponds to a speed of 1 rev/min
00009 #define R1 200000//voltage measurement resistor connected to device under measurement [Ohm]
00010 #define R2 100000//voltage measurement resistor connected to ground [Ohm]
00011 
00012 /***********************************************************/
00013 /*Variables                                                */
00014 /***********************************************************/
00015 
00016 float end, begin, current_read;
00017 int current_state;
00018 int count = 0;
00019 
00020 Timer speed_timer;
00021 
00022 /***********************************************************/
00023 /*Pin setup                                                */
00024 /***********************************************************/
00025 //Encoder
00026 DigitalIn speed_yellow(p7); //yellow wire from sinusoidal encoder
00027 DigitalIn speed_green(p8); //green wire from sinusoidal encoder
00028 
00029 /***********************************************************/
00030 /*Subroutines                                              */
00031 /***********************************************************/
00032 
00033 //get the voltage for one of the energy storage devices. Takes pin as a parameter
00034 float get_voltage (AnalogIn& pin) {
00035     float voltage;
00036     voltage = pin.read()*3.3*(R1+R2)/R2; //scaling to account for voltage divider
00037     return voltage;
00038 }
00039 
00040 //returns current in amps
00041 #ifdef CURRENT_SENSOR_ON
00042 void get_current() {
00043     current_read = current_sense.read(); //read raw AnalogIn value of current
00044     current = ISENSOR_SLOPE*current_read*3.3 + Y_INTERCEPT; //scaling to get current in A
00045 }
00046 #endif
00047 
00048 //returns speed in rad/sec
00049 void get_speed() {
00050     current_state = speed_yellow; //get the current state of speed_yellow pin (0 or 1)
00051     speed_timer.start();
00052     while (speed_yellow == current_state && speed_timer <=WAIT_BEFORE_SPEEDSTOP) {} //wait for value of the speed_yellow to change, indicating the beginning of a new cycle
00053     if (speed_timer < WAIT_BEFORE_SPEEDSTOP) { //check that the timer is less than WAIT_BEFORE_SPEEDSTOP, to make sure it has not been running for too long. This will happen if speed = 0
00054         speed_timer.reset(); //reset the timer so that it starts timing from the beginning of the new cycle
00055         begin = speed_timer.read();
00056         for (int i = 1; i <= NUM_HALF_CYCLES; i++) { //loop to allow timing over a set number of encoder cycles
00057             current_state = speed_yellow;
00058             while (speed_yellow == current_state && speed_timer <= WAIT_BEFORE_SPEEDSTOP) {}//wait for speed_yellow pin to change. If it does not change, the loop will exit when speed_timer = WAIT_BEFORE_SPEEDSTOP
00059         }
00060         if (speed_timer < WAIT_BEFORE_SPEEDSTOP) {
00061             end = speed_timer.read(); //time at the end of timing NUM_HALF_CYCLES cycles
00062             speed_timer.stop();
00063             speed =((3.14159265/8)*NUM_HALF_CYCLES)/(end-begin); //record speed in rad/sec
00064         } else {
00065             speed = 0; //speed = 0 if the timer has exceeded WAIT_BEFORE_SPEEDSTOP
00066         }
00067     } else {
00068         speed = 0;  //speed = 0 if the timer has exceeded WAIT_BEFORE_SPEEDSTOP
00069     }
00070 }