From beca8998e58963db377f63858deebb2eef07bb56 Mon Sep 17 00:00:00 2001 From: mhollfelder Date: Thu, 20 Jul 2017 00:44:20 +0200 Subject: [PATCH] ADDED: Preprocessor directives for FALSE/TRUE --- examples/motor/motor.ino | 93 +++++++++++++++ examples/pwm_demo/pwm_demo.ino | 9 +- library.properties | 2 +- src/TLE94112.cpp | 2 +- src/TLE94112.h | 8 ++ src/TLE94112_Motor.cpp | 201 +++++++++++++++++++++++++++++++++ src/TLE94112_Motor.h | 53 +++++++++ 7 files changed, 359 insertions(+), 9 deletions(-) create mode 100644 examples/motor/motor.ino create mode 100644 src/TLE94112_Motor.cpp create mode 100644 src/TLE94112_Motor.h diff --git a/examples/motor/motor.ino b/examples/motor/motor.ino new file mode 100644 index 00000000..40e77352 --- /dev/null +++ b/examples/motor/motor.ino @@ -0,0 +1,93 @@ +#include +#include + +// 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); +} diff --git a/examples/pwm_demo/pwm_demo.ino b/examples/pwm_demo/pwm_demo.ino index 2992ee66..26b829ec 100644 --- a/examples/pwm_demo/pwm_demo.ino +++ b/examples/pwm_demo/pwm_demo.ino @@ -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); @@ -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); -} \ No newline at end of file + delay(400); +} diff --git a/library.properties b/library.properties index 720a44b6..4052d7d0 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TLE94112 -version=0.1.0 +version=1.0.1 author=Infineon Technologies maintainer=Infineon Technologies sentence=This library provides an interface for Infineon's DC Motor Control Shield with TLE94112EL diff --git a/src/TLE94112.cpp b/src/TLE94112.cpp index e38576c1..b96c6cc3 100644 --- a/src/TLE94112.cpp +++ b/src/TLE94112.cpp @@ -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 diff --git a/src/TLE94112.h b/src/TLE94112.h index 15c7e34c..72bf35a6 100644 --- a/src/TLE94112.h +++ b/src/TLE94112.h @@ -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 diff --git a/src/TLE94112_Motor.cpp b/src/TLE94112_Motor.cpp new file mode 100644 index 00000000..7aa236f8 --- /dev/null +++ b/src/TLE94112_Motor.cpp @@ -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 && mNumLowsideconfigHB(connector, Tle94112::TLE_FLOATING, mChannel, mActiveFW); + } + if(pol==HIGHSIDE && mNumHighsideconfigHB(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; iconfigHB(mHbLowside[i], Tle94112::TLE_HIGH, mChannel, mActiveFW); + } + } + else + { + // connect all motor pins to low + for(i = 0; iconfigHB(mHbHighside[i], Tle94112::TLE_LOW, mChannel, mActiveFW); + } + for(i = 0; iconfigHB(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; iconfigHB(mHbHighside[i], Tle94112::TLE_FLOATING, mChannel, mActiveFW); + } + for(i = 0; iconfigHB(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; iconfigHB(mHbHighside[i], Tle94112::TLE_HIGH, mChannel, mActiveFW); + } + for(i = 0; iconfigHB(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; iconfigHB(mHbHighside[i], Tle94112::TLE_LOW, mChannel, mActiveFW); + } + for(i = 0; iconfigHB(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; + } +} diff --git a/src/TLE94112_Motor.h b/src/TLE94112_Motor.h new file mode 100644 index 00000000..71719952 --- /dev/null +++ b/src/TLE94112_Motor.h @@ -0,0 +1,53 @@ +#ifndef TLE94112_MOTOR_H +#define TLE94112_MOTOR_H + +#include "TLE94112.h" + +#define TLE94112_CONNECTOR_SIZE 6 + +class Tle94112Motor +{ +public: + + enum eMode + { + COAST, + FORWARD, + BACKWARD, + STOP + }; + enum ePolarity + { + LOWSIDE, + HIGHSIDE + }; + + Tle94112Motor(Tle94112 &driver, Tle94112::PWMChannel channel); + ~Tle94112Motor(); + void begin(void); + void end(void); + + void connect(Tle94112::HalfBridge connector, ePolarity pol); + void setPwmFreq(Tle94112::PWMFreq freq); + void stop(uint8_t force=255); + void coast(); + void start(int16_t speed); + void setSpeed(int16_t speed); + int16_t getSpeed(void); + +private: + Tle94112 *mDriver; + Tle94112::PWMChannel mChannel; + Tle94112::PWMFreq mPWMfreq; + uint8_t mNumHighside; + uint8_t mNumLowside; + Tle94112::HalfBridge mHbHighside[TLE94112_CONNECTOR_SIZE]; + Tle94112::HalfBridge mHbLowside[TLE94112_CONNECTOR_SIZE]; + uint8_t mActiveFW; + eMode mMode; + uint8_t mSpeed; + uint8_t mEnabled; + +}; + +#endif \ No newline at end of file