Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make min PWM a configuration option to allow custom motor drivers #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sunray/config_example.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Also, you may choose the serial port below for serial monitor output (CONSOLE).
// ----- gear motors --------------------------------------------------
// for brushless motors, study the sections (drivers, adapter, protection etc.) in the Wiki (https://wiki.ardumower.de/index.php?title=DIY_Brushless_Driver_Board)
// #define MOTOR_DRIVER_BRUSHLESS 1 // uncomment this for new brushless motor drivers
#define MOTOR_DRIVER_MIN_PWM 2 // set this to 0 if you use custom motor drivers and your motors do not stop spinning when stopped
#define MOWER_DRIVER_MIN_PWM 2 // set this to 0 if you use custom mower drivers and your motors do not stop spinning when stopped

#define MOTOR_OVERLOAD_CURRENT 0.8 // gear motors overload current (amps)

Expand Down Expand Up @@ -514,4 +516,3 @@ Also, you may choose the serial port below for serial monitor output (CONSOLE).
// 2. Locate file 'packages/arduino/hardware/sam/xxxxx/cores/arduino/RingBuffer.h

#define SERIAL_BUFFER_SIZE 1024

2 changes: 1 addition & 1 deletion sunray/robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ MPU9250_DMP imu;
SerialRainSensorDriver rainDriver(robotDriver);
#else
AmRobotDriver robotDriver;
AmMotorDriver motorDriver;
AmMotorDriver motorDriver(MOTOR_DRIVER_MIN_PWM, MOWER_DRIVER_MIN_PWM);
AmBatteryDriver batteryDriver;
AmBumperDriver bumper;
AmStopButtonDriver stopButton;
Expand Down
25 changes: 9 additions & 16 deletions sunray/src/driver/AmRobotDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void OdometryRightISR(){
odomTicksRight++;
}

AmMotorDriver::AmMotorDriver(){
AmMotorDriver::AmMotorDriver(int _motorMinPwm, int _mowerMinPwm) : motorMinPwm(_motorMinPwm), mowerMinPwm(_mowerMinPwm) {
}


Expand Down Expand Up @@ -128,27 +128,20 @@ void AmMotorDriver::setMC33926(int pinDir, int pinPWM, int speed) {
// IN1 PinPWM IN2 PinDir
// PWM L Forward
// PWM H Reverse
// verhindert dass das PWM Signal 0 wird. Der Driver braucht einen kurzen Impuls um das PWM zu erkennen.
// Wenn der z.B. vom max. PWM Wert auf 0 bzw. das Signal auf Low geht, behält er den vorherigen Wert bei und der Motor stoppt nicht
void AmMotorDriver::setBrushless(int pinDir, int pinPWM, int speed) {
// minPwm verhindert, dass das PWM Signal 0 wird. Manche Driver brauchen einen kurzen Impuls, um das PWM zu erkennen.
// Wenn der z.B. vom max. PWM Wert auf 0 bzw. das Signal auf Low geht, behalten diese den vorherigen Wert bei und der Motor stoppt nicht.
void AmMotorDriver::setBrushless(int pinDir, int pinPWM, int speed, int minPwm) {
//DEBUGLN(speed);
if (speed < 0) {
digitalWrite(pinDir, HIGH) ;
if (speed >= -2) speed = -2;
pinMan.analogWrite(pinPWM, ((byte)abs(speed)));
} else {
digitalWrite(pinDir, LOW) ;
if (speed <= 2) speed = 2;
pinMan.analogWrite(pinPWM, ((byte)abs(speed)));
}
digitalWrite(pinDir, speed < 0 ? HIGH : LOW);
pinMan.analogWrite(pinPWM, (byte) max(minPwm, abs(speed)) );
}


void AmMotorDriver::setMotorPwm(int leftPwm, int rightPwm, int mowPwm){
#ifdef MOTOR_DRIVER_BRUSHLESS
setBrushless(pinMotorLeftDir, pinMotorLeftPWM, leftPwm);
setBrushless(pinMotorRightDir, pinMotorRightPWM, rightPwm);
setBrushless(pinMotorMowDir, pinMotorMowPWM, mowPwm);
setBrushless(pinMotorLeftDir, pinMotorLeftPWM, leftPwm, motorMinPwm);
setBrushless(pinMotorRightDir, pinMotorRightPWM, rightPwm, motorMinPwm);
setBrushless(pinMotorMowDir, pinMotorMowPWM, mowPwm, mowerMinPwm);
#else
setMC33926(pinMotorLeftDir, pinMotorLeftPWM, leftPwm);
setMC33926(pinMotorRightDir, pinMotorRightPWM, rightPwm);
Expand Down
7 changes: 5 additions & 2 deletions sunray/src/driver/AmRobotDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ class AmRobotDriver {


class AmMotorDriver: public MotorDriver {
private:
int motorMinPwm;
int mowerMinPwm;
public:
AmMotorDriver();
AmMotorDriver(int _motorMinPwm, int _mowerMinPwm);
void begin() override;
void run() override;
void setMotorPwm(int leftPwm, int rightPwm, int mowPwm) override;
Expand All @@ -32,7 +35,7 @@ class AmMotorDriver: public MotorDriver {
void getMotorEncoderTicks(int &leftTicks, int &rightTicks, int &mowTicks) override;
protected:
void setMC33926(int pinDir, int pinPWM, int speed);
void setBrushless(int pinDir, int pinPWM, int speed);
void setBrushless(int pinDir, int pinPWM, int speed, int minPwm);
};

class AmBatteryDriver : public BatteryDriver {
Expand Down