Skip to content

Commit ccb7bc8

Browse files
committed
Added driving state.
Replaced LongitudinalController with driving state
1 parent 74e674b commit ccb7bc8

File tree

4 files changed

+133
-250
lines changed

4 files changed

+133
-250
lines changed

lib/ConvoyLeader/src/App.cpp

+22-70
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <Util.h>
4444
#include "StartupState.h"
4545
#include "IdleState.h"
46+
#include "DrivingState.h"
4647

4748
/******************************************************************************
4849
* Compiler Switches
@@ -171,12 +172,6 @@ void App::setup()
171172
}
172173
else
173174
{
174-
/* Setup longitudinal controller. */
175-
LongitudinalController::MotorSetpointCallback motorSetpointCallback = [this](const int16_t& topCenterSpeed)
176-
{ return this->motorSetpointCallback(topCenterSpeed); };
177-
178-
m_longitudinalController.setMotorSetpointCallback(motorSetpointCallback);
179-
180175
/* Initialize timers. */
181176
m_sendWaypointTimer.start(SEND_WAYPOINT_TIMER_INTERVAL);
182177
m_commandTimer.start(SEND_COMMANDS_TIMER_INTERVAL);
@@ -220,62 +215,16 @@ void App::loop()
220215
/* Process V2V Communication */
221216
m_v2vClient.process();
222217

223-
/* Process Longitudinal Controller */
224-
m_longitudinalController.process();
225-
226218
/* Process System State Machine */
227219
m_systemStateMachine.process();
228220

229221
/* Process periodic tasks. */
230222
processPeriodicTasks();
231223
}
232224

233-
void App::currentVehicleChannelCallback(const VehicleData& vehicleData)
234-
{
235-
m_latestVehicleData = Waypoint(vehicleData.xPos, vehicleData.yPos, vehicleData.orientation, vehicleData.left,
236-
vehicleData.right, vehicleData.center);
237-
238-
// latestVehicleData.debugPrint();
239-
240-
m_longitudinalController.calculateTopMotorSpeed(m_latestVehicleData);
241-
}
242-
243-
bool App::motorSetpointCallback(const int16_t topCenterSpeed)
225+
void App::setLatestVehicleData(const Waypoint& waypoint)
244226
{
245-
SpeedData payload;
246-
payload.center = topCenterSpeed;
247-
248-
return m_smpServer.sendData(m_serialMuxProtChannelIdMotorSpeeds, &payload, sizeof(payload));
249-
}
250-
251-
void App::release()
252-
{
253-
Command payload;
254-
payload.commandId = RemoteControl::CMD_ID_START_DRIVING;
255-
256-
/* Release robot. */
257-
if (false == m_longitudinalController.release())
258-
{
259-
LOG_WARNING("Robot could not be released.");
260-
}
261-
else if (false == m_smpServer.sendData(m_serialMuxProtChannelIdRemoteCtrl, &payload, sizeof(payload)))
262-
{
263-
LOG_WARNING("Failed to send release command to RU.");
264-
}
265-
else
266-
{
267-
LOG_INFO("Robot released.");
268-
}
269-
}
270-
271-
void App::setMaxMotorSpeed(const int16_t maxMotorSpeed)
272-
{
273-
m_longitudinalController.setMaxMotorSpeed(maxMotorSpeed);
274-
}
275-
276-
void App::setLastFollowerFeedback(const Waypoint& feedback)
277-
{
278-
m_longitudinalController.setLastFollowerFeedback(feedback);
227+
m_latestVehicleData = waypoint;
279228
}
280229

281230
/******************************************************************************
@@ -329,20 +278,19 @@ bool App::setupMqttClient()
329278
}
330279
else
331280
{
332-
isSuccessful =
333-
m_mqttClient.subscribe(TOPIC_NAME_RELEASE, true, [this](const String& payload) { this->release(); });
334-
335-
isSuccessful &= m_mqttClient.subscribe("platoons/0/vehicles/0/feedback", false,
336-
[this](const String& payload)
337-
{
338-
Waypoint* waypoint = Waypoint::deserialize(payload);
339-
340-
if (nullptr != waypoint)
341-
{
342-
this->setLastFollowerFeedback(*waypoint);
343-
delete waypoint;
344-
}
345-
});
281+
isSuccessful = m_mqttClient.subscribe(
282+
TOPIC_NAME_RELEASE, true, [this](const String& payload) { IdleState::getInstance().requestRelease(); });
283+
284+
isSuccessful &=
285+
m_mqttClient.subscribe("platoons/0/vehicles/0/feedback", false,
286+
[this](const String& payload)
287+
{
288+
Waypoint* waypoint = Waypoint::deserialize(payload);
289+
VehicleData feedback{waypoint->xPos, waypoint->yPos, waypoint->orientation,
290+
waypoint->left, waypoint->right, waypoint->center};
291+
292+
DrivingState::getInstance().setLastFollowerFeedback(feedback);
293+
});
346294
}
347295

348296
return isSuccessful;
@@ -441,7 +389,7 @@ void App_cmdRspChannelCallback(const uint8_t* payload, const uint8_t payloadSize
441389
else if (RemoteControl::CMD_ID_GET_MAX_SPEED == cmdRsp->commandId)
442390
{
443391
LOG_DEBUG("Max Speed: %d", cmdRsp->maxMotorSpeed);
444-
application->setMaxMotorSpeed(cmdRsp->maxMotorSpeed);
392+
DrivingState::getInstance().setMaxMotorSpeed(cmdRsp->maxMotorSpeed);
445393
StartupState::getInstance().notifyCommandProcessed();
446394
}
447395
else if (RemoteControl::CMD_ID_SET_INIT_POS == cmdRsp->commandId)
@@ -477,7 +425,11 @@ void App_currentVehicleChannelCallback(const uint8_t* payload, const uint8_t pay
477425
{
478426
const VehicleData* currentVehicleData = reinterpret_cast<const VehicleData*>(payload);
479427
App* application = reinterpret_cast<App*>(userData);
480-
application->currentVehicleChannelCallback(*currentVehicleData);
428+
Waypoint dataAsWaypoint(currentVehicleData->xPos, currentVehicleData->yPos, currentVehicleData->orientation,
429+
currentVehicleData->left, currentVehicleData->right, currentVehicleData->center);
430+
431+
application->setLatestVehicleData(dataAsWaypoint);
432+
DrivingState::getInstance().setVehicleData(*currentVehicleData);
481433
}
482434
else
483435
{

lib/ConvoyLeader/src/App.h

+3-39
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
#include <StateMachine.h>
5151
#include <V2VClient.h>
5252
#include "SerialMuxChannels.h"
53-
#include "LongitudinalController.h"
5453

5554
/******************************************************************************
5655
* Macros
@@ -73,7 +72,6 @@ class App
7372
m_smpServer(Board::getInstance().getDevice().getStream(), this),
7473
m_mqttClient(),
7574
m_v2vClient(m_mqttClient),
76-
m_longitudinalController(),
7775
m_sendWaypointTimer()
7876
{
7977
}
@@ -96,40 +94,11 @@ class App
9694
void loop();
9795

9896
/**
99-
* Callback for the current vehicle data.
97+
* Set latest vehicle data from RU.
10098
*
101-
* @param[in] vehicleData Current vehicle data.
99+
* @param[in] waypoint Latest vehicle data from RU.
102100
*/
103-
void currentVehicleChannelCallback(const VehicleData& vehicleData);
104-
105-
/**
106-
* Motor setpoint callback.
107-
* Called in order to send the motor speeds using SerialMuxProt to the robot.
108-
*
109-
* @param[in] topCenterSpeed Center motor speed [steps/s].
110-
*
111-
* @return If the motor speed was sent successfully, returns true. Otherwise, false.
112-
*/
113-
bool motorSetpointCallback(const int16_t topCenterSpeed);
114-
115-
/**
116-
* Release robot and start driving.
117-
*/
118-
void release();
119-
120-
/**
121-
* Set max motor speed.
122-
*
123-
* @param[in] maxMotorSpeed Max motor speed [steps/s].
124-
*/
125-
void setMaxMotorSpeed(const int16_t maxMotorSpeed);
126-
127-
/**
128-
* Set incoming feedback from last follower.
129-
*
130-
* @param[in] feedback Feedback from last follower.
131-
*/
132-
void setLastFollowerFeedback(const Waypoint& feedback);
101+
void setLatestVehicleData(const Waypoint& waypoint);
133102

134103
private:
135104
/** Minimum battery level in percent. */
@@ -176,11 +145,6 @@ class App
176145
/** The system state machine. */
177146
StateMachine m_systemStateMachine;
178147

179-
/**
180-
* Longitudinal controller.
181-
*/
182-
LongitudinalController m_longitudinalController;
183-
184148
/**
185149
* Latest vehicle data from RU.
186150
*/

lib/ConvoyLeader/src/LongitudinalController.cpp lib/ConvoyLeader/src/DrivingState.cpp

+38-56
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@
2525
DESCRIPTION
2626
*******************************************************************************/
2727
/**
28-
* @brief Leader Longitudinal Controller.
28+
* @brief Driving state.
2929
* @author Gabryel Reyes <gabryelrdiaz@gmail.com>
3030
*/
3131

3232
/******************************************************************************
3333
* Includes
3434
*****************************************************************************/
3535

36-
#include "LongitudinalController.h"
37-
#include <Logging.h>
36+
#include "DrivingState.h"
3837

3938
/******************************************************************************
4039
* Compiler Switches
@@ -60,72 +59,55 @@
6059
* Public Methods
6160
*****************************************************************************/
6261

63-
LongitudinalController::LongitudinalController() :
64-
m_maxMotorSpeed(0),
65-
m_state(STATE_STARTUP),
66-
m_lastFollowerFeedback(),
67-
m_motorSetpointCallback(nullptr)
62+
void DrivingState::entry()
6863
{
64+
m_isActive = true;
6965
}
7066

71-
LongitudinalController::~LongitudinalController()
67+
void DrivingState::process(StateMachine& sm)
7268
{
69+
/* Check if the state is active. */
70+
if (false == m_isActive)
71+
{
72+
m_topMotorSpeed = 0;
73+
}
74+
else
75+
{
76+
/* Check limits. */
77+
78+
/* Check follower feedback. Calculate platoon length and react accordingly */
79+
80+
/* Calculate top motor speed. */
81+
m_topMotorSpeed = m_maxMotorSpeed;
82+
}
7383
}
7484

75-
void LongitudinalController::process()
85+
void DrivingState::exit()
7686
{
77-
switch (m_state)
78-
{
79-
case STATE_STARTUP:
80-
/* Max speed must be positive. */
81-
if (0 < m_maxMotorSpeed)
82-
{
83-
m_state = STATE_READY;
84-
}
85-
86-
break;
87-
88-
case STATE_READY:
89-
/* Wait for external release. */
90-
break;
91-
92-
case STATE_DRIVING:
93-
/* Allow top motor speed calculation. */
94-
break;
95-
96-
case STATE_SAFE:
97-
/* Stop the vehicle. Sent continously to prevent overwriting by other modules. */
98-
if (nullptr != m_motorSetpointCallback)
99-
{
100-
m_motorSetpointCallback(0);
101-
}
102-
103-
break;
104-
105-
default:
106-
/* Should never happen. */
107-
m_state = STATE_SAFE;
108-
break;
109-
}
87+
m_isActive = false;
11088
}
11189

112-
void LongitudinalController::calculateTopMotorSpeed(const Waypoint& currentVehicleData)
90+
void DrivingState::setMaxMotorSpeed(int16_t maxSpeed)
11391
{
114-
if (STATE_DRIVING == m_state)
115-
{
116-
int16_t topMotorSpeed = 0;
92+
m_maxMotorSpeed = maxSpeed;
93+
}
11794

118-
/* TODO: Check follower feedback. Calculate platoon length and react accordingly. */
95+
bool DrivingState::getTopMotorSpeed(int16_t& topMotorSpeed) const
96+
{
97+
topMotorSpeed = m_topMotorSpeed;
11998

120-
/* Calculate top motor speed. */
121-
topMotorSpeed = m_maxMotorSpeed;
99+
/* Only valid if the state is active. */
100+
return m_isActive;
101+
}
122102

123-
/* Send top motor speed. */
124-
if (nullptr != m_motorSetpointCallback)
125-
{
126-
m_motorSetpointCallback(topMotorSpeed);
127-
}
128-
}
103+
void DrivingState::setVehicleData(const VehicleData& vehicleData)
104+
{
105+
m_vehicleData = vehicleData;
106+
}
107+
108+
void DrivingState::setLastFollowerFeedback(const VehicleData& feedback)
109+
{
110+
m_followerFeedback = feedback;
129111
}
130112

131113
/******************************************************************************

0 commit comments

Comments
 (0)