mbed2 pre-final

Dependencies:   LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI

servo.cpp

Committer:
domino5740
Date:
2020-06-15
Revision:
4:a8494b656292
Parent:
3:404526e83702

File content as of revision 4:a8494b656292:

#include "servo.h"

Servo::Servo(void) {
    
    Callib();
    
}

void Servo::GoTo(unsigned int uiPosition) {
    uiDesiredPosition = uiPosition % 360;
    eState = IN_PROGRESS;
    while(eState != IDLE) {
        Automat();
    }
}

void Servo::Callib(void) {
    
    MyServoGui.uiMarkerStartPos = 0;
    MyServoGui.uiMarkerPos = 0;
    MyServoGui.ucLedPos = 0;
    
    eState = CALLIB;
    while(eState != IDLE) {
        Automat();
    }
    
    MyServoGui.uiMarkerStartPos = MyServoGui.uiMarkerPos;
    MyServoGui.uiMarkerPos = 0;
    MyServoGui.ucLedPos = 0;
    uiCurrentPosition = 0;
    uiDesiredPosition = 0;
}

void Servo::Automat(void) {
    wait(0.02);
    switch(eState) {
        case IN_PROGRESS:
            if(uiCurrentPosition < uiDesiredPosition) {
                MyServoGui.LedStepRight();
                eState = IN_PROGRESS;
            }
            else if(uiCurrentPosition > uiDesiredPosition){
                MyServoGui.LedStepLeft();
                eState = IN_PROGRESS;
            }
            else {
                eState = IDLE;
            }
        break;
        case IDLE:
            if (uiCurrentPosition != uiDesiredPosition) {
                eState = IN_PROGRESS;
            }
            else {
                eState = IDLE;
            }
        break;
        case CALLIB:
            if(MyServoGui.eReadDetector() == ACTIVE) {
                eState = IDLE;
            }
            else {
                MyServoGui.LedStepLeft();
                eState = CALLIB;
            }
        break;
    }
    uiCurrentPosition = MyServoGui.uiMarkerPos;
    wait(0.02);
}