Skip to content

Commit

Permalink
Re-implemented DRV8833 features
Browse files Browse the repository at this point in the history
Renamed pin and pwm variables to match DRV8833's input names
Added details from data sheet about decay modes
  • Loading branch information
Kadah committed Aug 25, 2018
1 parent 57481a0 commit 9af62c8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
77 changes: 56 additions & 21 deletions Motor.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
/*
Driver for one channel of a DRV8833
http://www.ti.com/lit/ds/symlink/drv8833.pdf
*/

#include "Arduino.h"
#include "Motor.h"
#include "TweenDuino.h" // https://github.com/stickywes/TweenDuino
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include "Pwm.h"
#include "RandObject.h"

//#define DEBUG
// #define DEBUG

Motor::Motor( uint8_t pin_fwd, uint8_t pin_back ) :
Motor::Motor( uint8_t pin_in1, uint8_t pin_in2 ) :
_duty(0),
pwm_fwd(pin_fwd),
pwm_back(pin_back),
pwm_in1(pin_in1),
pwm_in2(pin_in2),
_repeats(0)
{
// Force initial state
//pwm_fwd = Pwm(pin_fwd);
//pwm_back = Pwm(pin_back);
//pwm_in1 = Pwm(pin_in1);
//pwm_in2 = Pwm(pin_in2);
setPWM(0);
}

Expand Down Expand Up @@ -132,26 +137,57 @@ void Motor::update(){
// fast_decay and forward are false by default
void Motor::setPWM( uint8_t duty, bool fast_decay, bool forward ){

//Serial.printf("Setting duty: %i on channel %i and 0 on channel %i \n", duty, pwm_fwd._channel, pwm_back._channel);
#ifdef DEBUG
Serial.printf("Setting duty: %3i on channels %3i and %2i, fast_decay: %5s, forward: %s\n", duty, pwm_in1._channel, pwm_in2._channel, fast_decay ? "true" : "false", forward ? "true" : "false");
#endif

_duty = duty;

pwm_fwd.setPWM(duty);
pwm_back.setPWM(0);

/*
Todo: Kadah I'm not 100% sure how this works, could you look into it?
/*
http://www.ti.com/lit/ds/symlink/drv8833.pdf
7.3.2 Bridge Control and Decay Modes
The AIN1 and AIN2 input pins control the state of the AOUT1 and AOUT2 outputs; similarly, the BIN1 and BIN2
input pins control the state of the BOUT1 and BOUT2 outputs. Table 1 shows the logic.
Table 1. H-Bridge Logic
xIN1 xIN2 xOUT1 xOUT2 FUNCTION
0 0 Z Z Coast/fast decay
0 1 L H Reverse
1 0 H L Forward
1 1 L L Brake/slow decay
The inputs can also be used for PWM control of the motor speed. When controlling a winding with PWM, when
the drive current is interrupted, the inductive nature of the motor requires that the current must continue to flow.
This is called recirculation current. To handle this recirculation current, the H-bridge can operate in two different
states: fast decay or slow decay. In fast decay mode, the H-bridge is disabled and recirculation current flows
through the body diodes; in slow decay, the motor winding is shorted.
To PWM using fast decay, the PWM signal is applied to one xIN pin while the other is held low; to use slow
decay, one xIN pin is held high.
To PWM using fast decay, the PWM signal is applied to one xIN pin while the other is held low; to use slow
decay, one xIN pin is held high.
Table 2: PWM Control of Motor Speed
xIN1 xIN2 FUNCTION
PWM 0 Forward PWM, fast decay
1 PWM Forward PWM, slow decay
0 PWM Reverse PWM, fast decay
PWM 1 Reverse PWM, slow decay
*/

if( forward ){

if( fast_decay ) {

pwmdriver.set_duty(motor, duty);
pwmdriver.set_duty(motor+1, 0);
pwm_in1.setPWM(duty);
pwm_in2.setPWM(0);

}
else{ // slow decay

pwmdriver.set_duty(motor, 255);
pwmdriver.set_duty(motor+1, 255-duty);
pwm_in1.setPWM(255);
pwm_in2.setPWM(255-duty);

}

Expand All @@ -160,20 +196,19 @@ void Motor::setPWM( uint8_t duty, bool fast_decay, bool forward ){

if( fast_decay ){

pwmdriver.set_duty(motor, 0);
pwmdriver.set_duty(motor+1, duty);
pwm_in1.setPWM(0);
pwm_in2.setPWM(duty);
}
else{

// slow decay
//Serial.printf("Setting duty: %i = %i, %i = %i \n", motor, 255-duty, motor+1, 255);
pwmdriver.set_duty(motor, 255-duty);
pwmdriver.set_duty(motor+1, 255);
pwm_in1.setPWM(255-duty);
pwm_in2.setPWM(255);

}

}
*/

}

6 changes: 3 additions & 3 deletions Motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class Motor{

public:
Motor( uint8_t pin_fwd, uint8_t pin_back ); // Creates the motor
Motor( uint8_t pin_in1, uint8_t pin_in2 ); // Creates the motor
void loadProgram( JsonArray &stages, int repeats ); // Loads a program onto this
void update(); // Program loop
void setPWM( uint8_t duty, bool fast_decay = false, bool forward = false ); // Sets the PWM
Expand All @@ -27,8 +27,8 @@ class Motor{

protected:
float _duty; // Duty cycle (0-255)
Pwm pwm_fwd; // Pwm forward object
Pwm pwm_back; // Pwm back object
Pwm pwm_in1; // Pwm forward object
Pwm pwm_in2; // Pwm back object

};

Expand Down

0 comments on commit 9af62c8

Please sign in to comment.