Skip to content

Commit

Permalink
ADDED: Preprocessor directives for FALSE/TRUE
Browse files Browse the repository at this point in the history
  • Loading branch information
mhollfelder committed Jul 19, 2017
1 parent cc34f22 commit beca899
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 9 deletions.
93 changes: 93 additions & 0 deletions examples/motor/motor.ino
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);
}
9 changes: 2 additions & 7 deletions examples/pwm_demo/pwm_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ void setup() {
Serial.begin(9600);
while(!Serial);

//LED
pinMode(LED1, OUTPUT);

//power supply voltage should be available before tle94112.begin() is called
delay(100);
Expand Down Expand Up @@ -71,8 +69,5 @@ void loop() {
}

//let LED blink to show that program is still running
delay(200);
digitalWrite(LED1, HIGH);
delay(200);
digitalWrite(LED1, LOW);
}
delay(400);
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=TLE94112
version=0.1.0
version=1.0.1
author=Infineon Technologies
maintainer=Infineon Technologies <www.infineon.com>
sentence=This library provides an interface for Infineon's DC Motor Control Shield with TLE94112EL
Expand Down
2 changes: 1 addition & 1 deletion src/TLE94112.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define TLE94112_CMD_CLEAR 0x80;
//standard pin assignment
#define TLE94112_PIN_CS1 10
#define TLE94112_PIN_CS2 12
#define TLE94112_PIN_CS2 9
#define TLE94112_PIN_EN 8
#define TLE94112_CS_RISETIME 2

Expand Down
8 changes: 8 additions & 0 deletions src/TLE94112.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
#include "./util/tle94112_conf.h"


#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif


/*! \brief the number of halfbridges on a TLE94112
*
* \see mHalfBridges
Expand Down
201 changes: 201 additions & 0 deletions src/TLE94112_Motor.cpp
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;
}
}
Loading

0 comments on commit beca899

Please sign in to comment.