A program to monitor some parameters for a motor
Dependencies: mbed-dev BufferSerial
Thanks to David Lowe for https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/ which I adapted for the use of TIM2 32bit timer as an encoder reader on the Nucleo L432KC board.
Diff: main.cpp
- Revision:
- 18:1ef20c13693c
- Parent:
- 17:75815e312312
- Child:
- 19:51075c725004
diff -r 75815e312312 -r 1ef20c13693c main.cpp --- a/main.cpp Thu Jun 15 17:20:59 2017 +0000 +++ b/main.cpp Thu Jun 15 18:16:15 2017 +0000 @@ -49,7 +49,6 @@ //Timer to be used for the speed computation function Timer timer1; -Timer timer3; //The input for the encoder's index channel InterruptIn event(PA_8); @@ -84,11 +83,11 @@ //Counter for the absolute position from start int32_t count1 = 0; //Counter for the index passes -int32_t count2 = 0; +volatile int32_t count2 = 0; //Records the wheel offset position to the index -int32_t count3 = 0; +volatile int32_t count3 = 0; //Counter recording absolute position at last index pulse -int32_t count4 = 0; +volatile int32_t count4 = 0; //Used to filter the first index pulse, so that the offset position //to the index may be recorded volatile bool adjustOffset = false; @@ -191,7 +190,7 @@ if (sens1 == sens2) { pos = pos2 - pos1; } else { - printf("\nE:Speed computation error, change of direction between readings!\n"); + printf("E:Speed computation error, change of direction between readings!\n"); } //For debugging @@ -200,7 +199,7 @@ //speed computation in rot/s speed = ((float) pos)*125.f/((float) deltaT); // (pulses/us)*1000000/8000 -> rot/s } else { - printf("\nE:Error, time interval not greater than zero, speed not calculated!\n"); + printf("E:Error, time interval not greater than zero, speed not calculated!\n"); } return speed; } @@ -228,16 +227,15 @@ int p1 = 0; if (strcmp(message, "adcOn") == 0) { adc_en = true; - printf("\nM:ADC true\n"); + printf("M:ADC true\n"); } else if (strcmp(message, "adcOff") == 0) { adc_en = false; - printf("\nM:ADC false\n"); + printf("M:ADC false\n"); } else if (p1=strstr(message, "dac=") != NULL) { //Writing the dac1 value read from serial //The DCPS has 1V offset, so we have to remove it dac_val = (atof(message+p1+3)-1.0f)/15.51; dac1.write(dac_val); - printf("\nM:Value to write to DAC: %f\n", dac_val*3.3f); } else if (strcmp(message, "reset") == 0) { //encoder related counters reset command TIM2->CNT = 0x0000; @@ -246,11 +244,11 @@ count3 = 0; count4 = 0; adjustOffset = false; - printf("\nM:Encoder counters reset!\n"); + printf("M:Encoder counters reset!\n"); } else if (strcmp(message, "powerOn") == 0) { //command to power on the DCPS relay1.write(1); - printf("\nM:DCPS on\n"); + printf("M:DCPS on\n"); } else if (strcmp(message, "powerOff") == 0) { //command to power off the DCPS relay1.write(0); @@ -258,27 +256,27 @@ } else if (strcmp(message, "posOn") == 0) { //command to enable the encoder position notification pos_en = true; - printf("\nM:Position notification enabled\n"); + printf("M:Position notification enabled\n"); } else if (strcmp(message, "posOff") == 0) { //command to disable the encoder position notification pos_en = false; - printf("\nM:Position notification disabled\n"); + printf("M:Position notification disabled\n"); } else if (strcmp(message, "posIndexOn") == 0) { //command to enable the index related encoder position notification posIndex_en = true; - printf("\nM:Index related position notification enabled\n"); + printf("M:Index related position notification enabled\n"); } else if (strcmp(message, "posIndexOff") == 0) { //command to disable the index related encoder position notification posIndex_en = false; - printf("\nM:Index related position notification disabled\n"); + printf("M:Index related position notification disabled\n"); } else if (strcmp(message, "speedOn") == 0) { //command to enable speed computation and notification speed_en = true; - printf("\nM:Speed enabled\n"); + printf("M:Speed enabled\n"); } else if (strcmp(message, "speedOff") == 0) { //command to disable speed computation and notification speed_en = false; - printf("\nM:Speed disabled\n"); + printf("M:Speed disabled\n"); } } @@ -286,16 +284,15 @@ led1 = 0; } +//Function to compute the average of ADC readings float ADC_read(AnalogIn adc) { - float Voltage; float Voltage_total = 0.0; // Voltage is summed then averaged for (int i=0; i<100; i++) { // do 100 readings Voltage = adc.read(); - Voltage_total = Voltage_total+ Voltage; //wait_us(10); } @@ -306,8 +303,9 @@ //The main function int main() { - //Power onn the DCPS + //Power on the DCPS relay1.write(1); + //Set the DCPS output to (0.32x3.3)x4.7+1 = ~6V dac1.write(0.32); //counting on both A&B inputs (A at PA0, B at PA1), 4 ticks per cycle, @@ -326,6 +324,9 @@ raspi.baud(115200); //Attach functin to call for serial interrupt event + //The wait() function ensures that not false serial RX + //interrupt is triggered at power up, because of initial + //serial port install by the Raspi OS wait(1); raspi.attach(&readData, Serial::RxIrq);