Code would not update variable inside drive method. The variable is updated inside the drive method, but it doesn't change in the rest of the class. I wrote a simple similar method in normal C++ and the variable actually updated in the rest of the class.
engine.hpp
#pragma once
#include <Arduino.h>
namespace engine
{
class Engine
{
public:
Engine(const unsigned char t_motor, const unsigned char t_forward, const unsigned char t_backward);
void drive(unsigned int t_speed, bool t_isForward = true);
bool isMovingForward() const;
void stop();
private:
const unsigned char m_motor;
const unsigned char m_forward;
const unsigned char m_backward;
// Movements
bool m_isActive;
bool m_isForward;
unsigned int m_speed;
};
} // namespace engine
engine.cpp init list
Engine::Engine(const unsigned char t_motor, const unsigned char t_forward, const unsigned char t_backward)
: m_motor(t_motor),
m_forward(t_forward),
m_backward(t_backward),
m_isActive(false),
m_isForward(true),
m_speed(0U)
{
}
drive method
void Engine::drive(unsigned int t_speed, bool t_isForward)
{
if (!m_isActive || m_isForward != t_isForward)
{
stop();
// Set direction
if (t_isForward)
{
digitalWrite(m_forward, HIGH);
digitalWrite(m_backward, LOW);
}
else
{
digitalWrite(m_forward, LOW);
digitalWrite(m_backward, HIGH);
}
m_isActive = true;
m_isForward = t_isForward;
}
if (m_speed != t_speed)
{
m_speed = t_speed; // Updated here
analogWrite(m_motor, t_speed);
}
}
-
how do you know it didn't update?Juraj– Juraj ♦2019年08月02日 07:05:32 +00:00Commented Aug 2, 2019 at 7:05
-
What code do you use to test this?chrisl– chrisl2019年08月02日 12:11:48 +00:00Commented Aug 2, 2019 at 12:11
-
I know it didn't update because when I check the m_speed variable after calling the drive method, the m_speed variable is set back to zero.F--- it– F--- it2019年08月02日 13:27:59 +00:00Commented Aug 2, 2019 at 13:27
-
Downvote just for the profane user name. Have some class manDelta_G– Delta_G2020年08月26日 17:24:10 +00:00Commented Aug 26, 2020 at 17:24
1 Answer 1
I figured it out.
I was passing the engine objects as references instead of pointers.
main.cpp
// Engines
engine::Engine
engine1(motors::Constants::MOTOR_1, motors::Constants::MOTOR_1_FORWARD, motors::Constants::MOTOR_1_BACKWARD),
engine2(motors::Constants::MOTOR_2, motors::Constants::MOTOR_2_FORWARD, motors::Constants::MOTOR_2_BACKWARD),
engine3(motors::Constants::MOTOR_3, motors::Constants::MOTOR_3_FORWARD, motors::Constants::MOTOR_3_BACKWARD),
engine4(motors::Constants::MOTOR_4, motors::Constants::MOTOR_4_FORWARD, motors::Constants::MOTOR_4_BACKWARD);
engine_controller.cpp init list
EngineController::EngineController(engine::Engine &t_e1, engine::Engine &t_e2, engine::Engine &t_e3, engine::Engine &t_e4)
: m_engine1(t_e1),
m_engine2(t_e2),
m_engine3(t_e3),
m_engine4(t_e4),
m_currentSpeed(EngineControllerConsts::MAX_SPEED),
// Directions
m_rights(0),
m_lefts(0),
m_rightDistance(-1),
m_leftDistance(-1),
m_lastForward(true),
m_counter(0),
m_lastForward1(true),
m_lastForward2(true),
m_turnsCounter(0)
{
}
The solution would be to use dynamic memory allocation.
main.cpp
// Engines
engine::Engine
*engine1 = new engine::Engine(motors::Constants::MOTOR_1, motors::Constants::MOTOR_1_FORWARD, motors::Constants::MOTOR_1_BACKWARD),
*engine2 = new engine::Engine(motors::Constants::MOTOR_2, motors::Constants::MOTOR_2_FORWARD, motors::Constants::MOTOR_2_BACKWARD),
*engine3 = new engine::Engine(motors::Constants::MOTOR_3, motors::Constants::MOTOR_3_FORWARD, motors::Constants::MOTOR_3_BACKWARD),
*engine4 = new engine::Engine(motors::Constants::MOTOR_4, motors::Constants::MOTOR_4_FORWARD, motors::Constants::MOTOR_4_BACKWARD);
engine_controller.cpp init list
EngineController::EngineController(
engine::Engine *t_e1, engine::Engine *t_e2,
engine::Engine *t_e3, engine::Engine *t_e4)
: m_engine1(t_e1),
m_engine2(t_e2),
m_engine3(t_e3),
m_engine4(t_e4),
m_currentSpeed(EngineControllerConsts::MAX_SPEED),
// Directions
m_rights(0),
m_lefts(0),
m_rightDistance(-1),
m_leftDistance(-1),
m_lastForward(true),
m_counter(0),
m_lastForward1(true),
m_lastForward2(true),
m_turnsCounter(0)
{
}