Skip to content

Commit e5b4fe7

Browse files
committed
Added Startup State
1 parent b83f102 commit e5b4fe7

File tree

4 files changed

+109
-59
lines changed

4 files changed

+109
-59
lines changed

lib/ConvoyLeader/src/App.cpp

+9-28
Original file line numberDiff line numberDiff line change
@@ -226,39 +226,20 @@ void App::loop()
226226
m_systemStateMachine.process();
227227

228228
/* Process periodic tasks. */
229-
if (true == m_initialCommandTimer.isTimeout())
229+
if ((true == m_initialCommandTimer.isTimeout()) && (true == m_smpServer.isSynced()))
230230
{
231-
if (false == m_receivedMaxMotorSpeed)
232-
{
233-
Command payload;
234-
payload.commandId = RemoteControl::CMD_ID_GET_MAX_SPEED;
235-
236-
(void)m_smpServer.sendData(m_serialMuxProtChannelIdRemoteCtrl, &payload, sizeof(payload));
237-
}
231+
Command* pendingCommand = StartupState::getInstance().getPendingCommand();
238232

239-
if (false == m_initialPositionSent)
233+
if (nullptr != pendingCommand)
240234
{
241-
SettingsHandler& settings = SettingsHandler::getInstance();
242-
Command initialVehicleDataCmd;
243-
initialVehicleDataCmd.commandId = RemoteControl::CMD_ID_SET_INIT_POS;
244-
initialVehicleDataCmd.xPos = settings.getInitialXPosition();
245-
initialVehicleDataCmd.yPos = settings.getInitialYPosition();
246-
initialVehicleDataCmd.orientation = settings.getInitialHeading();
247-
248-
(void)m_smpServer.sendData(m_serialMuxProtChannelIdRemoteCtrl, &initialVehicleDataCmd,
249-
sizeof(initialVehicleDataCmd));
250-
}
235+
if (false == m_smpServer.sendData(m_serialMuxProtChannelIdRemoteCtrl, pendingCommand, sizeof(Command)))
236+
{
237+
LOG_WARNING("Failed to send pending command to RU.");
238+
}
251239

252-
if ((false == m_receivedMaxMotorSpeed) || (false == m_initialPositionSent))
253-
{
254240
/* Something is pending. */
255241
m_initialCommandTimer.restart();
256242
}
257-
else
258-
{
259-
/* Nothing is pending anymore. */
260-
m_initialCommandTimer.stop();
261-
}
262243
}
263244

264245
if (true == m_sendWaypointTimer.isTimeout())
@@ -440,11 +421,11 @@ void App_cmdRspChannelCallback(const uint8_t* payload, const uint8_t payloadSize
440421
{
441422
LOG_DEBUG("Max Speed: %d", cmdRsp->maxMotorSpeed);
442423
application->setMaxMotorSpeed(cmdRsp->maxMotorSpeed);
443-
application->notifyMaxMotorSpeedIsReceived();
424+
StartupState::getInstance().notifyCommandProcessed();
444425
}
445426
else if (RemoteControl::CMD_ID_SET_INIT_POS == cmdRsp->commandId)
446427
{
447-
application->notifyInitialPositionIsSet();
428+
StartupState::getInstance().notifyCommandProcessed();
448429
}
449430
}
450431
else

lib/ConvoyLeader/src/App.h

-27
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class App
7373
m_smpServer(Board::getInstance().getDevice().getStream(), this),
7474
m_mqttClient(),
7575
m_v2vClient(m_mqttClient),
76-
m_initialPositionSent(false),
7776
m_longitudinalController(),
7877
m_sendWaypointTimer()
7978
{
@@ -132,22 +131,6 @@ class App
132131
*/
133132
void setLastFollowerFeedback(const Waypoint& feedback);
134133

135-
/**
136-
* Notify initial position is set.
137-
*/
138-
void notifyInitialPositionIsSet()
139-
{
140-
m_initialPositionSent = true;
141-
}
142-
143-
/**
144-
* Notify max motor speed is received.
145-
*/
146-
void notifyMaxMotorSpeedIsReceived()
147-
{
148-
m_receivedMaxMotorSpeed = true;
149-
}
150-
151134
private:
152135
/** Minimum battery level in percent. */
153136
static const uint8_t MIN_BATTERY_LEVEL = 10U;
@@ -190,16 +173,6 @@ class App
190173
/** The system state machine. */
191174
StateMachine m_systemStateMachine;
192175

193-
/**
194-
* Flag for setting initial position through SMP.
195-
*/
196-
bool m_initialPositionSent;
197-
198-
/**
199-
* Flag for received the max motor speed through SMP.
200-
*/
201-
bool m_receivedMaxMotorSpeed;
202-
203176
/**
204177
* Longitudinal controller.
205178
*/

lib/ConvoyLeader/src/StartupState.cpp

+67-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
*****************************************************************************/
3535

3636
#include "StartupState.h"
37+
#include "RemoteControl.h"
38+
#include <SettingsHandler.h>
3739

3840
/******************************************************************************
3941
* Compiler Switches
@@ -61,17 +63,79 @@
6163

6264
void StartupState::entry()
6365
{
64-
/* Nothing to do */
66+
m_isActive = true;
6567
}
6668

6769
void StartupState::process(StateMachine& sm)
6870
{
69-
/* Nothing to do */
71+
72+
SettingsHandler& settings = SettingsHandler::getInstance();
73+
74+
switch (m_pendingCommandCounter)
75+
{
76+
case CMD_GET_MAX_SPEED:
77+
if (nullptr == m_pendingCommand)
78+
{
79+
/* Create new pending commmand. */
80+
m_pendingCommand = new Command{RemoteControl::CMD_ID_GET_MAX_SPEED};
81+
}
82+
else
83+
{
84+
/* Command is still pending. */
85+
}
86+
break;
87+
88+
case CMD_SET_INIT_POS:
89+
if (nullptr == m_pendingCommand)
90+
{
91+
/* Create new pending commmand. */
92+
m_pendingCommand = new Command;
93+
m_pendingCommand->commandId = RemoteControl::CMD_ID_SET_INIT_POS;
94+
m_pendingCommand->xPos = settings.getInitialXPosition();
95+
m_pendingCommand->yPos = settings.getInitialYPosition();
96+
m_pendingCommand->orientation = settings.getInitialHeading();
97+
}
98+
else
99+
{
100+
/* Command is still pending. */
101+
}
102+
break;
103+
104+
case CMD_NONE:
105+
/* All commands processed. Switch to idle state. */
106+
// sm.setState(&IdleState::getInstance());
107+
break;
108+
109+
default:
110+
break;
111+
}
70112
}
71113

72114
void StartupState::exit()
73115
{
74-
/* Nothing to do */
116+
/* Ensure the pending command is deleted. */
117+
if (nullptr != m_pendingCommand)
118+
{
119+
delete m_pendingCommand;
120+
m_pendingCommand = nullptr;
121+
}
122+
123+
m_isActive = false;
124+
}
125+
126+
Command* StartupState::getPendingCommand()
127+
{
128+
return (true == m_isActive) ? m_pendingCommand : nullptr;
129+
}
130+
131+
void StartupState::notifyCommandProcessed()
132+
{
133+
if (nullptr != m_pendingCommand)
134+
{
135+
delete m_pendingCommand;
136+
m_pendingCommand = nullptr;
137+
m_pendingCommandCounter++;
138+
}
75139
}
76140

77141
/******************************************************************************

lib/ConvoyLeader/src/StartupState.h

+33-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
*****************************************************************************/
4545

4646
#include <IState.h>
47+
#include <StateMachine.h>
48+
#include "SerialMuxChannels.h"
49+
#include "SimpleTimer.hpp"
4750

4851
/******************************************************************************
4952
* Macros
@@ -57,6 +60,14 @@
5760
class StartupState : public IState
5861
{
5962
public:
63+
/** Commands to send and process in the Startup State. */
64+
enum StartupCommands : uint8_t
65+
{
66+
CMD_GET_MAX_SPEED = 0, /**< Get maximum motor speed. */
67+
CMD_SET_INIT_POS, /**< Set initial position. */
68+
CMD_NONE /**< No pending command. Required to signal the end of the enum. */
69+
};
70+
6071
/**
6172
* Get state instance.
6273
*
@@ -88,12 +99,33 @@ class StartupState : public IState
8899
*/
89100
void exit() final;
90101

102+
/**
103+
* Get pending command. If there is no pending command or the state is not active, it will return nullptr.
104+
*
105+
* @return Pending command or nullptr.
106+
*/
107+
Command* getPendingCommand();
108+
109+
/**
110+
* Notify the state, that the pending command is successfully processed by RU.
111+
*/
112+
void notifyCommandProcessed();
113+
91114
protected:
92115
private:
116+
/** Flag: State is active. */
117+
bool m_isActive;
118+
119+
/** Pending command. */
120+
Command* m_pendingCommand;
121+
122+
/** Pending command counter. */
123+
uint8_t m_pendingCommandCounter;
124+
93125
/**
94126
* Default constructor.
95127
*/
96-
StartupState()
128+
StartupState() : IState(), m_isActive(false), m_pendingCommand(nullptr), m_pendingCommandCounter(CMD_GET_MAX_SPEED)
97129
{
98130
}
99131

0 commit comments

Comments
 (0)