I'm experimenting with classes in Arduino code. I have the following small code snippet:
#include "Motor.h"
#include "Arduino.h"
Motor::Motor()
{
}
void Motor::Configure(uint16_t inA_pin, uint16_t inB_pin, uint16_t speed_pin, Stream* SerPort)
{
uint16_t _inA_pin = inA_pin;
uint16_t _inB_pin = inB_pin;
uint16_t _speed_pin = speed_pin;
gl_pSerPort = SerPort;
gl_pSerPort->printf("...._inA_pin set to %d\n", _inA_pin);
gl_pSerPort->printf("...._inB_pin set to %d\n", _inB_pin);
gl_pSerPort->printf("...._speed_pin set to %d\n", _speed_pin);
pinMode(_inA_pin, OUTPUT);
pinMode(_inB_pin, OUTPUT);
pinMode(_speed_pin, OUTPUT);
}
void Motor::RunMsec(bool bisFwd, uint16_t onMsec, uint16_t speed)
{
gl_pSerPort->printf("RunMsec(%d, %d, %d\n", bisFwd, onMsec, speed);
gl_pSerPort->printf("RunMsec: _inB_pin = %d\n", _inB_pin);
gl_pSerPort->printf("RunMsec: _speed_pin = %d\n", _speed_pin);
gl_pSerPort->printf("%lu: Calling Run(%d, %d)\n",millis(), bisFwd,speed);
Run(bisFwd, speed);
//Step 2: Delay timsec seconds
delay(onMsec);
//Step3: Stop motors added 04/12/21
gl_pSerPort->printf("%lu: Calling Stop()\n",millis());
Stop();
}
The Motor.h file is:
#pragma once
#include "Arduino.h"
const uint16_t MOTOR_SPEED_FULL = 200; //range is 0-255
const uint16_t MOTOR_SPEED_MAX = 255; //range is 0-255
const uint16_t MOTOR_SPEED_HALF = 127; //range is 0-255
const uint16_t MOTOR_SPEED_QTR = 75; //added 09/25/20
const uint16_t MOTOR_SPEED_LOW = 50; //added 01/22/15
const uint16_t MOTOR_SPEED_MIN = 15; //added 12/24/23 for 'mirrored sfc' detection
const uint16_t MOTOR_SPEED_OFF = 0; //range is 0-255
const uint16_t MOTOR_SPEED_CAPTURE_OFFSET = 75; //added 06/21/20 for offset capture
const uint16_t TURN_START_SPEED = MOTOR_SPEED_QTR; //added 11/14/21
class Motor
{
public:
Motor();
void Configure(uint16_t inA_pin, uint16_t inB_pin, uint16_t speed_pin, Stream* gl_pSerPort);
void RunMsec(bool bIsFwd = true, uint16_t onMsec = 1000, uint16_t speed = MOTOR_SPEED_HALF);
void Run(bool bIsFwd = true, uint16_t speed = MOTOR_SPEED_HALF);
void Stop();
private:
uint16_t _inA_pin;
uint16_t _inB_pin;
uint16_t _speed_pin;
Stream* gl_pSerPort;
void SetDirAndSpeed(bool bIsFwd, uint16_t speed);
};
The code in the corresponding .ino file is:
#include "Motor.h"
#include <Wire.h>
#include "FlashTxx.h" // TLC/T3x/T4x flash primitives
#pragma region MOTOR PINS
//11/04/21 Now using Pololu VNH5019 drivers for all 4 motors
const uint16_t InA_Left = 22;
const uint16_t InB_Left = 21;
const uint16_t Spd_Left = 23;
const uint16_t InA_Right = 34;
const uint16_t InB_Right = 33;
const uint16_t Spd_Right = 35;
#pragma endregion MOTOR PINS
Motor LeftMotor;
Motor RightMotor;
Stream* gl_pSerPort = 0; //09/26/22 made global so can use everywhere.
void setup()
{
#pragma region SERIAL_PORTS
Serial.begin(115200);
delay(2000); //10/06/21 - just use fixed delay instead
Serial1.begin(115200);
delay(2000); //11/20/21 use fixed delay instead of waiting
if (Serial)
{
Serial.printf("Serial port active\n");
gl_pSerPort = (Stream*)&Serial;
}
else if (Serial1)
{
Serial.printf("Serial1 port active\n");
gl_pSerPort = (Stream*)&Serial1;
}
gl_pSerPort->printf("gl_pSerPort now points to active Serial (USB or Wixel)\n");
//02/25/23 added for Garmin LIDAR-Lite V4/LED use
Serial2.begin(115200);
delay(2000); //11/20/21 use fixed delay instead of waiting
LeftMotor.Configure(InA_Left, InB_Left, Spd_Left, gl_pSerPort);
RightMotor.Configure(InA_Right, InB_Right, Spd_Right, gl_pSerPort);
#pragma endregion SERIAL_PORTS
}
void loop()
{
//redLED.Blink(2000);
//delay(200);
//yelLED.Blink(2000);
//delay(1000);
gl_pSerPort->printf("%lu: LeftMotor.RunMsec()\n", millis());
LeftMotor.RunMsec(5000); //fwd for 1sec
delay(500);
gl_pSerPort->printf("%lu: RightMotor.RunMsec()\n", millis());
RightMotor.RunMsec(5000); //fwd for 1sec
delay(500);
}
When this is run, I get the following output from the embedded print statements:
Opening port
Port open
Serial port active
gl_pSerPort now points to active Serial (USB or Wixel)
...._inA_pin set to 22
...._inB_pin set to 21
...._speed_pin set to 23
...._inA_pin set to 34
...._inB_pin set to 33
...._speed_pin set to 35
6300: LeftMotor.RunMsec()
RunMsec(1, 1000, 127
RunMsec: _inB_pin = 0
RunMsec: _speed_pin = 0
6300: Calling Run(1, 127)
In SetDirAndSpeed(true, 127)
In TRUE block of SetRighttMotorDirAndSpeed(true, 127)
....Pin 0 set to 1
....Pin 0 set to 1
7300: Calling Stop()
In SetDirAndSpeed(true, 0)
In TRUE block of SetRighttMotorDirAndSpeed(true, 0)
....Pin 0 set to 0
....Pin 0 set to 0
7800: RightMotor.RunMsec()
From the printouts, I can see that Motor::Configure() properly initializes the private member variables, but Motor::RunMsec() reports that _inB_pin = 0, and _speed_pin = 0, so something is preventing RunMsec from seeing the values set by Configure().
Can anyone see what I'm doing wrong here?
TIA,
Frank
-
I tried making changing the private variables to 'public' (by commenting out the 'private:' line), but this had no effect - the variable values were still shown as '0' in the RunMsec() functionstarship15– starship152024年06月03日 23:04:05 +00:00Commented Jun 3, 2024 at 23:04
-
1why are you commenting on your own question? ... if you have anything to add, then please edit the question insteadjsotola– jsotola2024年06月03日 23:55:34 +00:00Commented Jun 3, 2024 at 23:55
-
duplicate of arduino.stackexchange.com/questions/57515/…Juraj– Juraj ♦2024年06月04日 07:11:31 +00:00Commented Jun 4, 2024 at 7:11
1 Answer 1
In your configure function:
void Motor::Configure(uint16_t inA_pin, uint16_t inB_pin, uint16_t speed_pin, Stream* SerPort)
{
uint16_t _inA_pin = inA_pin;
uint16_t _inB_pin = inB_pin;
uint16_t _speed_pin = speed_pin;
gl_pSerPort = SerPort;
You are creating new local variables named _inA_pin and _inB_pin and _speed_pin. These new local variables shadow the member variables. You have multiple variables with the same name, and the more local scope is the one that will be used.
I don't think you want to have the uint16_t
there. I think you want to put those values into the member variables in the class.
void Motor::Configure(uint16_t inA_pin, uint16_t inB_pin, uint16_t speed_pin, Stream* SerPort)
{
_inA_pin = inA_pin;
_inB_pin = inB_pin;
_speed_pin = speed_pin;
gl_pSerPort = SerPort;
-
DUH!! Thanks for pointing out the obvious (to everyone but me!) error.starship15– starship152024年06月04日 00:16:53 +00:00Commented Jun 4, 2024 at 0:16