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:
- 16:e423f891cfbc
- Parent:
- 15:8adff67fe707
- Child:
- 17:75815e312312
--- a/main.cpp Wed Jun 14 01:23:53 2017 +0000 +++ b/main.cpp Thu Jun 15 04:23:01 2017 +0000 @@ -122,7 +122,7 @@ { count4 =__HAL_TIM_GET_COUNTER(&timer2); if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2)) { - if (count2 == 0 && adjustOffset == false) { //catch first index pulse + if (count2 == 0 && adjustOffset == false) { //catch first index pulse count3 = count4; adjustOffset = true; } @@ -167,7 +167,7 @@ //wait_ms(10); // Avoiding wait(), since it uses interrupts and timing here // is not critical. This takes 10 ms, if not interrupted - for (uint32_t j=0; j<199950; j++){} + for (uint32_t j=0; j<199950; j++) {} pos2=__HAL_TIM_GET_COUNTER(&timer2); sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2); @@ -177,7 +177,7 @@ //wait_ms(10); // Avoiding wait(), since it uses interrupts and timing here // is not critical. This takes 10 ms, if not interrupted - for (uint32_t j=0; j<199950; j++){} + for (uint32_t j=0; j<199950; j++) {} i++; pos2=__HAL_TIM_GET_COUNTER(&timer2); sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2); @@ -190,16 +190,16 @@ if (sens1 == sens2) { pos = pos2 - pos1; } else { - printf("E:Speed computation error, change of direction between readings!\r\n"); + printf("E:Speed computation error, change of direction between readings!\n"); } //For debugging - //printf("%lu microseconds, %ld steps: start:%lu stop%lu\r\n", deltaT, pos, pos1, pos2); + //printf("%lu microseconds, %ld steps: start:%lu stop%lu\n", deltaT, pos, pos1, pos2); if (deltaT > 0) { //speed computation in rot/s speed = ((float) pos)*125.f/((float) deltaT); // (pulses/us)*1000000/8000 -> rot/s } else { - printf("E:Error, time interval not greater than zero, speed not calculated!\r\n"); + printf("E:Error, time interval not greater than zero, speed not calculated!\n"); } return speed; } @@ -208,28 +208,29 @@ //the serial messages and invoques the corresponding commands void readData(void) { - char message[50]; + + char message[20]; if(raspi.readable()) { // Signalling the beginning of serial read led1 = 1; int p1 = 0; - + raspi.scanf("%s", message); //Message received printed for debugging - //printf("M:%d %s\r\n", strlen(message), message); - + //printf("M:%d %s\n", strlen(message), message); + if (strcmp(message, "adcOn") == 0) { adc_en = true; - printf("M:ADC true\r\n"); + printf("M:ADC true\n"); } else if (strcmp(message, "adcOff") == 0) { adc_en = false; - printf("M:ADC false\r\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("M:Value to write to DAC: %f\r\n", dac_val*3.3f); + printf("M: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; @@ -238,39 +239,39 @@ count3 = 0; count4 = 0; adjustOffset = false; - printf("M:Encoder counters reset!\r\n"); + printf("M:Encoder counters reset!\n"); } else if (strcmp(message, "powerOn") == 0) { //command to power on the DCPS relay1.write(1); - printf("M:DCPS on\r\n"); + printf("M:DCPS on\n"); } else if (strcmp(message, "powerOff") == 0) { //command to power off the DCPS relay1.write(0); - printf("M:DCPS off\r\n"); + printf("M:DCPS off\n"); } else if (strcmp(message, "posOn") == 0) { //command to enable the encoder position notification pos_en = true; - printf("M:Position notification enabled\r\n"); + printf("M:Position notification enabled\n"); } else if (strcmp(message, "posOff") == 0) { //command to disable the encoder position notification pos_en = false; - printf("M:Position notification disabled\r\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("M:Index related position notification enabled\r\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("M:Index related position notification disabled\r\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("M:Speed enabled\r\n"); + printf("M:Speed enabled\n"); } else if (strcmp(message, "speedOff") == 0) { //command to disable speed computation and notification speed_en = false; - printf("M:Speed disabled\r\n"); + printf("M:Speed disabled\n"); } } @@ -280,10 +281,10 @@ 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(); @@ -299,7 +300,8 @@ int main() { //Power onn the DCPS - relay1.write(0); + relay1.write(1); + dac1.write(0.32); //counting on both A&B inputs (A at PA0, B at PA1), 4 ticks per cycle, //full 32-bit count @@ -317,11 +319,12 @@ raspi.baud(115200); //Attach functin to call for serial interrupt event + wait(1); raspi.attach(&readData); //Message to mark the initialisation of the program - printf("M:\n\rSTM HAL encoder with ADC and DAC\n\r"); - printf("M:Running at %u MHz\r\n\n", HAL_RCC_GetSysClockFreq()/1000000); + printf("M:STM HAL encoder with ADC and DAC\n"); + printf("M:Running at %u MHz\n", HAL_RCC_GetSysClockFreq()/1000000); //The main loop while(1) { @@ -330,40 +333,46 @@ int8_t dir1; //Prints the timestamp in miliseconds - printf("T:%u\r\n", us_ticker_read()/1000); + printf("T:%u", us_ticker_read()/1000); if (pos_en) { //It gets the position and the direction of the encoder count1=__HAL_TIM_GET_COUNTER(&timer2); dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2); - printf("P:%ld S:%s C:%d\r\n", count1, dir1==0 ? "+":"-", count2); + printf("P:%ldD:%sC:%d", count1, dir1==0 ? "+":"-", count2); if (posIndex_en) { // Does not work if speed computation is enabled and uses wait(), - // because speed computation may take too long and wait() + // because speed computation may take too long and wait() // disables other interrupts, therefore the index pulse - // interrupt as well. + // interrupt as well. if (count2 > 1) { - printf("PI:%ld S:%s C:%d\r\n", (count1-count4) + (count2-1)*8000 + count3, dir1==0 ? "+":"-", count2); + printf("Pi:%ld", (count1-count4) + (count2-1)*8000 + count3); } else if (count2 < -1) { - printf("PI:%ld S:%s C:%d\r\n", (count1-count4) + (count2+1)*8000 + count3, dir1==0 ? "+":"-", count2); + printf("Pi:%ld", (count1-count4) + (count2+1)*8000 + count3); } else { - printf("PI:%ld S:%s C:%d\r\n", (count1-count4) + count3, dir1==0 ? "+":"-", count2); + printf("Pi:%ld", (count1-count4) + count3); } } } if (speed_en) { //Print speed - printf("S:%f\r\n", speedRead()); + printf("S:%.3f%", speedRead()); // in rot/s } if (adc_en) { - printf("Phase= %3.3f%V\r\n", (3.3f*ADC_read(adc1)+0.022f)*125.62f); - printf("Voltage DCPS: %3.3f%V\r\n", (3.3f*ADC_read(adc2)+0.062f)/0.207f); - printf("Average Current: %3.3f%mA\r\n", 3.3f*ADC_read(adc3)+0.022f); + //Print phase shift in Degrees, DCPS output voltage in Volts + // and current intensity in Ampers + printf("F:%3.3fU:%3.3fI:%3.3f", (3.3f*ADC_read(adc1)+0.022f)*125.62f, + (3.3f*ADC_read(adc2)+0.062f)/0.207f, 3.3f*ADC_read(adc3)+0.022f); + //Print DCPS output voltage in Volts + //printf("U:%3.3f%", (3.3f*ADC_read(adc2)+0.062f)/0.207f); + //Print current intensity in Ampers + //printf("I:%3.3f%\n", 3.3f*ADC_read(adc3)+0.022f); } + printf("\n"); - wait(0.09); + wait(0.08); } } \ No newline at end of file