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:
Thu Jun 15 17:20:59 2017 +0000
Revision:
17:75815e312312
Parent:
16:e423f891cfbc
Child:
18:1ef20c13693c
Adjust error messages and change serial read method from scanf() to getc()

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
tonnyleonard 8:28ad0ba5a673 46 //Defining the timer and its coresponding encoder
tonnyleonard 8:28ad0ba5a673 47 TIM_HandleTypeDef timer2;
tonnyleonard 4:4f115819171f 48 TIM_Encoder_InitTypeDef encoder1;
tonnyleonard 8:28ad0ba5a673 49
tonnyleonard 15:8adff67fe707 50 //Timer to be used for the speed computation function
tonnyleonard 15:8adff67fe707 51 Timer timer1;
tonnyleonard 17:75815e312312 52 Timer timer3;
tonnyleonard 15:8adff67fe707 53
tonnyleonard 8:28ad0ba5a673 54 //The input for the encoder's index channel
tonnyleonard 5:29372f6cb533 55 InterruptIn event(PA_8);
tonnyleonard 8:28ad0ba5a673 56
tonnyleonard 8:28ad0ba5a673 57 //LED1 to signal USB serial RX interrupt reading
tonnyleonard 8:28ad0ba5a673 58 DigitalOut led1(LED1);
tonnyleonard 8:28ad0ba5a673 59
tonnyleonard 8:28ad0ba5a673 60 //LED2 to signal the encoder's index impulses
tonnyleonard 10:8862c8779b71 61 //only for feedback, to calibrate the position
tonnyleonard 8:28ad0ba5a673 62 //of the AVAGO encoder with respect to the shaft
tonnyleonard 8:28ad0ba5a673 63 DigitalOut led2(PB_4);
tonnyleonard 8:28ad0ba5a673 64
tonnyleonard 15:8adff67fe707 65 //Relay port to power on and off the Digitally Controlled Power Source (DCPS)
tonnyleonard 12:8c355d78e081 66 DigitalOut relay1(PB_5);
tonnyleonard 12:8c355d78e081 67
tonnyleonard 8:28ad0ba5a673 68 //Defining the ADCs
tonnyleonard 15:8adff67fe707 69 //For reading the Phase Comparator output
tonnyleonard 6:5d4c09973041 70 AnalogIn adc1(PA_3); //ADC1_IN8
tonnyleonard 15:8adff67fe707 71 //For reading the DCPS output
tonnyleonard 15:8adff67fe707 72 AnalogIn adc2(PA_4); //ADC1_IN9
tonnyleonard 15:8adff67fe707 73 //For reading the current value from the shunt monitor
tonnyleonard 6:5d4c09973041 74 AnalogIn adc3(PA_6); //ADC1_IN11
tonnyleonard 8:28ad0ba5a673 75
tonnyleonard 15:8adff67fe707 76 //Defining the DAC for the input of DCPS
tonnyleonard 8:28ad0ba5a673 77 //(DCPS - Digitally Controlled Power Source)
tonnyleonard 8:28ad0ba5a673 78 //DAC1_OUT2 on pin PA_5 is at least twices as fast as DAC1_OUT1 on pin PA_4
tonnyleonard 15:8adff67fe707 79 AnalogOut dac1(PA_5); // DAC1_OUT2
tonnyleonard 8:28ad0ba5a673 80
tonnyleonard 8:28ad0ba5a673 81 //Defining the serial object to be used for communicating with Raspi
tonnyleonard 8:28ad0ba5a673 82 Serial raspi(USBTX, USBRX);
tonnyleonard 10:8862c8779b71 83
tonnyleonard 15:8adff67fe707 84 //Counter for the absolute position from start
tonnyleonard 15:8adff67fe707 85 int32_t count1 = 0;
tonnyleonard 15:8adff67fe707 86 //Counter for the index passes
tonnyleonard 15:8adff67fe707 87 int32_t count2 = 0;
tonnyleonard 15:8adff67fe707 88 //Records the wheel offset position to the index
tonnyleonard 15:8adff67fe707 89 int32_t count3 = 0;
tonnyleonard 15:8adff67fe707 90 //Counter recording absolute position at last index pulse
tonnyleonard 15:8adff67fe707 91 int32_t count4 = 0;
tonnyleonard 15:8adff67fe707 92 //Used to filter the first index pulse, so that the offset position
tonnyleonard 15:8adff67fe707 93 //to the index may be recorded
tonnyleonard 15:8adff67fe707 94 volatile bool adjustOffset = false;
tonnyleonard 14:e5f5b345b2fe 95
tonnyleonard 15:8adff67fe707 96 //Last data written to dac1 (using subunitary values)
tonnyleonard 15:8adff67fe707 97 float dac_val = 0.0;
tonnyleonard 8:28ad0ba5a673 98
tonnyleonard 14:e5f5b345b2fe 99 //Array with adc1, adc2 & adc3 correction addition,
tonnyleonard 15:8adff67fe707 100 //experimentally determined (hardware offset voltage dependent)
tonnyleonard 14:e5f5b345b2fe 101 const float adc_corr[] = { 0, 0.022f, 0.2995, 0.0267};
tonnyleonard 15:8adff67fe707 102
tonnyleonard 15:8adff67fe707 103 //Enable ADC reading and printing to the serial interface
tonnyleonard 15:8adff67fe707 104 bool adc_en = true;
tonnyleonard 15:8adff67fe707 105
tonnyleonard 15:8adff67fe707 106 //Enables speed computing and printing to the serial interface
tonnyleonard 15:8adff67fe707 107 bool speed_en = true;
tonnyleonard 14:e5f5b345b2fe 108
tonnyleonard 15:8adff67fe707 109 //Enable position reading and printing to the serial interface
tonnyleonard 15:8adff67fe707 110 bool pos_en = true;
tonnyleonard 8:28ad0ba5a673 111
tonnyleonard 15:8adff67fe707 112 //Enable alternative position computation and printing to the serial interface,
tonnyleonard 15:8adff67fe707 113 //relative to the encoder index pulses counter
tonnyleonard 15:8adff67fe707 114 bool posIndex_en = true;
tonnyleonard 8:28ad0ba5a673 115
tonnyleonard 8:28ad0ba5a673 116 //Function invoked by the encoder's index interrupt pulses
tonnyleonard 15:8adff67fe707 117 //It counts the index pulses, incrementing or decrementing the number,
tonnyleonard 15:8adff67fe707 118 //and registers the offset position of the wheel at start in count3;
tonnyleonard 15:8adff67fe707 119 //The index counter is used to correct possible reading errors of the
tonnyleonard 15:8adff67fe707 120 //encoder's counter
tonnyleonard 15:8adff67fe707 121 //led2 is used for visual feedback.
tonnyleonard 7:7f59b69d8895 122 void atint()
tonnyleonard 7:7f59b69d8895 123 {
tonnyleonard 15:8adff67fe707 124 count4 =__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 7:7f59b69d8895 125 if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2)) {
tonnyleonard 16:e423f891cfbc 126 if (count2 == 0 && adjustOffset == false) { //catch first index pulse
tonnyleonard 15:8adff67fe707 127 count3 = count4;
tonnyleonard 15:8adff67fe707 128 adjustOffset = true;
tonnyleonard 5:29372f6cb533 129 }
tonnyleonard 15:8adff67fe707 130 count2--;
tonnyleonard 15:8adff67fe707 131 led2 =!led2;
tonnyleonard 7:7f59b69d8895 132 } else {
tonnyleonard 15:8adff67fe707 133 if (count2 == 0 && adjustOffset == false) { //catch first index pulse
tonnyleonard 15:8adff67fe707 134 count3 = count4;
tonnyleonard 15:8adff67fe707 135 adjustOffset = true;
tonnyleonard 5:29372f6cb533 136 }
tonnyleonard 15:8adff67fe707 137 count2++;
tonnyleonard 15:8adff67fe707 138 led2 =!led2;
tonnyleonard 5:29372f6cb533 139 }
tonnyleonard 5:29372f6cb533 140 }
tonnyleonard 15:8adff67fe707 141
tonnyleonard 15:8adff67fe707 142 //Function for adaptive speed computation
tonnyleonard 14:e5f5b345b2fe 143 float speedRead()
tonnyleonard 14:e5f5b345b2fe 144 {
tonnyleonard 15:8adff67fe707 145 //counter for the waiting time window
tonnyleonard 10:8862c8779b71 146 uint16_t i = 0;
tonnyleonard 15:8adff67fe707 147 //sample time for speed computation
tonnyleonard 10:8862c8779b71 148 uint32_t deltaT;
tonnyleonard 15:8adff67fe707 149
tonnyleonard 15:8adff67fe707 150 //Computer speed
tonnyleonard 10:8862c8779b71 151 float speed;
tonnyleonard 15:8adff67fe707 152
tonnyleonard 15:8adff67fe707 153 //First and second registered position
tonnyleonard 15:8adff67fe707 154 int32_t pos1;
tonnyleonard 15:8adff67fe707 155 int32_t pos2;
tonnyleonard 15:8adff67fe707 156 //position difference to be used for speed computation
tonnyleonard 10:8862c8779b71 157 int32_t pos;
tonnyleonard 15:8adff67fe707 158 //Direction of rotation, read from the encoder, for the first
tonnyleonard 15:8adff67fe707 159 //and the second registered positions
tonnyleonard 10:8862c8779b71 160 int8_t sens1;
tonnyleonard 10:8862c8779b71 161 int8_t sens2;
tonnyleonard 15:8adff67fe707 162
tonnyleonard 15:8adff67fe707 163 timer1.start();
tonnyleonard 10:8862c8779b71 164 pos1=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 165 sens1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 15:8adff67fe707 166 //Minumum waiting time for this project would be 100 microseconds,
tonnyleonard 15:8adff67fe707 167 //for 10 kHz quadrature frequency. We will set it 100 times longer
tonnyleonard 15:8adff67fe707 168 //wait_ms(10);
tonnyleonard 15:8adff67fe707 169 // Avoiding wait(), since it uses interrupts and timing here
tonnyleonard 15:8adff67fe707 170 // is not critical. This takes 10 ms, if not interrupted
tonnyleonard 16:e423f891cfbc 171 for (uint32_t j=0; j<199950; j++) {}
tonnyleonard 10:8862c8779b71 172 pos2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 173 sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 14:e5f5b345b2fe 174
tonnyleonard 10:8862c8779b71 175 //The speed computation method adapts to slow/fast speeds, in order to
tonnyleonard 10:8862c8779b71 176 //optimize the rapport between computation precision and time windows size
tonnyleonard 15:8adff67fe707 177 while (pos2 == pos1 && i<49) { // The accepted max delay time is 0.5 seconds
tonnyleonard 15:8adff67fe707 178 //wait_ms(10);
tonnyleonard 15:8adff67fe707 179 // Avoiding wait(), since it uses interrupts and timing here
tonnyleonard 15:8adff67fe707 180 // is not critical. This takes 10 ms, if not interrupted
tonnyleonard 16:e423f891cfbc 181 for (uint32_t j=0; j<199950; j++) {}
tonnyleonard 10:8862c8779b71 182 i++;
tonnyleonard 10:8862c8779b71 183 pos2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 184 sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 14:e5f5b345b2fe 185 }
tonnyleonard 10:8862c8779b71 186 pos2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 187 sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 15:8adff67fe707 188 timer1.stop();
tonnyleonard 15:8adff67fe707 189 deltaT = timer1.read_us();
tonnyleonard 15:8adff67fe707 190 timer1.reset();
tonnyleonard 15:8adff67fe707 191 if (sens1 == sens2) {
tonnyleonard 15:8adff67fe707 192 pos = pos2 - pos1;
tonnyleonard 15:8adff67fe707 193 } else {
tonnyleonard 17:75815e312312 194 printf("\nE:Speed computation error, change of direction between readings!\n");
tonnyleonard 15:8adff67fe707 195 }
tonnyleonard 14:e5f5b345b2fe 196
tonnyleonard 15:8adff67fe707 197 //For debugging
tonnyleonard 16:e423f891cfbc 198 //printf("%lu microseconds, %ld steps: start:%lu stop%lu\n", deltaT, pos, pos1, pos2);
tonnyleonard 14:e5f5b345b2fe 199 if (deltaT > 0) {
tonnyleonard 15:8adff67fe707 200 //speed computation in rot/s
tonnyleonard 11:7a9837acbea0 201 speed = ((float) pos)*125.f/((float) deltaT); // (pulses/us)*1000000/8000 -> rot/s
tonnyleonard 14:e5f5b345b2fe 202 } else {
tonnyleonard 17:75815e312312 203 printf("\nE:Error, time interval not greater than zero, speed not calculated!\n");
tonnyleonard 14:e5f5b345b2fe 204 }
tonnyleonard 10:8862c8779b71 205 return speed;
tonnyleonard 14:e5f5b345b2fe 206 }
tonnyleonard 0:789510d98ade 207
tonnyleonard 15:8adff67fe707 208 //Function attached to the serial RX interrupt event, it parses
tonnyleonard 15:8adff67fe707 209 //the serial messages and invoques the corresponding commands
tonnyleonard 7:7f59b69d8895 210 void readData(void)
tonnyleonard 7:7f59b69d8895 211 {
tonnyleonard 17:75815e312312 212 led1 = 1;
tonnyleonard 17:75815e312312 213 char message[15];
tonnyleonard 17:75815e312312 214 if (raspi.readable()) {
tonnyleonard 15:8adff67fe707 215 // Signalling the beginning of serial read
tonnyleonard 17:75815e312312 216 int i = 0;
tonnyleonard 17:75815e312312 217 bool rx = true;
tonnyleonard 17:75815e312312 218 while(rx && i<14) {
tonnyleonard 17:75815e312312 219 message[i] = raspi.getc();
tonnyleonard 17:75815e312312 220 if (message[i] == '\r') {
tonnyleonard 17:75815e312312 221 message[i] = NULL;
tonnyleonard 17:75815e312312 222 rx = false;
tonnyleonard 17:75815e312312 223 }
tonnyleonard 17:75815e312312 224 i++;
tonnyleonard 17:75815e312312 225 }
tonnyleonard 15:8adff67fe707 226 //Message received printed for debugging
tonnyleonard 16:e423f891cfbc 227 //printf("M:%d %s\n", strlen(message), message);
tonnyleonard 17:75815e312312 228 int p1 = 0;
tonnyleonard 15:8adff67fe707 229 if (strcmp(message, "adcOn") == 0) {
tonnyleonard 15:8adff67fe707 230 adc_en = true;
tonnyleonard 17:75815e312312 231 printf("\nM:ADC true\n");
tonnyleonard 15:8adff67fe707 232 } else if (strcmp(message, "adcOff") == 0) {
tonnyleonard 15:8adff67fe707 233 adc_en = false;
tonnyleonard 17:75815e312312 234 printf("\nM:ADC false\n");
tonnyleonard 15:8adff67fe707 235 } else if (p1=strstr(message, "dac=") != NULL) {
tonnyleonard 15:8adff67fe707 236 //Writing the dac1 value read from serial
tonnyleonard 14:e5f5b345b2fe 237 //The DCPS has 1V offset, so we have to remove it
tonnyleonard 15:8adff67fe707 238 dac_val = (atof(message+p1+3)-1.0f)/15.51;
tonnyleonard 15:8adff67fe707 239 dac1.write(dac_val);
tonnyleonard 17:75815e312312 240 printf("\nM:Value to write to DAC: %f\n", dac_val*3.3f);
tonnyleonard 10:8862c8779b71 241 } else if (strcmp(message, "reset") == 0) {
tonnyleonard 15:8adff67fe707 242 //encoder related counters reset command
tonnyleonard 10:8862c8779b71 243 TIM2->CNT = 0x0000;
tonnyleonard 15:8adff67fe707 244 count1 = 0;
tonnyleonard 15:8adff67fe707 245 count2 = 0;
tonnyleonard 15:8adff67fe707 246 count3 = 0;
tonnyleonard 15:8adff67fe707 247 count4 = 0;
tonnyleonard 15:8adff67fe707 248 adjustOffset = false;
tonnyleonard 17:75815e312312 249 printf("\nM:Encoder counters reset!\n");
tonnyleonard 15:8adff67fe707 250 } else if (strcmp(message, "powerOn") == 0) {
tonnyleonard 15:8adff67fe707 251 //command to power on the DCPS
tonnyleonard 12:8c355d78e081 252 relay1.write(1);
tonnyleonard 17:75815e312312 253 printf("\nM:DCPS on\n");
tonnyleonard 15:8adff67fe707 254 } else if (strcmp(message, "powerOff") == 0) {
tonnyleonard 15:8adff67fe707 255 //command to power off the DCPS
tonnyleonard 12:8c355d78e081 256 relay1.write(0);
tonnyleonard 16:e423f891cfbc 257 printf("M:DCPS off\n");
tonnyleonard 15:8adff67fe707 258 } else if (strcmp(message, "posOn") == 0) {
tonnyleonard 15:8adff67fe707 259 //command to enable the encoder position notification
tonnyleonard 15:8adff67fe707 260 pos_en = true;
tonnyleonard 17:75815e312312 261 printf("\nM:Position notification enabled\n");
tonnyleonard 15:8adff67fe707 262 } else if (strcmp(message, "posOff") == 0) {
tonnyleonard 15:8adff67fe707 263 //command to disable the encoder position notification
tonnyleonard 15:8adff67fe707 264 pos_en = false;
tonnyleonard 17:75815e312312 265 printf("\nM:Position notification disabled\n");
tonnyleonard 15:8adff67fe707 266 } else if (strcmp(message, "posIndexOn") == 0) {
tonnyleonard 15:8adff67fe707 267 //command to enable the index related encoder position notification
tonnyleonard 15:8adff67fe707 268 posIndex_en = true;
tonnyleonard 17:75815e312312 269 printf("\nM:Index related position notification enabled\n");
tonnyleonard 15:8adff67fe707 270 } else if (strcmp(message, "posIndexOff") == 0) {
tonnyleonard 15:8adff67fe707 271 //command to disable the index related encoder position notification
tonnyleonard 15:8adff67fe707 272 posIndex_en = false;
tonnyleonard 17:75815e312312 273 printf("\nM:Index related position notification disabled\n");
tonnyleonard 15:8adff67fe707 274 } else if (strcmp(message, "speedOn") == 0) {
tonnyleonard 15:8adff67fe707 275 //command to enable speed computation and notification
tonnyleonard 15:8adff67fe707 276 speed_en = true;
tonnyleonard 17:75815e312312 277 printf("\nM:Speed enabled\n");
tonnyleonard 15:8adff67fe707 278 } else if (strcmp(message, "speedOff") == 0) {
tonnyleonard 15:8adff67fe707 279 //command to disable speed computation and notification
tonnyleonard 15:8adff67fe707 280 speed_en = false;
tonnyleonard 17:75815e312312 281 printf("\nM:Speed disabled\n");
tonnyleonard 10:8862c8779b71 282 }
tonnyleonard 8:28ad0ba5a673 283 }
tonnyleonard 17:75815e312312 284
tonnyleonard 15:8adff67fe707 285 //Signalling the end of searial read
tonnyleonard 8:28ad0ba5a673 286 led1 = 0;
tonnyleonard 8:28ad0ba5a673 287 }
tonnyleonard 7:7f59b69d8895 288
tonnyleonard 15:8adff67fe707 289 float ADC_read(AnalogIn adc)
tonnyleonard 14:e5f5b345b2fe 290 {
tonnyleonard 16:e423f891cfbc 291
tonnyleonard 15:8adff67fe707 292 float Voltage;
tonnyleonard 15:8adff67fe707 293 float Voltage_total = 0.0;
tonnyleonard 16:e423f891cfbc 294
tonnyleonard 15:8adff67fe707 295 // Voltage is summed then averaged
tonnyleonard 15:8adff67fe707 296 for (int i=0; i<100; i++) { // do 100 readings
tonnyleonard 14:e5f5b345b2fe 297 Voltage = adc.read();
tonnyleonard 14:e5f5b345b2fe 298
tonnyleonard 15:8adff67fe707 299 Voltage_total = Voltage_total+ Voltage;
tonnyleonard 14:e5f5b345b2fe 300 //wait_us(10);
tonnyleonard 14:e5f5b345b2fe 301 }
tonnyleonard 15:8adff67fe707 302 Voltage = Voltage_total/100.f;
tonnyleonard 15:8adff67fe707 303 return Voltage;
tonnyleonard 14:e5f5b345b2fe 304 }
tonnyleonard 14:e5f5b345b2fe 305
tonnyleonard 8:28ad0ba5a673 306 //The main function
tonnyleonard 0:789510d98ade 307 int main()
tonnyleonard 0:789510d98ade 308 {
tonnyleonard 12:8c355d78e081 309 //Power onn the DCPS
tonnyleonard 16:e423f891cfbc 310 relay1.write(1);
tonnyleonard 16:e423f891cfbc 311 dac1.write(0.32);
tonnyleonard 14:e5f5b345b2fe 312
tonnyleonard 7:7f59b69d8895 313 //counting on both A&B inputs (A at PA0, B at PA1), 4 ticks per cycle,
tonnyleonard 5:29372f6cb533 314 //full 32-bit count
tonnyleonard 7:7f59b69d8895 315 //For L432KC to work with TIM2 one needs to reassign the system ticker
tonnyleonard 10:8862c8779b71 316 //from TIM2 to TIM7 in TARGET/../device/hal_tick.h\
tonnyleonard 8:28ad0ba5a673 317 //Initialise encoder
tonnyleonard 4:4f115819171f 318 EncoderInit(&encoder1, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
tonnyleonard 10:8862c8779b71 319
tonnyleonard 10:8862c8779b71 320 //Define function to call for interrupt event rise
tonnyleonard 8:28ad0ba5a673 321 //This is triggered by the encoder's index pulses
tonnyleonard 8:28ad0ba5a673 322 //and it resets the encoder counter
tonnyleonard 5:29372f6cb533 323 event.rise(&atint);
tonnyleonard 14:e5f5b345b2fe 324
tonnyleonard 12:8c355d78e081 325 //Set serial baud rate
tonnyleonard 12:8c355d78e081 326 raspi.baud(115200);
tonnyleonard 14:e5f5b345b2fe 327
tonnyleonard 8:28ad0ba5a673 328 //Attach functin to call for serial interrupt event
tonnyleonard 16:e423f891cfbc 329 wait(1);
tonnyleonard 17:75815e312312 330 raspi.attach(&readData, Serial::RxIrq);
tonnyleonard 7:7f59b69d8895 331
tonnyleonard 8:28ad0ba5a673 332 //Message to mark the initialisation of the program
tonnyleonard 16:e423f891cfbc 333 printf("M:STM HAL encoder with ADC and DAC\n");
tonnyleonard 16:e423f891cfbc 334 printf("M:Running at %u MHz\n", HAL_RCC_GetSysClockFreq()/1000000);
tonnyleonard 7:7f59b69d8895 335
tonnyleonard 8:28ad0ba5a673 336 //The main loop
tonnyleonard 0:789510d98ade 337 while(1) {
tonnyleonard 10:8862c8779b71 338
tonnyleonard 8:28ad0ba5a673 339 //Variable for the direction of the counter
tonnyleonard 4:4f115819171f 340 int8_t dir1;
tonnyleonard 10:8862c8779b71 341
tonnyleonard 15:8adff67fe707 342 //Prints the timestamp in miliseconds
tonnyleonard 16:e423f891cfbc 343 printf("T:%u", us_ticker_read()/1000);
tonnyleonard 10:8862c8779b71 344
tonnyleonard 15:8adff67fe707 345 if (pos_en) {
tonnyleonard 15:8adff67fe707 346 //It gets the position and the direction of the encoder
tonnyleonard 15:8adff67fe707 347 count1=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 15:8adff67fe707 348 dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 16:e423f891cfbc 349 printf("P:%ldD:%sC:%d", count1, dir1==0 ? "+":"-", count2);
tonnyleonard 15:8adff67fe707 350 if (posIndex_en) {
tonnyleonard 15:8adff67fe707 351 // Does not work if speed computation is enabled and uses wait(),
tonnyleonard 16:e423f891cfbc 352 // because speed computation may take too long and wait()
tonnyleonard 15:8adff67fe707 353 // disables other interrupts, therefore the index pulse
tonnyleonard 16:e423f891cfbc 354 // interrupt as well.
tonnyleonard 15:8adff67fe707 355 if (count2 > 1) {
tonnyleonard 16:e423f891cfbc 356 printf("Pi:%ld", (count1-count4) + (count2-1)*8000 + count3);
tonnyleonard 15:8adff67fe707 357 } else if (count2 < -1) {
tonnyleonard 16:e423f891cfbc 358 printf("Pi:%ld", (count1-count4) + (count2+1)*8000 + count3);
tonnyleonard 15:8adff67fe707 359 } else {
tonnyleonard 16:e423f891cfbc 360 printf("Pi:%ld", (count1-count4) + count3);
tonnyleonard 15:8adff67fe707 361 }
tonnyleonard 15:8adff67fe707 362 }
tonnyleonard 15:8adff67fe707 363 }
tonnyleonard 6:5d4c09973041 364
tonnyleonard 15:8adff67fe707 365 if (speed_en) {
tonnyleonard 15:8adff67fe707 366 //Print speed
tonnyleonard 16:e423f891cfbc 367 printf("S:%.3f%", speedRead()); // in rot/s
tonnyleonard 15:8adff67fe707 368 }
tonnyleonard 14:e5f5b345b2fe 369
tonnyleonard 15:8adff67fe707 370 if (adc_en) {
tonnyleonard 16:e423f891cfbc 371 //Print phase shift in Degrees, DCPS output voltage in Volts
tonnyleonard 16:e423f891cfbc 372 // and current intensity in Ampers
tonnyleonard 16:e423f891cfbc 373 printf("F:%3.3fU:%3.3fI:%3.3f", (3.3f*ADC_read(adc1)+0.022f)*125.62f,
tonnyleonard 16:e423f891cfbc 374 (3.3f*ADC_read(adc2)+0.062f)/0.207f, 3.3f*ADC_read(adc3)+0.022f);
tonnyleonard 16:e423f891cfbc 375 //Print DCPS output voltage in Volts
tonnyleonard 16:e423f891cfbc 376 //printf("U:%3.3f%", (3.3f*ADC_read(adc2)+0.062f)/0.207f);
tonnyleonard 16:e423f891cfbc 377 //Print current intensity in Ampers
tonnyleonard 16:e423f891cfbc 378 //printf("I:%3.3f%\n", 3.3f*ADC_read(adc3)+0.022f);
tonnyleonard 10:8862c8779b71 379 }
tonnyleonard 16:e423f891cfbc 380 printf("\n");
tonnyleonard 10:8862c8779b71 381
tonnyleonard 16:e423f891cfbc 382 wait(0.08);
tonnyleonard 0:789510d98ade 383 }
tonnyleonard 8:28ad0ba5a673 384
tonnyleonard 8:28ad0ba5a673 385 }