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.

Committer:
tonnyleonard
Date:
Tue May 30 22:35:08 2017 +0000
Revision:
10:8862c8779b71
Parent:
9:4d736d29ce19
Child:
11:7a9837acbea0
Add encoder speed algorithm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tonnyleonard 0:789510d98ade 1 /*
tonnyleonard 10:8862c8779b71 2 * Nucleo STM32F4(or L4) quadrature decoder, ADCs and DACs
tonnyleonard 8:28ad0ba5a673 3 * with serial communication over the USB virtual serial port
tonnyleonard 10:8862c8779b71 4
tonnyleonard 8:28ad0ba5a673 5 * Developed for Elliptec X15 piezoelectric motor control, on a L432KC board
tonnyleonard 8:28ad0ba5a673 6 *
tonnyleonard 0:789510d98ade 7 * Using STM32's counter peripherals to interface rotary encoders.
tonnyleonard 10:8862c8779b71 8 * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count,
tonnyleonard 8:28ad0ba5a673 9 * others 16bit.
tonnyleonard 10:8862c8779b71 10 * Take into account that on F4xx Mbed uses TIM5 for system timer, SPI needs TIM1,
tonnyleonard 8:28ad0ba5a673 11 * others are used for PWM.
tonnyleonard 10:8862c8779b71 12 * Check your platform's PeripheralPins.c & PeripheralNames.h if you need
tonnyleonard 8:28ad0ba5a673 13 * both PWM & encoders. This project does not use PWM.
tonnyleonard 0:789510d98ade 14 *
tonnyleonard 10:8862c8779b71 15 * On L432KC, for example, only TIM2 has 32bit count, others have 16bit.
tonnyleonard 8:28ad0ba5a673 16 * However, mbed has TIM2 assigned by default to the system ticker
tonnyleonard 10:8862c8779b71 17 * For L432KC to work with TIM2 encoder input one needs to reassign
tonnyleonard 10:8862c8779b71 18 * the system ticker from TIM2 to TIM7, for example,
tonnyleonard 8:28ad0ba5a673 19 * in mbed-dev/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/device/hal_tick.h
tonnyleonard 8:28ad0ba5a673 20 *
tonnyleonard 8:28ad0ba5a673 21 * Edit HAL_TIM_Encoder_MspInitFx(Lx).cpp to suit your mcu & board's available
tonnyleonard 8:28ad0ba5a673 22 * pinouts & pullups/downs.
tonnyleonard 0:789510d98ade 23 *
tonnyleonard 0:789510d98ade 24 * Thanks to:
tonnyleonard 8:28ad0ba5a673 25 * David Lowe (for the quadrature encoder code)
tonnyleonard 8:28ad0ba5a673 26 * https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/
tonnyleonard 10:8862c8779b71 27 *
tonnyleonard 9:4d736d29ce19 28 * And Frederic Blanc
tonnyleonard 9:4d736d29ce19 29 * https://developer.mbed.org/users/fblanc/code/AnalogIn_Diff/
tonnyleonard 9:4d736d29ce19 30 *
tonnyleonard 9:4d736d29ce19 31 * And Eric Lewiston / STM32L4xx_HAL_Driver
tonnyleonard 9:4d736d29ce19 32 * https://developer.mbed.org/users/EricLew/code/STM32L4xx_HAL_Driver/docs/tip/group__ADC__LL__EF__Configuration__Channels.html
tonnyleonard 0:789510d98ade 33 *
tonnyleonard 0:789510d98ade 34 * References:
tonnyleonard 8:28ad0ba5a673 35 * http://www.st.com/resource/en/datasheet/stm32l432kc.pdf
tonnyleonard 8:28ad0ba5a673 36 * https://developer.mbed.org/platforms/ST-Nucleo-L432KC/
tonnyleonard 0:789510d98ade 37 * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf
tonnyleonard 0:789510d98ade 38 * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf
tonnyleonard 7:7f59b69d8895 39 *
tonnyleonard 8:28ad0ba5a673 40 * Tonny-Leonard Farauanu, 2017
tonnyleonard 0:789510d98ade 41 */
tonnyleonard 0:789510d98ade 42
tonnyleonard 0:789510d98ade 43 #include "mbed.h"
tonnyleonard 0:789510d98ade 44 #include "Encoder.h"
tonnyleonard 8:28ad0ba5a673 45 #include "inttypes.h" // for PRIu32 encoding
tonnyleonard 8:28ad0ba5a673 46
tonnyleonard 8:28ad0ba5a673 47 //Defining the timer and its coresponding encoder
tonnyleonard 8:28ad0ba5a673 48 TIM_HandleTypeDef timer2;
tonnyleonard 4:4f115819171f 49 TIM_Encoder_InitTypeDef encoder1;
tonnyleonard 8:28ad0ba5a673 50
tonnyleonard 8:28ad0ba5a673 51 //The input for the encoder's index channel
tonnyleonard 5:29372f6cb533 52 InterruptIn event(PA_8);
tonnyleonard 8:28ad0ba5a673 53
tonnyleonard 8:28ad0ba5a673 54 //LED1 to signal USB serial RX interrupt reading
tonnyleonard 8:28ad0ba5a673 55 DigitalOut led1(LED1);
tonnyleonard 8:28ad0ba5a673 56
tonnyleonard 8:28ad0ba5a673 57 //LED2 to signal the encoder's index impulses
tonnyleonard 10:8862c8779b71 58 //only for feedback, to calibrate the position
tonnyleonard 8:28ad0ba5a673 59 //of the AVAGO encoder with respect to the shaft
tonnyleonard 8:28ad0ba5a673 60 DigitalOut led2(PB_4);
tonnyleonard 8:28ad0ba5a673 61
tonnyleonard 9:4d736d29ce19 62 //AnalogIn vref(ADC_VREF);
tonnyleonard 9:4d736d29ce19 63 //AnalogIn tempint(ADC_TEMP);
tonnyleonard 8:28ad0ba5a673 64
tonnyleonard 8:28ad0ba5a673 65 //Defining the ADCs
tonnyleonard 6:5d4c09973041 66 AnalogIn adc1(PA_3); //ADC1_IN8
tonnyleonard 10:8862c8779b71 67 AnalogIn adc2(PA_7); //ADC1_IN9
tonnyleonard 6:5d4c09973041 68 AnalogIn adc3(PA_6); //ADC1_IN11
tonnyleonard 8:28ad0ba5a673 69
tonnyleonard 10:8862c8779b71 70 //Defining the DAC for setting the half of the current amplitude
tonnyleonard 10:8862c8779b71 71 //and generate a quasi square signal for the PLL current phase input
tonnyleonard 10:8862c8779b71 72 AnalogOut dac1(PA_4); // DAC1_OUT1
tonnyleonard 10:8862c8779b71 73
tonnyleonard 10:8862c8779b71 74 //Defining the DAC for the Power Source
tonnyleonard 8:28ad0ba5a673 75 //(DCPS - Digitally Controlled Power Source)
tonnyleonard 8:28ad0ba5a673 76 //DAC1_OUT2 on pin PA_5 is at least twices as fast as DAC1_OUT1 on pin PA_4
tonnyleonard 10:8862c8779b71 77 AnalogOut dac2(PA_5); // DAC1_OUT2
tonnyleonard 8:28ad0ba5a673 78
tonnyleonard 8:28ad0ba5a673 79 //Defining the serial object to be used for communicating with Raspi
tonnyleonard 8:28ad0ba5a673 80 Serial raspi(USBTX, USBRX);
tonnyleonard 10:8862c8779b71 81
tonnyleonard 8:28ad0ba5a673 82 //Serial& raspi = get_stdio_serial();
tonnyleonard 7:7f59b69d8895 83 //Serial debug(PB_6,PA_10); // Serial1 tx rx
tonnyleonard 7:7f59b69d8895 84
tonnyleonard 8:28ad0ba5a673 85 uint16_t count1=0;
tonnyleonard 5:29372f6cb533 86 int16_t count2=0;
tonnyleonard 8:28ad0ba5a673 87 int32_t count3=0;
tonnyleonard 5:29372f6cb533 88 int16_t adjust=0;
tonnyleonard 8:28ad0ba5a673 89
tonnyleonard 10:8862c8779b71 90 float speedLast=0;
tonnyleonard 10:8862c8779b71 91
tonnyleonard 8:28ad0ba5a673 92 float dac_val=0;
tonnyleonard 10:8862c8779b71 93 float adc3_val=0;
tonnyleonard 8:28ad0ba5a673 94
tonnyleonard 7:7f59b69d8895 95 volatile bool adc3_en = false;
tonnyleonard 8:28ad0ba5a673 96 int16_t lines = 4;
tonnyleonard 8:28ad0ba5a673 97
tonnyleonard 8:28ad0ba5a673 98 //TIM1 to be used with the interrupt for the encoder's index pulses
tonnyleonard 5:29372f6cb533 99 Timer timer1;
tonnyleonard 10:8862c8779b71 100 Timer timer3;
tonnyleonard 8:28ad0ba5a673 101
tonnyleonard 8:28ad0ba5a673 102 //Function invoked by the encoder's index interrupt pulses
tonnyleonard 7:7f59b69d8895 103 void atint()
tonnyleonard 7:7f59b69d8895 104 {
tonnyleonard 5:29372f6cb533 105 timer1.start();
tonnyleonard 7:7f59b69d8895 106 if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2)) {
tonnyleonard 7:7f59b69d8895 107 adjust--;
tonnyleonard 10:8862c8779b71 108 if (adjust == -1) {
tonnyleonard 5:29372f6cb533 109 count2--;
tonnyleonard 5:29372f6cb533 110 count1 +=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 9:4d736d29ce19 111 //TIM2->CNT = 0x0000;
tonnyleonard 5:29372f6cb533 112 adjust = 0;
tonnyleonard 8:28ad0ba5a673 113 led2 =!led2;
tonnyleonard 5:29372f6cb533 114 }
tonnyleonard 7:7f59b69d8895 115 } else {
tonnyleonard 5:29372f6cb533 116 adjust++;
tonnyleonard 10:8862c8779b71 117 if (adjust == 1) {
tonnyleonard 5:29372f6cb533 118 count2++;
tonnyleonard 5:29372f6cb533 119 count1 +=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 9:4d736d29ce19 120 //TIM2->CNT = 0x0000;
tonnyleonard 5:29372f6cb533 121 adjust = 0;
tonnyleonard 8:28ad0ba5a673 122 led2 =!led2;
tonnyleonard 5:29372f6cb533 123 }
tonnyleonard 5:29372f6cb533 124 }
tonnyleonard 5:29372f6cb533 125 }
tonnyleonard 10:8862c8779b71 126 float speedRead(){
tonnyleonard 10:8862c8779b71 127 uint16_t i = 0;
tonnyleonard 10:8862c8779b71 128 uint32_t deltaT;
tonnyleonard 10:8862c8779b71 129 float speed;
tonnyleonard 10:8862c8779b71 130 uint32_t pos1;
tonnyleonard 10:8862c8779b71 131 uint32_t pos2;
tonnyleonard 10:8862c8779b71 132 int32_t pos;
tonnyleonard 10:8862c8779b71 133 int8_t sens1;
tonnyleonard 10:8862c8779b71 134 int8_t sens2;
tonnyleonard 10:8862c8779b71 135 printf("Encoder speed reading!\r\n");
tonnyleonard 10:8862c8779b71 136 timer3.start();
tonnyleonard 10:8862c8779b71 137 pos1=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 138 sens1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 10:8862c8779b71 139 wait_ms(5);
tonnyleonard 10:8862c8779b71 140 pos2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 141 sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 10:8862c8779b71 142
tonnyleonard 10:8862c8779b71 143 //The speed computation method adapts to slow/fast speeds, in order to
tonnyleonard 10:8862c8779b71 144 //optimize the rapport between computation precision and time windows size
tonnyleonard 10:8862c8779b71 145 while (pos2 == pos1 && i<99) { // The accepted max delay time is 0.5 seconds
tonnyleonard 10:8862c8779b71 146 wait_ms(5);
tonnyleonard 10:8862c8779b71 147 i++;
tonnyleonard 10:8862c8779b71 148 pos2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 149 sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 10:8862c8779b71 150 }
tonnyleonard 10:8862c8779b71 151 pos2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 152 sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 10:8862c8779b71 153 timer3.stop();
tonnyleonard 10:8862c8779b71 154 deltaT = timer3.read_us();
tonnyleonard 10:8862c8779b71 155 timer3.reset();
tonnyleonard 10:8862c8779b71 156 pos = (int32_t) pos2 - (int32_t) pos1;
tonnyleonard 10:8862c8779b71 157
tonnyleonard 10:8862c8779b71 158 printf("Time is %lu microseconds, position modified %ld %lu %lu\r\n", deltaT, pos, pos1, pos2);
tonnyleonard 10:8862c8779b71 159 if (deltaT > 0){
tonnyleonard 10:8862c8779b71 160 speed = ((float) pos)*12.5f/((float) deltaT); // (pulses/us)*1000000/8000 -> rot/s
tonnyleonard 10:8862c8779b71 161 }
tonnyleonard 10:8862c8779b71 162 else {
tonnyleonard 10:8862c8779b71 163 printf("Error, time interval not greater than zero, speed not calculated!\r\n");
tonnyleonard 10:8862c8779b71 164 }
tonnyleonard 10:8862c8779b71 165 printf("The encoder speed is %f rot/s\r\n", speed);
tonnyleonard 10:8862c8779b71 166 return speed;
tonnyleonard 10:8862c8779b71 167 }
tonnyleonard 0:789510d98ade 168
tonnyleonard 8:28ad0ba5a673 169 //Function attached to the serial RX interrupt event
tonnyleonard 7:7f59b69d8895 170 void readData(void)
tonnyleonard 7:7f59b69d8895 171 {
tonnyleonard 7:7f59b69d8895 172 char message[125];
tonnyleonard 8:28ad0ba5a673 173 if(raspi.readable()) {
tonnyleonard 8:28ad0ba5a673 174 int p1=0;
tonnyleonard 8:28ad0ba5a673 175 led1 = 1;
tonnyleonard 8:28ad0ba5a673 176 raspi.scanf("%s", message);
tonnyleonard 7:7f59b69d8895 177 printf("%d %s\r\n", strlen(message), message);
tonnyleonard 7:7f59b69d8895 178 if (strcmp(message, "startAdc") == 0) {
tonnyleonard 7:7f59b69d8895 179 adc3_en = true;
tonnyleonard 8:28ad0ba5a673 180 lines = 6;
tonnyleonard 7:7f59b69d8895 181 printf("true\r\n");
tonnyleonard 8:28ad0ba5a673 182 } else if (strcmp(message, "stopAdc") == 0) {
tonnyleonard 7:7f59b69d8895 183 adc3_en = false;
tonnyleonard 8:28ad0ba5a673 184 lines = 4;
tonnyleonard 7:7f59b69d8895 185 printf("false\r\n");
tonnyleonard 10:8862c8779b71 186 } else if (p1=strstr(message, "DAC=") != NULL) {
tonnyleonard 8:28ad0ba5a673 187 dac_val = atof(message+p1+3);
tonnyleonard 10:8862c8779b71 188 dac2.write(dac_val);
tonnyleonard 8:28ad0ba5a673 189 printf("Value to write to DAC: %f\r\n", dac_val*3.3f);
tonnyleonard 10:8862c8779b71 190 } else if (strcmp(message, "reset") == 0) {
tonnyleonard 10:8862c8779b71 191 TIM2->CNT = 0x0000;
tonnyleonard 10:8862c8779b71 192 printf("Encoder reset!\r\n");
tonnyleonard 10:8862c8779b71 193 }
tonnyleonard 10:8862c8779b71 194
tonnyleonard 8:28ad0ba5a673 195 }
tonnyleonard 8:28ad0ba5a673 196 led1 = 0;
tonnyleonard 8:28ad0ba5a673 197 }
tonnyleonard 7:7f59b69d8895 198
tonnyleonard 8:28ad0ba5a673 199 //The main function
tonnyleonard 0:789510d98ade 200 int main()
tonnyleonard 0:789510d98ade 201 {
tonnyleonard 0:789510d98ade 202
tonnyleonard 7:7f59b69d8895 203 //counting on both A&B inputs (A at PA0, B at PA1), 4 ticks per cycle,
tonnyleonard 5:29372f6cb533 204 //full 32-bit count
tonnyleonard 7:7f59b69d8895 205 //For L432KC to work with TIM2 one needs to reassign the system ticker
tonnyleonard 10:8862c8779b71 206 //from TIM2 to TIM7 in TARGET/../device/hal_tick.h\
tonnyleonard 8:28ad0ba5a673 207 //Initialise encoder
tonnyleonard 4:4f115819171f 208 EncoderInit(&encoder1, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
tonnyleonard 10:8862c8779b71 209
tonnyleonard 10:8862c8779b71 210 //Define function to call for interrupt event rise
tonnyleonard 8:28ad0ba5a673 211 //This is triggered by the encoder's index pulses
tonnyleonard 8:28ad0ba5a673 212 //and it resets the encoder counter
tonnyleonard 5:29372f6cb533 213 event.rise(&atint);
tonnyleonard 10:8862c8779b71 214
tonnyleonard 8:28ad0ba5a673 215 //Attach functin to call for serial interrupt event
tonnyleonard 8:28ad0ba5a673 216 raspi.attach(&readData);
tonnyleonard 7:7f59b69d8895 217
tonnyleonard 8:28ad0ba5a673 218 //Message to mark the initialisation of the program
tonnyleonard 4:4f115819171f 219 printf("\n\rSTM HAL encoder with ADC and DAC\n\r");
tonnyleonard 9:4d736d29ce19 220 printf("Running at %u MHz\r\n\n", HAL_RCC_GetSysClockFreq()/1000000);
tonnyleonard 7:7f59b69d8895 221
tonnyleonard 6:5d4c09973041 222 //printf("%" PRIu32 "\n", UINT32_MAX);
tonnyleonard 7:7f59b69d8895 223
tonnyleonard 10:8862c8779b71 224 int i = 0;
tonnyleonard 8:28ad0ba5a673 225 //The main loop
tonnyleonard 0:789510d98ade 226 while(1) {
tonnyleonard 10:8862c8779b71 227
tonnyleonard 8:28ad0ba5a673 228 //Variable for the one loop encoder counter
tonnyleonard 5:29372f6cb533 229 uint32_t count3=0;
tonnyleonard 7:7f59b69d8895 230
tonnyleonard 8:28ad0ba5a673 231 //Variable for the direction of the counter
tonnyleonard 4:4f115819171f 232 int8_t dir1;
tonnyleonard 10:8862c8779b71 233
tonnyleonard 9:4d736d29ce19 234 //uint16_t voltage1=0;
tonnyleonard 0:789510d98ade 235
tonnyleonard 0:789510d98ade 236
tonnyleonard 0:789510d98ade 237 //OK 401 411 446 NOK 030
tonnyleonard 5:29372f6cb533 238 //count1=TIM2->CNT;
tonnyleonard 5:29372f6cb533 239 //dir1=TIM2->CR1&TIM_CR1_DIR;
tonnyleonard 10:8862c8779b71 240
tonnyleonard 8:28ad0ba5a673 241 //It reads the ADC1 value converted from 12bit to 16bit resolution
tonnyleonard 9:4d736d29ce19 242 //voltage1=adc1.read_u16();
tonnyleonard 10:8862c8779b71 243
tonnyleonard 8:28ad0ba5a673 244 //It resets the DAC1
tonnyleonard 8:28ad0ba5a673 245 //dac1.free();
tonnyleonard 10:8862c8779b71 246
tonnyleonard 8:28ad0ba5a673 247 //It writes the DAC1 value as a 16bit number
tonnyleonard 8:28ad0ba5a673 248 //dac1.write_u16(count3*2);
tonnyleonard 10:8862c8779b71 249
tonnyleonard 8:28ad0ba5a673 250 //It writes the DAC1 value as a subunitary float number
tonnyleonard 8:28ad0ba5a673 251 //to be multiplied with the max DAC_RANGE
tonnyleonard 8:28ad0ba5a673 252
tonnyleonard 10:8862c8779b71 253
tonnyleonard 8:28ad0ba5a673 254 //It gets the one loop position and the direction of the encoder
tonnyleonard 5:29372f6cb533 255 count3=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 4:4f115819171f 256 dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 10:8862c8779b71 257
tonnyleonard 10:8862c8779b71 258 if (i >= 100) {
tonnyleonard 10:8862c8779b71 259 adc3_val = adc3.read();
tonnyleonard 10:8862c8779b71 260 dac1.write(adc3_val);
tonnyleonard 10:8862c8779b71 261
tonnyleonard 10:8862c8779b71 262 printf("%ld%s passes=%d\r\n", count3, dir1==0 ? "+":"-", count2);
tonnyleonard 10:8862c8779b71 263 printf("Voltage ADC1= %3.3f%V, DAC=%f\r\n", adc1.read()*3.3f, dac2.read()*3.3f);
tonnyleonard 10:8862c8779b71 264 //printf("Vref: %f\r\n", vref.read());
tonnyleonard 10:8862c8779b71 265 printf("Voltage ADC2: %3.3f%V\r\n", adc2.read()*3.3f);
tonnyleonard 6:5d4c09973041 266
tonnyleonard 10:8862c8779b71 267 //printf("Vref(f): %f, Vref : %u, Temperature : %u\r\n",
tonnyleonard 10:8862c8779b71 268 // vref.read(), vref.read_u16(), tempint.read_u16());
tonnyleonard 10:8862c8779b71 269 if (adc3_en) {
tonnyleonard 10:8862c8779b71 270 printf("Voltage ADC3: %3.3f%V\r\n", adc3_val*3.3f);
tonnyleonard 10:8862c8779b71 271 printf("Voltage ADC3: %u\r\n", adc3.read_u16());
tonnyleonard 10:8862c8779b71 272 printf("DAC1 read: %3.3f%V\r\n", dac1.read()*3.3f);
tonnyleonard 10:8862c8779b71 273 printf("DAC2 read: %3.3f%V\r\n", dac2.read()*3.3f);
tonnyleonard 10:8862c8779b71 274 }
tonnyleonard 10:8862c8779b71 275 speedLast = speedRead();
tonnyleonard 10:8862c8779b71 276 //printf("\033[%dA", lines); // Moves cursor up of #lines
tonnyleonard 10:8862c8779b71 277
tonnyleonard 10:8862c8779b71 278 i=0;
tonnyleonard 10:8862c8779b71 279 }
tonnyleonard 10:8862c8779b71 280 i++;
tonnyleonard 10:8862c8779b71 281
tonnyleonard 10:8862c8779b71 282 wait(0.04);
tonnyleonard 0:789510d98ade 283 }
tonnyleonard 8:28ad0ba5a673 284
tonnyleonard 8:28ad0ba5a673 285 }