Skip to content

Commit b83f102

Browse files
committed
Introduced State Machine and startup state
1 parent b62887d commit b83f102

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed

lib/ConvoyLeader/src/App.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <ArduinoJson.h>
4242
#include <WiFi.h>
4343
#include <Util.h>
44+
#include "StartupState.h"
4445

4546
/******************************************************************************
4647
* Compiler Switches
@@ -169,14 +170,19 @@ void App::setup()
169170
}
170171
else
171172
{
173+
/* Setup longitudinal controller. */
172174
LongitudinalController::MotorSetpointCallback motorSetpointCallback = [this](const int16_t& topCenterSpeed)
173175
{ return this->motorSetpointCallback(topCenterSpeed); };
174176

175177
m_longitudinalController.setMotorSetpointCallback(motorSetpointCallback);
176178

179+
/* Initialize timers. */
177180
m_sendWaypointTimer.start(SEND_WAYPOINT_TIMER_INTERVAL);
178181
m_initialCommandTimer.start(SEND_WAYPOINT_TIMER_INTERVAL);
179182

183+
/* Start with startup state. */
184+
m_systemStateMachine.setState(&StartupState::getInstance());
185+
180186
isSuccessful = true;
181187
}
182188
}
@@ -216,6 +222,10 @@ void App::loop()
216222
/* Process Longitudinal Controller */
217223
m_longitudinalController.process();
218224

225+
/* Process System State Machine */
226+
m_systemStateMachine.process();
227+
228+
/* Process periodic tasks. */
219229
if (true == m_initialCommandTimer.isTimeout())
220230
{
221231
if (false == m_receivedMaxMotorSpeed)

lib/ConvoyLeader/src/App.h

+3
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ class App
187187
*/
188188
V2VClient m_v2vClient;
189189

190+
/** The system state machine. */
191+
StateMachine m_systemStateMachine;
192+
190193
/**
191194
* Flag for setting initial position through SMP.
192195
*/

lib/ConvoyLeader/src/StartupState.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* MIT License
2+
*
3+
* Copyright (c) 2024 Andreas Merkle <web@blue-andi.de>
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
/*******************************************************************************
25+
DESCRIPTION
26+
*******************************************************************************/
27+
/**
28+
* @brief Startup State.
29+
* @author Gabryel Reyes <gabryelrdiaz@gmail.com>
30+
*/
31+
32+
/******************************************************************************
33+
* Includes
34+
*****************************************************************************/
35+
36+
#include "StartupState.h"
37+
38+
/******************************************************************************
39+
* Compiler Switches
40+
*****************************************************************************/
41+
42+
/******************************************************************************
43+
* Macros
44+
*****************************************************************************/
45+
46+
/******************************************************************************
47+
* Types and classes
48+
*****************************************************************************/
49+
50+
/******************************************************************************
51+
* Prototypes
52+
*****************************************************************************/
53+
54+
/******************************************************************************
55+
* Local Variables
56+
*****************************************************************************/
57+
58+
/******************************************************************************
59+
* Public Methods
60+
*****************************************************************************/
61+
62+
void StartupState::entry()
63+
{
64+
/* Nothing to do */
65+
}
66+
67+
void StartupState::process(StateMachine& sm)
68+
{
69+
/* Nothing to do */
70+
}
71+
72+
void StartupState::exit()
73+
{
74+
/* Nothing to do */
75+
}
76+
77+
/******************************************************************************
78+
* Protected Methods
79+
*****************************************************************************/
80+
81+
/******************************************************************************
82+
* Private Methods
83+
*****************************************************************************/
84+
85+
/******************************************************************************
86+
* External Functions
87+
*****************************************************************************/
88+
89+
/******************************************************************************
90+
* Local Functions
91+
*****************************************************************************/

lib/ConvoyLeader/src/StartupState.h

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* MIT License
2+
*
3+
* Copyright (c) 2024 Andreas Merkle <web@blue-andi.de>
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
/*******************************************************************************
25+
DESCRIPTION
26+
*******************************************************************************/
27+
/**
28+
* @brief Startup State.
29+
* @author Gabryel Reyes <gabryelrdiaz@gmail.com>
30+
*
31+
* @addtogroup Application
32+
*
33+
* @{
34+
*/
35+
#ifndef STARTUP_STATE_H
36+
#define STARTUP_STATE_H
37+
38+
/******************************************************************************
39+
* Compile Switches
40+
*****************************************************************************/
41+
42+
/******************************************************************************
43+
* Includes
44+
*****************************************************************************/
45+
46+
#include <IState.h>
47+
48+
/******************************************************************************
49+
* Macros
50+
*****************************************************************************/
51+
52+
/******************************************************************************
53+
* Types and Classes
54+
*****************************************************************************/
55+
56+
/** The system startup state. */
57+
class StartupState : public IState
58+
{
59+
public:
60+
/**
61+
* Get state instance.
62+
*
63+
* @return State instance.
64+
*/
65+
static StartupState& getInstance()
66+
{
67+
static StartupState instance;
68+
69+
/* Singleton idiom to force initialization during first usage. */
70+
71+
return instance;
72+
}
73+
74+
/**
75+
* If the state is entered, this method will called once.
76+
*/
77+
void entry() final;
78+
79+
/**
80+
* Processing the state.
81+
*
82+
* @param[in] sm State machine, which is calling this state.
83+
*/
84+
void process(StateMachine& sm) final;
85+
86+
/**
87+
* If the state is left, this method will be called once.
88+
*/
89+
void exit() final;
90+
91+
protected:
92+
private:
93+
/**
94+
* Default constructor.
95+
*/
96+
StartupState()
97+
{
98+
}
99+
100+
/**
101+
* Default destructor.
102+
*/
103+
virtual ~StartupState()
104+
{
105+
}
106+
107+
/* Not allowed. */
108+
StartupState(const StartupState& state); /**< Copy construction of an instance. */
109+
StartupState& operator=(const StartupState& state); /**< Assignment of an instance. */
110+
};
111+
112+
/******************************************************************************
113+
* Functions
114+
*****************************************************************************/
115+
116+
#endif /* STARTUP_STATE_H */
117+
/** @} */

0 commit comments

Comments
 (0)