SmartVehicle-Basis
SmartFactory
DriveCtrl Class Reference

Contains the FSM to controll the Drive. More...

#include <DriveCtrl.h>

Collaboration diagram for DriveCtrl:

Public Types

enum  Event {
  Event::TurnLeft, Event::TurnRight, Event::TurnAround, Event::FollowLineForward,
  Event::FollowLineBackward, Event::FullLineDetected, Event::LineAligned, Event::Error,
  Event::Resume, Event::Reset, Event::NoEvent
}
 Enum holds all possible events. More...
 
enum  State {
  State::idle, State::turningLeft, State::turningRight, State::turningAround,
  State::followingLineForward, State::followingLineBackward, State::resetState, State::errorState
}
 Enum holds all possible states. More...
 

Public Member Functions

 DriveCtrl ()
 Construct a new Drive Ctrl object and set the currentState with idle state and initialize the PID-Controller. More...
 
void loop ()
 Calls the do-function of the active state and hence generates Events. More...
 
void loop (Event currentEvent)
 procceses the current Event and calls the do-function of the active state More...
 
const State getcurrentState ()
 Get the current State. More...
 

Private Member Functions

void process (Event e)
 changes the state of the FSM based on the event More...
 
void entryAction_idle ()
 executes the entry action of the idle More...
 
DriveCtrl::Event doAction_idle ()
 executes the main action of the idle More...
 
void exitAction_idle ()
 executes the exit action of the idle More...
 
void entryAction_turningLeft ()
 executes the entry action of the turningLeft More...
 
DriveCtrl::Event doAction_turningLeft ()
 executes the main action of the turningLeft More...
 
void exitAction_turningLeft ()
 executes the exit action of the turningLeft More...
 
void entryAction_turningRight ()
 executes the entry action of the turningRight More...
 
DriveCtrl::Event doAction_turningRight ()
 executes the main action of the turningRight More...
 
void exitAction_turningRight ()
 executes the exit action of the turningRight More...
 
void entryAction_turningAround ()
 executes the entry action of the turningAround More...
 
DriveCtrl::Event doAction_turningAround ()
 executes the main action of the turningAround More...
 
void exitAction_turningAround ()
 executes the exit action of the turningAround More...
 
void entryAction_followingLineForward ()
 executes the entry action of the followingLineForward More...
 
DriveCtrl::Event doAction_followingLineForward ()
 executes the main action of the followingLineForward More...
 
void exitAction_followingLineForward ()
 executes the exit action of the followingLineForward More...
 
void entryAction_followingLineBackward ()
 executes the entry action of the followingLineBackward More...
 
DriveCtrl::Event doAction_followingLineBackward ()
 executes the main action of the followingLineBackward More...
 
void exitAction_followingLineBackward ()
 executes the exit action of the followingLineBackward More...
 
void entryAction_errorState ()
 entry action of the errorState More...
 
DriveCtrl::Event doAction_errorState ()
 main action of the errorState More...
 
void exitAction_errorState ()
 exit action of the errorState More...
 
void entryAction_resetState ()
 entry action of the resetState More...
 
DriveCtrl::Event doAction_resetState ()
 main action of the resetState More...
 
void exitAction_resetState ()
 exit action of the resetState More...
 
String decodeState (State state)
 Decodes the State-Enum and returns a description. More...
 
String decodeEvent (Event event)
 Decodes the Event-Enum and returns a description. More...
 

Private Attributes

State lastStateBevorError
 holds the last state of the FSM so it's possible to resume after error More...
 
State currentState
 holds the current state of the FSM More...
 
Event currentEvent
 holds the current event of the FSM More...
 
Event(DriveCtrl::* doActionFPtr )(void) = nullptr
 Functionpointer to call the current states do-function. More...
 
unsigned long currentMillis = 0
 will store current time More...
 
unsigned long previousMillis = 0
 will store last time More...
 
const unsigned long ignoreSensorTurnMillis = 500
 sets how long the sensors are ignored for a turn More...
 
const unsigned long ignoreSensorAroundMillis = 1500
 sets how long the sensors are ignored for a turning around More...
 
double pController_Input = 0
 Controller Input. More...
 
double pController_Output = 0
 Controller Output. More...
 
double pController_Setpoint = 0
 Controller Setpoint. More...
 
int pSampleTime = 50
 Controler SampleTime in ms. More...
 
double pVal_p = PID_KP
 P-Value. More...
 
double pVal_i = PID_KI / pSampleTime
 I-Value (independet of SamplingTime) More...
 
double pVal_d = PID_KD * pSampleTime
 D-Value (independet of SamplingTime) More...
 
PID pController = PID(&pController_Input, &pController_Output, &pController_Setpoint, pVal_p, pVal_i, pVal_d, DIRECT)
 PID-Controller Object. More...
 
Drive pDrive = Drive(RIGHT_MOTOR, LEFT_MOTOR)
 Drive Object. More...
 
EnvironmentDetection pEnvdetect
 EnviromentDetection Object. More...
 

Detailed Description

Contains the FSM to controll the Drive.

DriveCtrl.png

Definition at line 32 of file DriveCtrl.h.

Member Enumeration Documentation

◆ Event

enum DriveCtrl::Event
strong

Enum holds all possible events.

Enumerator
TurnLeft 

Ext.: Turn left.

TurnRight 

Ext.: Turn right.

TurnAround 

Ext.: Turn around on position.

FollowLineForward 

Ext.: Follow the line forwards.

FollowLineBackward 

Ext.: Follow the line backwards.

FullLineDetected 

Signal: Full line detected.

LineAligned 

Signal: Line is alligned in the middle of the vehicle.

Error 

Ext.: Error occured.

Resume 

Ext.: Resume after Error occured.

Reset 

Ext.: Reset after Error occured.

NoEvent 

No event generated.

Definition at line 39 of file DriveCtrl.h.

◆ State

enum DriveCtrl::State
strong

Enum holds all possible states.

Enumerator
idle 

idle state

turningLeft 

turning left state

turningRight 

turning right state

turningAround 

turning around state

followingLineForward 

follow the line while driving forward state

followingLineBackward 

follow the line while driving backward state

resetState 

Reset state.

errorState 

Error state.

Definition at line 56 of file DriveCtrl.h.

Constructor & Destructor Documentation

◆ DriveCtrl()

DriveCtrl::DriveCtrl ( )

Construct a new Drive Ctrl object and set the currentState with idle state and initialize the PID-Controller.

Definition at line 17 of file DriveCtrl.cpp.

Member Function Documentation

◆ loop() [1/2]

void DriveCtrl::loop ( )

Calls the do-function of the active state and hence generates Events.

Definition at line 26 of file DriveCtrl.cpp.

◆ loop() [2/2]

void DriveCtrl::loop ( Event  currentEvent)

procceses the current Event and calls the do-function of the active state

Parameters
currentEvent- Event

Definition at line 31 of file DriveCtrl.cpp.

◆ getcurrentState()

const DriveCtrl::State DriveCtrl::getcurrentState ( )

Get the current State.

Returns
State - current State

Definition at line 37 of file DriveCtrl.cpp.

◆ process()

void DriveCtrl::process ( Event  e)
private

changes the state of the FSM based on the event

Parameters
e- Event

Definition at line 41 of file DriveCtrl.cpp.

◆ entryAction_idle()

void DriveCtrl::entryAction_idle ( )
private

executes the entry action of the idle

Definition at line 156 of file DriveCtrl.cpp.

◆ doAction_idle()

DriveCtrl::Event DriveCtrl::doAction_idle ( )
private

executes the main action of the idle

This is an idle-state. Return NoEvent.

Returns
DriveCtrl::Event - generated Event

Definition at line 163 of file DriveCtrl.cpp.

◆ exitAction_idle()

void DriveCtrl::exitAction_idle ( )
private

executes the exit action of the idle

Definition at line 169 of file DriveCtrl.cpp.

◆ entryAction_turningLeft()

void DriveCtrl::entryAction_turningLeft ( )
private

executes the entry action of the turningLeft

Start Turning Left.

Definition at line 174 of file DriveCtrl.cpp.

◆ doAction_turningLeft()

DriveCtrl::Event DriveCtrl::doAction_turningLeft ( )
private

executes the main action of the turningLeft

Turn Left until the line is aligned again. Return LineAligned else return no Event.

Returns
DriveCtrl::Event - generated Event

Definition at line 184 of file DriveCtrl.cpp.

◆ exitAction_turningLeft()

void DriveCtrl::exitAction_turningLeft ( )
private

executes the exit action of the turningLeft

Stop Driving.

Definition at line 194 of file DriveCtrl.cpp.

◆ entryAction_turningRight()

void DriveCtrl::entryAction_turningRight ( )
private

executes the entry action of the turningRight

Start Turning Right.

Definition at line 200 of file DriveCtrl.cpp.

◆ doAction_turningRight()

DriveCtrl::Event DriveCtrl::doAction_turningRight ( )
private

executes the main action of the turningRight

Turn Right until the line is aligned again. Return LineAligned else return no Event.

Returns
DriveCtrl::Event - generated Event

Definition at line 210 of file DriveCtrl.cpp.

◆ exitAction_turningRight()

void DriveCtrl::exitAction_turningRight ( )
private

executes the exit action of the turningRight

Stop Driving.

Definition at line 220 of file DriveCtrl.cpp.

◆ entryAction_turningAround()

void DriveCtrl::entryAction_turningAround ( )
private

executes the entry action of the turningAround

Start Turning Right.

Definition at line 226 of file DriveCtrl.cpp.

◆ doAction_turningAround()

DriveCtrl::Event DriveCtrl::doAction_turningAround ( )
private

executes the main action of the turningAround

Turn Right until the line is aligned again. Return LineAligned else return no Event.

Returns
DriveCtrl::Event - generated Event

Definition at line 237 of file DriveCtrl.cpp.

◆ exitAction_turningAround()

void DriveCtrl::exitAction_turningAround ( )
private

executes the exit action of the turningAround

Stop Driving.

Definition at line 250 of file DriveCtrl.cpp.

◆ entryAction_followingLineForward()

void DriveCtrl::entryAction_followingLineForward ( )
private

executes the entry action of the followingLineForward

Start Driving Straight

Definition at line 256 of file DriveCtrl.cpp.

◆ doAction_followingLineForward()

DriveCtrl::Event DriveCtrl::doAction_followingLineForward ( )
private

executes the main action of the followingLineForward

Check Line-deviation and correct accordingly via PID-Controller and drive until a FullLine is Detected. Return Event FullLine else reutrn NoEvent.

Returns
DriveCtrl::Event - generated Event

Definition at line 267 of file DriveCtrl.cpp.

◆ exitAction_followingLineForward()

void DriveCtrl::exitAction_followingLineForward ( )
private

executes the exit action of the followingLineForward

Stop driving.

Definition at line 294 of file DriveCtrl.cpp.

◆ entryAction_followingLineBackward()

void DriveCtrl::entryAction_followingLineBackward ( )
private

executes the entry action of the followingLineBackward

Start Driving Straight

Definition at line 300 of file DriveCtrl.cpp.

◆ doAction_followingLineBackward()

DriveCtrl::Event DriveCtrl::doAction_followingLineBackward ( )
private

executes the main action of the followingLineBackward

Check Line-deviation and correct accordingly via PID-Controller and drive until a FullLine is Detected. Return Event FullLine else reutrn NoEvent.

Returns
DriveCtrl::Event - generated Event

Definition at line 314 of file DriveCtrl.cpp.

◆ exitAction_followingLineBackward()

void DriveCtrl::exitAction_followingLineBackward ( )
private

executes the exit action of the followingLineBackward

Stop driving.

Definition at line 343 of file DriveCtrl.cpp.

◆ entryAction_errorState()

void DriveCtrl::entryAction_errorState ( )
private

entry action of the errorState

Definition at line 351 of file DriveCtrl.cpp.

◆ doAction_errorState()

DriveCtrl::Event DriveCtrl::doAction_errorState ( )
private

main action of the errorState

Returns
DriveCtrl::Event - generated Event

Definition at line 360 of file DriveCtrl.cpp.

◆ exitAction_errorState()

void DriveCtrl::exitAction_errorState ( )
private

exit action of the errorState

Definition at line 366 of file DriveCtrl.cpp.

◆ entryAction_resetState()

void DriveCtrl::entryAction_resetState ( )
private

entry action of the resetState

Definition at line 371 of file DriveCtrl.cpp.

◆ doAction_resetState()

DriveCtrl::Event DriveCtrl::doAction_resetState ( )
private

main action of the resetState

Returns
DriveCtrl::Event - generated Event

Definition at line 377 of file DriveCtrl.cpp.

◆ exitAction_resetState()

void DriveCtrl::exitAction_resetState ( )
private

exit action of the resetState

Definition at line 383 of file DriveCtrl.cpp.

◆ decodeState()

String DriveCtrl::decodeState ( State  state)
private

Decodes the State-Enum and returns a description.

Parameters
state- enum State
Returns
String - State as String

Definition at line 389 of file DriveCtrl.cpp.

◆ decodeEvent()

String DriveCtrl::decodeEvent ( Event  event)
private

Decodes the Event-Enum and returns a description.

Parameters
event- enum Event
Returns
String - Event as String

Definition at line 414 of file DriveCtrl.cpp.

Member Data Documentation

◆ lastStateBevorError

State DriveCtrl::lastStateBevorError
private

holds the last state of the FSM so it's possible to resume after error

Definition at line 95 of file DriveCtrl.h.

◆ currentState

State DriveCtrl::currentState
private

holds the current state of the FSM

Definition at line 96 of file DriveCtrl.h.

◆ currentEvent

Event DriveCtrl::currentEvent
private

holds the current event of the FSM

Definition at line 97 of file DriveCtrl.h.

◆ doActionFPtr

Event(DriveCtrl::* DriveCtrl::doActionFPtr) (void) = nullptr
private

Functionpointer to call the current states do-function.

https://stackoverflow.com/questions/1485983/calling-c-class-methods-via-a-function-pointer

Definition at line 104 of file DriveCtrl.h.

◆ currentMillis

unsigned long DriveCtrl::currentMillis = 0
private

will store current time

Definition at line 106 of file DriveCtrl.h.

◆ previousMillis

unsigned long DriveCtrl::previousMillis = 0
private

will store last time

Definition at line 107 of file DriveCtrl.h.

◆ ignoreSensorTurnMillis

const unsigned long DriveCtrl::ignoreSensorTurnMillis = 500
private

sets how long the sensors are ignored for a turn

Definition at line 108 of file DriveCtrl.h.

◆ ignoreSensorAroundMillis

const unsigned long DriveCtrl::ignoreSensorAroundMillis = 1500
private

sets how long the sensors are ignored for a turning around

Definition at line 109 of file DriveCtrl.h.

◆ pController_Input

double DriveCtrl::pController_Input = 0
private

Controller Input.

Definition at line 111 of file DriveCtrl.h.

◆ pController_Output

double DriveCtrl::pController_Output = 0
private

Controller Output.

Definition at line 112 of file DriveCtrl.h.

◆ pController_Setpoint

double DriveCtrl::pController_Setpoint = 0
private

Controller Setpoint.

Definition at line 113 of file DriveCtrl.h.

◆ pSampleTime

int DriveCtrl::pSampleTime = 50
private

Controler SampleTime in ms.

Definition at line 114 of file DriveCtrl.h.

◆ pVal_p

double DriveCtrl::pVal_p = PID_KP
private

P-Value.

Definition at line 115 of file DriveCtrl.h.

◆ pVal_i

double DriveCtrl::pVal_i = PID_KI / pSampleTime
private

I-Value (independet of SamplingTime)

Definition at line 116 of file DriveCtrl.h.

◆ pVal_d

double DriveCtrl::pVal_d = PID_KD * pSampleTime
private

D-Value (independet of SamplingTime)

Definition at line 117 of file DriveCtrl.h.

◆ pController

PID DriveCtrl::pController = PID(&pController_Input, &pController_Output, &pController_Setpoint, pVal_p, pVal_i, pVal_d, DIRECT)
private

PID-Controller Object.

https://www.quora.com/What-would-be-appropriate-tuning-factors-for-PID-line-follower-robot

  1. Set all the gains to zero
  2. Increase only Kp (Proportional gain) to get an ultimate oscillating value Kp(max)
  3. Increase Kd (Derivative gain) until the oscillations disappear.
  4. Repeat steps 2 and 3 until increasing Kd does not dampen the oscillations
  5. Now increase Ki (Integral gain) to get a good system with desired number of oscillations (Ideally zero)
Todo:
Improve controller and make it adaptive to speedchanges

Definition at line 131 of file DriveCtrl.h.

◆ pDrive

Drive DriveCtrl::pDrive = Drive(RIGHT_MOTOR, LEFT_MOTOR)
private

Drive Object.

Definition at line 132 of file DriveCtrl.h.

◆ pEnvdetect

EnvironmentDetection DriveCtrl::pEnvdetect
private

EnviromentDetection Object.

Definition at line 133 of file DriveCtrl.h.


The documentation for this class was generated from the following files: