pr7

Dependencies:   mbed

main.cpp

Committer:
pmic
Date:
2019-05-10
Revision:
1:92f175969d90
Parent:
0:05dd1de8cc3f
Child:
2:1ded9d10f322

File content as of revision 1:92f175969d90:

#include "mbed.h"
#include "EncoderCounter.h"
#include "PID_Cntrl.h"
#include "LinearCharacteristics.h"
//------------------------------------------
#define PI 3.1415927f
//------------------------------------------

/* GRT: Control of voice-coil with PID-T1 - Controller

    Eigther reseting the Nucleo via the black button or save a new software on 
    the Nucleo sets the analog output to zero. Zero is equal to -4 Ampere!!!
    Therefor: NEVER !!! reset or save a new software while the VC is powered on
    (the green button on the VC is glowing green)

 */
Serial pc(SERIAL_TX, SERIAL_RX);        // serial connection via USB - programmer
InterruptIn button(USER_BUTTON);        // User Button, short presses: reduce speed, long presses: increase speed
AnalogOut out(PA_5);                    // Analog OUT on PA_5   1.6 V -> 0A 3.2A -> 4A (see ESCON)

bool key_was_pressed = false;
bool controller_active = false;
void pressed(void);                     // user Button pressed
void released(void);                    // user Button released

//------------------------------------------
// ... here define variables like gains etc.
//------------------------------------------
LinearCharacteristics i2u(-4.0f, 4.0f, 0.0f, 3.2f/3.3f);         // output is normalized output
//------------------------------------------
Ticker  ControllerLoopTimer;            // interrupt for control loop
EncoderCounter counter1(PB_6, PB_7);    // initialize counter on PB_6 and PB_7
Timer t_but;                            // define timer for button
// ----- User defined functions -----------
void updateLoop(void);   // loop for State machine (via interrupt)
float Ts = 0.0005f;                     // sample time of main loop
uint16_t k = 0;
// initiate PIDT1 controller objects
// ???
// ???
//******************************************************************************
//---------- main loop -------------
//******************************************************************************
int main()
{
    pc.baud(115200);   // for serial comm.
    counter1.reset();   // encoder reset
    out.write(i2u(0.0));
    button.fall(&pressed);          // attach key pressed function
    button.rise(&released);         // attach key pressed function
    pc.printf("Start controller now or reset...\r\n");
    ControllerLoopTimer.attach(&updateLoop, Ts); //Assume Fs = ...;
    while(1);
}   // END OF main
//******************************************************************************
//---------- main loop (called via interrupt) -------------
//******************************************************************************
void updateLoop(void)
{
    float x = (float)(counter1)/1000.0f;  // get counts from Encoder
    float i_des = 0.0f;         // default: set motor current to zero (will be overwritten)
    if(controller_active) {
        // controller update
        // ???
    }
    out.write(i2u(i_des));
    if(++k>1000) {
        pc.printf("x: %1.3f, i: %1.4f\r\n",x,i_des);
        k = 0;

    }
} // END OF updateLoop(void)

//******************************************************************************


// start timer as soon as Button is pressed
void pressed()
{
    t_but.start();
}
// Falling edge of button: enable/disable controller
void released()
{
    // readout, stop and reset timer
    float ButtonTime = t_but.read();
    t_but.stop();
    t_but.reset();
    if(ButtonTime > 0.05f) {
        controller_active = !controller_active;
        if(controller_active) {
            pc.printf("Controller actived\r\n");
            // reset controller here!!!
            // ???
        } else
            pc.printf("Controller disabled\r\n");
    }
}