-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADDED: Preprocessor directives for FALSE/TRUE
- Loading branch information
1 parent
cc34f22
commit beca899
Showing
7 changed files
with
359 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <TLE94112.h> | ||
#include <TLE94112_Motor.h> | ||
|
||
// The TLE94112 has only got 3 PWM channels | ||
// There is no one left for motor4 | ||
// This means, we cannot control its speed | ||
Tle94112Motor motor1(tle94112, tle94112.TLE_PWM1); | ||
Tle94112Motor motor2(tle94112, tle94112.TLE_PWM2); | ||
Tle94112Motor motor3(tle94112, tle94112.TLE_PWM3); | ||
Tle94112Motor motor4(tle94112, tle94112.TLE_NOPWM); | ||
|
||
|
||
void setup() | ||
{ | ||
// call begin for the TLE94112 firs | ||
tle94112.begin(); | ||
// motor1 is connected to 2 halfbridges on each side | ||
motor1.connect(tle94112.TLE_HB1, motor1.HIGHSIDE); | ||
motor1.connect(tle94112.TLE_HB2, motor1.HIGHSIDE); | ||
motor1.connect(tle94112.TLE_HB3, motor1.LOWSIDE); | ||
motor1.connect(tle94112.TLE_HB4, motor1.LOWSIDE); | ||
|
||
// motor2 is only connected on its highside connector | ||
// the lowside connector is not connected to the TLE94112. | ||
// The library assumes it is connected to ground. | ||
// This means, the motor can only run forward | ||
motor2.connect(tle94112.TLE_HB5, motor2.HIGHSIDE); | ||
|
||
// motor3 is only connected on its lowside connector | ||
// the highside connector is not connected to the TLE94112. | ||
// The library assumes it is connected to Vbat | ||
// This means, the motor can only run forward | ||
motor3.connect(tle94112.TLE_HB6, motor3.HIGHSIDE); | ||
|
||
// motor4 is connected to 1 halfbridge on each side | ||
motor4.connect(tle94112.TLE_HB7, motor4.HIGHSIDE); | ||
motor4.connect(tle94112.TLE_HB8, motor4.LOWSIDE); | ||
|
||
//change the motors PWM frequency (just in case you will ever need it...) | ||
motor3.setPwmFreq(tle94112.TLE_FREQ80HZ); | ||
|
||
//when configuration is done, call begin to start operating the motors | ||
motor1.begin(); | ||
motor2.begin(); | ||
motor3.begin(); | ||
motor4.begin(); | ||
//after calling begin(), the motors are coasting. | ||
|
||
} | ||
|
||
|
||
|
||
void loop() | ||
{ | ||
// start the motor and let it run backwards on half speed | ||
motor1.start(-127); | ||
//motors 2 and 3 can only run forward. If you let them run backward, they will stop instead | ||
motor2.start(127); | ||
motor3.start(-127); | ||
//motor 4 can not be controlled by PWM. It can only coast, stop and run with full speed. | ||
motor4.start(127); | ||
|
||
delay(5000); | ||
|
||
//accelerate motor1 to full speed | ||
motor1.setSpeed(-255); | ||
//set motor2 free | ||
motor2.coast(); | ||
//let motor4 actively break (it will stop much faster than motor2) | ||
motor4.stop(); | ||
|
||
delay(5000); | ||
|
||
//stop all motors (you can use the parameter to set the force which stops and holds them. standard and maximum is 255) | ||
motor1.stop(255); | ||
motor2.stop(20); | ||
motor3.stop(127); | ||
motor4.stop(127); | ||
|
||
delay(5000); | ||
|
||
//motor1 and motor4 can also run the other way | ||
motor1.start(127); | ||
motor4.start(-127); | ||
|
||
delay(5000); | ||
|
||
//ok, that was enough | ||
motor1.stop(); | ||
motor4.stop(); | ||
|
||
delay(5000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
#include "TLE94112_Motor.h" | ||
|
||
#ifndef FALSE | ||
#define FALSE 0 | ||
#endif | ||
#ifndef TRUE | ||
#define TRUE 1 | ||
#endif | ||
|
||
#define TLE94112_MAX_SPEED 255 | ||
|
||
|
||
Tle94112Motor::Tle94112Motor(Tle94112 &driver, Tle94112::PWMChannel channel) | ||
{ | ||
mDriver = &driver; | ||
mChannel = channel; | ||
mNumHighside = 0; | ||
mNumLowside = 0; | ||
mPWMfreq = Tle94112::TLE_FREQ200HZ; | ||
mSpeed = 0; | ||
mActiveFW = FALSE; | ||
mMode = COAST; | ||
mEnabled = FALSE; | ||
} | ||
|
||
Tle94112Motor::~Tle94112Motor() | ||
{ | ||
end(); | ||
} | ||
|
||
void Tle94112Motor::begin(void) | ||
{ | ||
mDriver->configPWM(mChannel, mPWMfreq, mSpeed); | ||
mEnabled = TRUE; | ||
coast(); | ||
} | ||
|
||
void Tle94112Motor::end(void) | ||
{ | ||
coast(); | ||
mEnabled = FALSE; | ||
} | ||
|
||
void Tle94112Motor::connect(Tle94112::HalfBridge connector, ePolarity pol) | ||
{ | ||
if(mEnabled == FALSE) | ||
{ | ||
if(pol==LOWSIDE && mNumLowside<TLE94112_CONNECTOR_SIZE) | ||
{ | ||
mHbLowside[mNumLowside] = connector; | ||
mNumLowside++; | ||
mDriver->configHB(connector, Tle94112::TLE_FLOATING, mChannel, mActiveFW); | ||
} | ||
if(pol==HIGHSIDE && mNumHighside<TLE94112_CONNECTOR_SIZE) | ||
{ | ||
mHbHighside[mNumHighside] = connector; | ||
mNumHighside++; | ||
mDriver->configHB(connector, Tle94112::TLE_FLOATING, mChannel, mActiveFW); | ||
} | ||
} | ||
} | ||
|
||
void Tle94112Motor::setPwmFreq(Tle94112::PWMFreq freq) | ||
{ | ||
if(mEnabled == FALSE) | ||
{ | ||
mPWMfreq = freq; | ||
mDriver->configPWM(mChannel, freq, 0); | ||
} | ||
} | ||
|
||
void Tle94112Motor::stop(uint8_t force) | ||
{ | ||
uint8_t i; | ||
if(mEnabled == TRUE) | ||
{ | ||
//set all outputs floating to avoid short-circuits | ||
coast(); | ||
mMode = STOP; | ||
mSpeed = force; | ||
if(mNumHighside==0) | ||
{ | ||
// highside is constantly connected to high. | ||
// also connect lowside to high to stop motor | ||
for(i = 0; i<mNumLowside; i++) | ||
{ | ||
mDriver->configHB(mHbLowside[i], Tle94112::TLE_HIGH, mChannel, mActiveFW); | ||
} | ||
} | ||
else | ||
{ | ||
// connect all motor pins to low | ||
for(i = 0; i<mNumHighside; i++) | ||
{ | ||
mDriver->configHB(mHbHighside[i], Tle94112::TLE_LOW, mChannel, mActiveFW); | ||
} | ||
for(i = 0; i<mNumLowside; i++) | ||
{ | ||
mDriver->configHB(mHbLowside[i], Tle94112::TLE_LOW, mChannel, mActiveFW); | ||
} | ||
} | ||
// set dutycycle depending on parameter force | ||
// higher force lets the motor stop quicker | ||
mDriver->configPWM(mChannel, mPWMfreq, mSpeed); | ||
} | ||
} | ||
|
||
void Tle94112Motor::coast() | ||
{ | ||
if(mEnabled == TRUE) | ||
{ | ||
uint8_t i; | ||
mSpeed = 0; | ||
mMode = COAST; | ||
mDriver->configPWM(mChannel, mPWMfreq, mSpeed); | ||
for(i = 0; i<mNumHighside; i++) | ||
{ | ||
mDriver->configHB(mHbHighside[i], Tle94112::TLE_FLOATING, mChannel, mActiveFW); | ||
} | ||
for(i = 0; i<mNumLowside; i++) | ||
{ | ||
mDriver->configHB(mHbLowside[i], Tle94112::TLE_FLOATING, mChannel, mActiveFW); | ||
} | ||
} | ||
} | ||
|
||
void Tle94112Motor::start(int16_t speed) | ||
{ | ||
setSpeed(speed); | ||
} | ||
|
||
void Tle94112Motor::setSpeed(int16_t speed) | ||
{ | ||
uint8_t i; | ||
if(mEnabled == TRUE) | ||
{ | ||
mSpeed = speed; | ||
if(speed == 0) | ||
{ | ||
coast(); | ||
} | ||
else if(speed > 0) | ||
{ | ||
mSpeed = (speed>TLE94112_MAX_SPEED) ? TLE94112_MAX_SPEED : speed; | ||
if(mMode != FORWARD) | ||
{ | ||
//change configuration to running forward | ||
coast(); //set all outputs to HIGH-IMPEDANCE to avoid short-circuits | ||
mMode = FORWARD; | ||
for(i = 0; i<mNumHighside; i++) | ||
{ | ||
mDriver->configHB(mHbHighside[i], Tle94112::TLE_HIGH, mChannel, mActiveFW); | ||
} | ||
for(i = 0; i<mNumLowside; i++) | ||
{ | ||
mDriver->configHB(mHbLowside[i], Tle94112::TLE_LOW, mChannel, mActiveFW); | ||
} | ||
} | ||
mDriver->configPWM(mChannel, mPWMfreq, mSpeed); | ||
} | ||
else if(speed<0 && mNumHighside>0 && mNumLowside>0) | ||
{ | ||
// backward is only available if highside and lowside are connected to the TLE | ||
mSpeed = (speed < -TLE94112_MAX_SPEED) ? TLE94112_MAX_SPEED : -speed; | ||
if(mMode != BACKWARD) | ||
{ | ||
//change configuration to running backward | ||
coast(); //set all outputs to HIGH-IMPEDANCE to avoid short-circuits | ||
mMode = BACKWARD; | ||
for(i = 0; i<mNumHighside; i++) | ||
{ | ||
mDriver->configHB(mHbHighside[i], Tle94112::TLE_LOW, mChannel, mActiveFW); | ||
} | ||
for(i = 0; i<mNumLowside; i++) | ||
{ | ||
mDriver->configHB(mHbLowside[i], Tle94112::TLE_HIGH, mChannel, mActiveFW); | ||
} | ||
} | ||
mDriver->configPWM(mChannel, mPWMfreq, mSpeed); | ||
} | ||
else | ||
{ | ||
// backward desired but not available: assume user wants to break instead | ||
stop((uint8_t)(-speed)); | ||
} | ||
} | ||
} | ||
|
||
int16_t Tle94112Motor::getSpeed(void) | ||
{ | ||
if(mEnabled == TRUE) | ||
{ | ||
switch(mMode) | ||
{ | ||
case FORWARD: return mSpeed; | ||
case BACKWARD: return (-1)*mSpeed; | ||
default: return 0; | ||
} | ||
return mSpeed; | ||
} | ||
} |
Oops, something went wrong.