SmartVehicle-Basis
SmartFactory
DriveCtrl.h
Go to the documentation of this file.
1 
16 #ifndef DRIVECTRL_H__
17 #define DRIVECTRL_H__
18 
19 #include "HardwareConfiguration.h"
20 #include "LogConfiguration.h"
21 
22 #include <PID_v1.h>
23 
24 #include "Drive/Drive.h"
25 #include "EnvironmentDetection.h"
26 
32 class DriveCtrl {
33  //=====PUBLIC====================================================================================
34  public:
39  enum class Event { TurnLeft,
40  TurnRight,
41  TurnAround,
45  LineAligned,
46  Error,
47  Resume,
48  Reset,
49  NoEvent
50  };
51 
56  enum class State { idle,
57  turningLeft,
58  turningRight,
62  resetState,
63  errorState
64  };
65 
71  DriveCtrl();
72 
77  void loop();
78 
84  void loop(Event currentEvent);
85 
91  const State getcurrentState();
92 
93  //=====PRIVATE====================================================================================
94  private:
98 
104  Event (DriveCtrl::*doActionFPtr)(void) = nullptr;
105 
106  unsigned long currentMillis = 0;
107  unsigned long previousMillis = 0;
108  const unsigned long ignoreSensorTurnMillis = 500;
109  const unsigned long ignoreSensorAroundMillis = 1500;
110 
111  double pController_Input = 0;
112  double pController_Output = 0;
113  double pController_Setpoint = 0;
114  int pSampleTime = 50;
115  double pVal_p = PID_KP;
116  double pVal_i = PID_KI / pSampleTime;
117  double pVal_d = PID_KD * pSampleTime;
118 
134 
135  //=====PrivateFunctions=========================================================================
141  void process(Event e);
142 
143  //=====StateFunctions=====
144  //=====idle==========================================================
149  void entryAction_idle();
150 
160 
165  void exitAction_idle();
166 
167  //=====turningLeft==========================================================
175 
185 
191  void exitAction_turningLeft();
192 
193  //=====turningRight==========================================================
200 
210 
217 
218  //=====turningAround==========================================================
225 
235 
242 
243  //=====followingLineForward==========================================================
250 
260 
267 
268  //=====followingLineBackward==========================================================
275 
285 
292  //==errorState==========================================================
297  void entryAction_errorState();
298 
305 
310  void exitAction_errorState();
311 
312  //==resetState==========================================================
317  void entryAction_resetState();
318 
325 
330  void exitAction_resetState();
331  //============================================================================
332  //==Aux-Function==============================================================
339  String decodeState(State state);
340 
347  String decodeEvent(Event event);
348 };
349 #endif
const unsigned int LEFT_MOTOR
Portnumber of the left motor on the motorshield.
void exitAction_followingLineForward()
executes the exit action of the followingLineForward
Definition: DriveCtrl.cpp:294
Contains the FSM to controll the Drive.
Definition: DriveCtrl.h:32
const double PID_KI
I-value of the PID-controller.
follow the line while driving forward state
const State getcurrentState()
Get the current State.
Definition: DriveCtrl.cpp:37
Class EnvironmentDetection provides an Interface for all Enviroment detection Systems of the Vehilce.
DriveCtrl::Event doAction_errorState()
main action of the errorState
Definition: DriveCtrl.cpp:360
void entryAction_idle()
executes the entry action of the idle
Definition: DriveCtrl.cpp:156
File that contains the configuratioindetails of the different modules.
DriveCtrl::Event doAction_turningAround()
executes the main action of the turningAround
Definition: DriveCtrl.cpp:237
void entryAction_turningLeft()
executes the entry action of the turningLeft
Definition: DriveCtrl.cpp:174
Ext.: Error occured.
unsigned long currentMillis
will store current time
Definition: DriveCtrl.h:106
Ext.: Reset after Error occured.
Ext.: Follow the line backwards.
PID pController
PID-Controller Object.
Definition: DriveCtrl.h:131
No event generated.
double pController_Output
Controller Output.
Definition: DriveCtrl.h:112
Drive pDrive
Drive Object.
Definition: DriveCtrl.h:132
EnvironmentDetection pEnvdetect
EnviromentDetection Object.
Definition: DriveCtrl.h:133
const unsigned long ignoreSensorTurnMillis
sets how long the sensors are ignored for a turn
Definition: DriveCtrl.h:108
void exitAction_turningLeft()
executes the exit action of the turningLeft
Definition: DriveCtrl.cpp:194
Event currentEvent
holds the current event of the FSM
Definition: DriveCtrl.h:97
Provides an Interface for all Enviroment detection Systems of the Vehilce.
const double PID_KD
D-value of the PID-controller.
double pVal_d
D-Value (independet of SamplingTime)
Definition: DriveCtrl.h:117
State lastStateBevorError
holds the last state of the FSM so it's possible to resume after error
Definition: DriveCtrl.h:95
Class Drive handels the Driving-System and provides basic functions.
Definition: Drive.h:42
Ext.: Follow the line forwards.
void exitAction_idle()
executes the exit action of the idle
Definition: DriveCtrl.cpp:169
State currentState
holds the current state of the FSM
Definition: DriveCtrl.h:96
void process(Event e)
changes the state of the FSM based on the event
Definition: DriveCtrl.cpp:41
DriveCtrl::Event doAction_turningRight()
executes the main action of the turningRight
Definition: DriveCtrl.cpp:210
void exitAction_resetState()
exit action of the resetState
Definition: DriveCtrl.cpp:383
Event(DriveCtrl::* doActionFPtr)(void)
Functionpointer to call the current states do-function.
Definition: DriveCtrl.h:104
DriveCtrl::Event doAction_followingLineBackward()
executes the main action of the followingLineBackward
Definition: DriveCtrl.cpp:314
void entryAction_turningRight()
executes the entry action of the turningRight
Definition: DriveCtrl.cpp:200
void exitAction_turningAround()
executes the exit action of the turningAround
Definition: DriveCtrl.cpp:250
DriveCtrl::Event doAction_turningLeft()
executes the main action of the turningLeft
Definition: DriveCtrl.cpp:184
follow the line while driving backward state
Signal: Full line detected.
const unsigned int RIGHT_MOTOR
Portnumber of the right motor on the motorshield.
String decodeState(State state)
Decodes the State-Enum and returns a description.
Definition: DriveCtrl.cpp:389
String decodeEvent(Event event)
Decodes the Event-Enum and returns a description.
Definition: DriveCtrl.cpp:414
Signal: Line is alligned in the middle of the vehicle.
DriveCtrl::Event doAction_idle()
executes the main action of the idle
Definition: DriveCtrl.cpp:163
double pVal_i
I-Value (independet of SamplingTime)
Definition: DriveCtrl.h:116
State
Enum holds all possible states.
Definition: DriveCtrl.h:56
double pVal_p
P-Value.
Definition: DriveCtrl.h:115
int pSampleTime
Controler SampleTime in ms.
Definition: DriveCtrl.h:114
void entryAction_errorState()
entry action of the errorState
Definition: DriveCtrl.cpp:351
void entryAction_followingLineForward()
executes the entry action of the followingLineForward
Definition: DriveCtrl.cpp:256
The Drive Class handles the activation of the dc-motors for the drive.
double pController_Input
Controller Input.
Definition: DriveCtrl.h:111
const unsigned long ignoreSensorAroundMillis
sets how long the sensors are ignored for a turning around
Definition: DriveCtrl.h:109
DriveCtrl::Event doAction_followingLineForward()
executes the main action of the followingLineForward
Definition: DriveCtrl.cpp:267
void exitAction_followingLineBackward()
executes the exit action of the followingLineBackward
Definition: DriveCtrl.cpp:343
Event
Enum holds all possible events.
Definition: DriveCtrl.h:39
void entryAction_followingLineBackward()
executes the entry action of the followingLineBackward
Definition: DriveCtrl.cpp:300
DriveCtrl::Event doAction_resetState()
main action of the resetState
Definition: DriveCtrl.cpp:377
Ext.: Turn around on position.
void loop()
Calls the do-function of the active state and hence generates Events.
Definition: DriveCtrl.cpp:26
void exitAction_turningRight()
executes the exit action of the turningRight
Definition: DriveCtrl.cpp:220
void entryAction_resetState()
entry action of the resetState
Definition: DriveCtrl.cpp:371
void exitAction_errorState()
exit action of the errorState
Definition: DriveCtrl.cpp:366
const double PID_KP
P-value of the PID-controller.
Ext.: Resume after Error occured.
double pController_Setpoint
Controller Setpoint.
Definition: DriveCtrl.h:113
unsigned long previousMillis
will store last time
Definition: DriveCtrl.h:107
DriveCtrl()
Construct a new Drive Ctrl object and set the currentState with idle state and initialize the PID-Con...
Definition: DriveCtrl.cpp:17
void entryAction_turningAround()
executes the entry action of the turningAround
Definition: DriveCtrl.cpp:226