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:
- 8:28ad0ba5a673
- Parent:
- 7:7f59b69d8895
- Child:
- 9:4d736d29ce19
--- a/main.cpp Thu May 18 00:08:20 2017 +0000 +++ b/main.cpp Fri May 19 23:01:01 2017 +0000 @@ -1,53 +1,121 @@ /* + * Nucleo STM32F4(or L4) quadrature decoder, ADCs and DAC + * with serial communication over the USB virtual serial port + + * Developed for Elliptec X15 piezoelectric motor control, on a L432KC board + * * Using STM32's counter peripherals to interface rotary encoders. - * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count, others 16bit. - * Beware mbed uses TIM5 for system timer, SPI needs TIM1, others used for PWM. - * Check your platform's PeripheralPins.c & PeripheralNames.h if you need both PWM & encoders. + * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count, + * others 16bit. + * Take into account that Mbed uses TIM5 for system timer, SPI needs TIM1, + * others are used for PWM. + * Check your platform's PeripheralPins.c & PeripheralNames.h if you need + * both PWM & encoders. This project does not use PWM. * - * Edit HAL_TIM_Encoder_MspInitFx.cpp to suit your mcu & board's available pinouts & pullups/downs. + * On L432KC, for example, only TIM2 has 32bit count, others 16bit. + * However, mbed has TIM2 assigned by default to the system ticker + * For L432KC to work with TIM2 encoder input one needs to reassign + * the system ticker from TIM2 to TIM7, for example, + * in mbed-dev/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/device/hal_tick.h + * + * Edit HAL_TIM_Encoder_MspInitFx(Lx).cpp to suit your mcu & board's available + * pinouts & pullups/downs. * * Thanks to: - * http://petoknm.wordpress.com/2015/01/05/rotary-encoder-and-stm32/ + * David Lowe (for the quadrature encoder code) + * https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/ * * References: - * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00122015.pdf - * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/reference_manual/DM00096844.pdf + * http://www.st.com/resource/en/datasheet/stm32l432kc.pdf + * https://developer.mbed.org/platforms/ST-Nucleo-L432KC/ * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf * - * David Lowe Jan 2015 + * Tonny-Leonard Farauanu, 2017 */ #include "mbed.h" #include "Encoder.h" -#include "inttypes.h" -#include <string.h> // strstr() -#include <stdbool.h> // bool, true, false +#include "inttypes.h" // for PRIu32 encoding +//#include <string.h> // strstr() +//#include <stdbool.h> // bool, true, false +#define DAC_RANGE (0xFFF) // 12 bits +#define SAMPLE_RATE 150000 + +//Defining the timer and its coresponding encoder +TIM_HandleTypeDef timer2; TIM_Encoder_InitTypeDef encoder1; -TIM_HandleTypeDef timer2; + +//The input for the encoder's index channel InterruptIn event(PA_8); -DigitalOut led(LED1); + +//LED1 to signal USB serial RX interrupt reading +DigitalOut led1(LED1); + +//LED2 to signal the encoder's index impulses +//only for debugging, to calibrate the position +//of the AVAGO encoder with respect to the shaft +DigitalOut led2(PB_4); + +AnalogIn vref(ADC_VREF); +AnalogIn tempint(ADC_TEMP); + +//Defining the ADCs AnalogIn adc1(PA_3); //ADC1_IN8 -AnalogIn adc2(PA_5); //ADC1_IN10 +AnalogIn adc2(PA_4); //ADC1_IN9 AnalogIn adc3(PA_6); //ADC1_IN11 -AnalogOut dac1(PA_4); // DAC1_OUT1 -//Serial pc(SERIAL_TX, SERIAL_RX); -Serial pc(USBTX, USBRX); -//Serial& pc = get_stdio_serial(); + +//Defining the DAC for the Power Source +//(DCPS - Digitally Controlled Power Source) +//DAC1_OUT2 on pin PA_5 is at least twices as fast as DAC1_OUT1 on pin PA_4 +AnalogOut dac1(PA_5); // DAC1_OUT2 + +//Defining the serial object to be used for communicating with Raspi +Serial raspi(USBTX, USBRX); +//Serial raspi(SERIAL_TX, SERIAL_RX); +//Serial& raspi = get_stdio_serial(); //Serial debug(PB_6,PA_10); // Serial1 tx rx //Serial configBT(PA_2,PA_3); // Serial2 tx rx //Serial slave(PC_10,PC_11); // Serial3 tx rx //Serial modem(PA_0,PA_1); // Serial4 tx rx -uint32_t count1=0; +uint16_t count1=0; int16_t count2=0; -int16_t count3=0; +int32_t count3=0; int16_t adjust=0; + +float dac_val=0; + volatile bool adc3_en = false; +int16_t lines = 4; + +//TIM1 to be used with the interrupt for the encoder's index pulses Timer timer1; +//Function to convert a string to float +float stof(const char* s){ + float rez = 0, fact = 1; + if (*s == '-'){ + s++; + fact = -1; + }; + for (int point_seen = 0; *s; s++){ + if (*s == '.'){ + point_seen = 1; + continue; + }; + int d = *s - '0'; + if (d >= 0 && d <= 9){ + if (point_seen) fact /= 10.0f; + rez = rez * 10.0f + (float)d; + }; + }; + return rez * fact; +}; + +//Function invoked by the encoder's index interrupt pulses void atint() { timer1.start(); @@ -58,7 +126,7 @@ count1 +=__HAL_TIM_GET_COUNTER(&timer2); TIM2->CNT = 0x0000; adjust = 0; - led =!led; + led2 =!led2; } } else { adjust++; @@ -67,27 +135,38 @@ count1 +=__HAL_TIM_GET_COUNTER(&timer2); TIM2->CNT = 0x0000; adjust = 0; - led =!led; + led2 =!led2; } } } +//Function attached to the serial RX interrupt event void readData(void) { char message[125]; - if(pc.readable()) { - pc.scanf("%s", message); + if(raspi.readable()) { + int p1=0; + led1 = 1; + raspi.scanf("%s", message); printf("%d %s\r\n", strlen(message), message); if (strcmp(message, "startAdc") == 0) { adc3_en = true; + lines = 6; printf("true\r\n"); - } else { + } else if (strcmp(message, "stopAdc") == 0) { adc3_en = false; + lines = 4; printf("false\r\n"); - } + } else if (p1=strstr(message, "DAC=") != NULL){ + dac_val = atof(message+p1+3); + printf("Value to write to DAC: %f\r\n", dac_val*3.3f); + } + + } + led1 = 0; +} - } -} +//The main function int main() { @@ -95,37 +174,72 @@ //full 32-bit count //For L432KC to work with TIM2 one needs to reassign the system ticker //from TIM2 to TIM7 in TARGET/../device/hal_tick.h + + //Initialise encoder EncoderInit(&encoder1, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12); + + //Define function to call for interrupt event rise + //This is triggered by the encoder's index pulses + //and it resets the encoder counter event.rise(&atint); - pc.attach(&readData); + + //Attach functin to call for serial interrupt event + raspi.attach(&readData); + //Message to mark the initialisation of the program printf("\n\rSTM HAL encoder with ADC and DAC\n\r"); //printf("%" PRIu32 "\n", UINT32_MAX); + + //The main loop while(1) { + + //Variable for the one loop encoder counter uint32_t count3=0; + //Variable for the direction of the counter int8_t dir1; + + uint16_t voltage1=0; //OK 401 411 446 NOK 030 //count1=TIM2->CNT; //dir1=TIM2->CR1&TIM_CR1_DIR; + + //It reads the ADC1 value converted from 12bit to 16bit resolution + voltage1=adc1.read_u16(); + + //It resets the DAC1 + //dac1.free(); + + //It writes the DAC1 value as a 16bit number + //dac1.write_u16(count3*2); + + //It writes the DAC1 value as a subunitary float number + //to be multiplied with the max DAC_RANGE + dac1.write(dac_val); + + //It gets the one loop position and the direction of the encoder count3=__HAL_TIM_GET_COUNTER(&timer2); dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2); - voltage1=adc1.read_u16(); - dac1.write_u16(count3*2); - + printf("%ld%s passes=%d short=%ld\r\n", count1+count3, dir1==0 ? "+":"-", count2, count3); - printf("Voltage= %d, DAC=%f\r\n", voltage1, dac1.read()*3.3); + printf("Voltage ADC1= %u, DAC=%f\r\n", voltage1, dac1.read()*3.3); + printf("Vref: %f\r\n", (3.3f)*vref.read()); printf("percentage: %3.3f%%\r\n", adc2.read()*100.0f); + + //printf("Vref(f): %f, Vref : %u, Temperature : %u\r\n", + // vref.read(), vref.read_u16(), tempint.read_u16()); if (adc3_en) { - printf("percentage: %3.3f%V\r\n", adc3.read()*3.3f); - } - + printf("Voltage ADC3: %3.3f%V\r\n", adc3.read()*3.3f); + printf("Difference: %f\r\n", dac1.read()*3.3 - adc3.read()*3.3f); + } + printf("\033[%dA", lines); // Moves cursor up of #lines - wait(2.1); + wait(0.04); } -} + +} \ No newline at end of file