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.

main.cpp

Committer:
tonnyleonard
Date:
2017-06-02
Revision:
12:8c355d78e081
Parent:
11:7a9837acbea0
Child:
13:e08e3540c30b

File content as of revision 12:8c355d78e081:

/*
 * Nucleo STM32F4(or L4) quadrature decoder, ADCs and DACs
 * 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.
 * Take into account that on F4xx 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.
 *
 * On L432KC, for example, only TIM2 has 32bit count, others have 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:
 * David Lowe (for the quadrature encoder code)
 * https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/
 *
 * And Frederic Blanc
 * https://developer.mbed.org/users/fblanc/code/AnalogIn_Diff/
 *
 * And Eric Lewiston /  STM32L4xx_HAL_Driver
 * https://developer.mbed.org/users/EricLew/code/STM32L4xx_HAL_Driver/docs/tip/group__ADC__LL__EF__Configuration__Channels.html
 *
 * References:
 * 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
 *
 * Tonny-Leonard Farauanu, 2017
 */

#include "mbed.h"
#include "Encoder.h"
#include "inttypes.h" // for PRIu32 encoding

//Defining the timer and its coresponding encoder
TIM_HandleTypeDef timer2;
TIM_Encoder_InitTypeDef encoder1;

//The input for the encoder's index channel
InterruptIn event(PA_8);

//LED1 to signal USB serial RX interrupt reading
DigitalOut led1(LED1);

//LED2 to signal the encoder's index impulses
//only for feedback, to calibrate the position
//of the AVAGO encoder with respect to the shaft
DigitalOut led2(PB_4);

//Relay to power on and off the DCPS
DigitalOut relay1(PB_5);

//AnalogIn    vref(ADC_VREF);
//AnalogIn   tempint(ADC_TEMP);

//Defining the ADCs
AnalogIn adc1(PA_3); //ADC1_IN8
AnalogIn adc2(PA_7); //ADC1_IN9
AnalogIn adc3(PA_6); //ADC1_IN11

//Defining the DAC for setting the half of the current amplitude
//and generate a quasi square signal for the PLL current phase input
AnalogOut dac1(PA_4); // DAC1_OUT1

//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 dac2(PA_5); // DAC1_OUT2

//Defining the serial object to be used for communicating with Raspi
Serial raspi(USBTX, USBRX);

//Serial& raspi = get_stdio_serial();
//Serial debug(PB_6,PA_10);    // Serial1 tx rx

uint16_t count1=0;
int16_t count2=0;
int32_t count3=0;
int16_t adjust=0;

float speedLast=0;

float dac_val=0;
float adc3_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;
Timer timer3;

//Function invoked by the encoder's index interrupt pulses
void atint()
{
    timer1.start();
    if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2)) {
        adjust--;
        if (adjust == -1) {
            count2--;
            count1 +=__HAL_TIM_GET_COUNTER(&timer2);
            //TIM2->CNT = 0x0000;
            adjust = 0;
            led2 =!led2;
        }
    } else {
        adjust++;
        if (adjust == 1) {
            count2++;
            count1 +=__HAL_TIM_GET_COUNTER(&timer2);
            //TIM2->CNT = 0x0000;
            adjust = 0;
            led2 =!led2;
        }
    }
}
float speedRead(){
    uint16_t i = 0;
    uint32_t deltaT;
    float speed;
    uint32_t pos1;
    uint32_t pos2;
    int32_t pos;
    int8_t sens1;
    int8_t sens2;
    printf("Encoder speed reading!\r\n");
    timer3.start();
    pos1=__HAL_TIM_GET_COUNTER(&timer2);
    sens1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
    wait_ms(5);
    pos2=__HAL_TIM_GET_COUNTER(&timer2);
    sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
    
    //The speed computation method adapts to slow/fast speeds, in order to
    //optimize the rapport between computation precision and time windows size
    while (pos2 == pos1 && i<99) { // The accepted max delay time is 0.5 seconds
        wait_ms(5);
        i++;
        pos2=__HAL_TIM_GET_COUNTER(&timer2);
        sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
        }
    pos2=__HAL_TIM_GET_COUNTER(&timer2);
    sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
    timer3.stop();
    deltaT = timer3.read_us();
    timer3.reset();
    pos = (int32_t) pos2 - (int32_t) pos1;
    
    printf("Time is %lu microseconds, position modified %ld %lu %lu\r\n", deltaT, pos, pos1, pos2);
    if (deltaT > 0){
        speed = ((float) pos)*125.f/((float) deltaT); // (pulses/us)*1000000/8000 -> rot/s
        }
    else {
        printf("Error, time interval not greater than zero, speed not calculated!\r\n");
        }
    printf("The encoder speed is %f rot/s\r\n", speed); 
    return speed;
    }

//Function attached to the serial RX interrupt event
void readData(void)
{
    char message[125];
    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 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);
            dac2.write(dac_val);
            printf("Value to write to DAC: %f\r\n", dac_val*3.3f);
        } else if (strcmp(message, "reset") == 0) {
            TIM2->CNT = 0x0000;
            printf("Encoder reset!\r\n");
        } else if (strcmp(message, "poweron") == 0) {
            relay1.write(1);
            printf("DCPS on\r\n");
        } else if (strcmp(message, "poweroff") == 0) {
            relay1.write(0);
            printf("DCPS off\r\n");
        }

    }
    led1 = 0;
}

//The main function
int main()
{
    //Power onn the DCPS
    relay1.write(1);
    
    //counting on both A&B inputs (A at PA0, B at PA1), 4 ticks per cycle,
    //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);
    
    //Set serial baud rate
    raspi.baud(115200);
    
    //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("Running at %u MHz\r\n\n", HAL_RCC_GetSysClockFreq()/1000000);

    //printf("%" PRIu32 "\n", UINT32_MAX);

    int i = 0;
    //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
        

        //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);

        if (i >= 100) {
            adc3_val = adc3.read();
            dac1.write(adc3_val);
            
            printf("%ld%s passes=%d\r\n", count3, dir1==0 ? "+":"-", count2);
            printf("Voltage ADC1= %3.3f%V, DAC=%f\r\n", adc1.read()*3.3f, dac2.read()*3.3f);
            //printf("Vref: %f\r\n", vref.read());
            printf("Voltage ADC2: %3.3f%V\r\n", adc2.read()*3.3f);

            //printf("Vref(f): %f, Vref : %u, Temperature : %u\r\n",
            //         vref.read(), vref.read_u16(), tempint.read_u16());
            if (adc3_en) {
                printf("Voltage ADC3: %3.3f%V\r\n", adc3_val*3.3f);
                printf("Voltage ADC3: %u\r\n", adc3.read_u16());
                printf("DAC1 read: %3.3f%V\r\n", dac1.read()*3.3f);
                printf("DAC2 read: %3.3f%V\r\n", dac2.read()*3.3f);
            }
            speedLast = speedRead();
            //printf("\033[%dA", lines); // Moves cursor up of #lines
            
            i=0;
        }
        i++;

        wait(0.04);
    }

}