Skip to content

Commit

Permalink
updated code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
djaenicke committed Dec 21, 2020
1 parent f657a7c commit 24f1076
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 66 deletions.
108 changes: 66 additions & 42 deletions tb6612.cpp
@@ -1,52 +1,68 @@
#include "tb6612.h"

namespace tb6612 {

TB6612::TB6612(PinName pwm_a, PinName pwm_b, PinName a_in1, \
PinName a_in2, PinName b_in1, PinName b_in2,
Motor_Polarity_T a_polarity, Motor_Polarity_T b_polarity):
pwm_a_(pwm_a), pwm_b_(pwm_b), a_in1_(a_in1),
a_in2_(a_in2), b_in1_(b_in1), b_in2_(b_in2) {
namespace tb6612
{
TB6612::TB6612(PinName pwm_a, PinName pwm_b, PinName a_in1, PinName a_in2, PinName b_in1, PinName b_in2,
MotorPolarity a_polarity, MotorPolarity b_polarity)
: pwm_a_(pwm_a), pwm_b_(pwm_b), a_in1_(a_in1), a_in2_(a_in2), b_in1_(b_in1), b_in2_(b_in2)
{
a_polarity_ = a_polarity;
b_polarity_ = b_polarity;
}

void TB6612::SetPWMPeriod(float period_s) {
pwm_a_.period(period_s);
pwm_b_.period(period_s);
void TB6612::setPWMPeriod(float period_s)
{
pwm_a_.period(period_s);
pwm_b_.period(period_s);
}

Direction_T TB6612:: HandlePolarity(Motor_Polarity_T polarity, Direction_T new_dir) {
Direction_T ret_val;
Direction TB6612::handlePolarity(MotorPolarity polarity, Direction new_dir)
{
Direction ret_val;

if (STANDARD == polarity) {
if (STANDARD == polarity)
{
ret_val = new_dir;
} else if (FORWARD == new_dir) {
}
else if (FORWARD == new_dir)
{
ret_val = REVERSE;
} else if (REVERSE == new_dir) {
}
else if (REVERSE == new_dir)
{
ret_val = FORWARD;
} else {
}
else
{
MBED_ASSERT(false);
}

return ret_val;
}

void TB6612::SetDirection(Motor_Id_T motor, Direction_T new_dir) {
Direction_T dir;
void TB6612::setDirection(MotorId motor, Direction new_dir)
{
Direction dir;

if (MOTOR_A == motor) {
dir = HandlePolarity(a_polarity_, new_dir);
if (MOTOR_A == motor)
{
dir = handlePolarity(a_polarity_, new_dir);
motor_a_dir_ = new_dir;
} else if (MOTOR_B == motor) {
dir = HandlePolarity(b_polarity_, new_dir);
}
else if (MOTOR_B == motor)
{
dir = handlePolarity(b_polarity_, new_dir);
motor_b_dir_ = new_dir;
} else {
}
else
{
MBED_ASSERT(false);
}
switch (dir) {
switch (dir)
{
case FORWARD:
switch (motor) {
switch (motor)
{
case MOTOR_A:
a_in1_ = 1;
a_in2_ = 0;
Expand All @@ -60,7 +76,8 @@ void TB6612::SetDirection(Motor_Id_T motor, Direction_T new_dir) {
}
break;
case REVERSE:
switch (motor) {
switch (motor)
{
case MOTOR_A:
a_in1_ = 0;
a_in2_ = 1;
Expand All @@ -73,15 +90,17 @@ void TB6612::SetDirection(Motor_Id_T motor, Direction_T new_dir) {
MBED_ASSERT(false);
}
break;
default:
MBED_ASSERT(false);
default:
MBED_ASSERT(false);
}
}

Direction_T TB6612::GetDirection(Motor_Id_T motor) {
Direction_T dir;
Direction TB6612::getDirection(MotorId motor)
{
Direction dir;

switch (motor) {
switch (motor)
{
case MOTOR_A:
dir = motor_a_dir_;
break;
Expand All @@ -95,21 +114,25 @@ Direction_T TB6612::GetDirection(Motor_Id_T motor) {
return (dir);
}

void TB6612::SetDC(Motor_Id_T motor, uint8_t percent) {
switch (motor) {
void TB6612::setDutyCycle(MotorId motor, uint8_t percent)
{
switch (motor)
{
case MOTOR_A:
pwm_a_ = percent/100.0f;
pwm_a_ = percent / 100.0f;
break;
case MOTOR_B:
pwm_b_ = percent/100.0f;
pwm_b_ = percent / 100.0f;
break;
default:
MBED_ASSERT(false);
}
}

void TB6612::Stop(Motor_Id_T motor) {
switch (motor) {
void TB6612::stop(MotorId motor)
{
switch (motor)
{
case MOTOR_A:
a_in1_ = 0;
a_in2_ = 0;
Expand All @@ -123,11 +146,12 @@ void TB6612::Stop(Motor_Id_T motor) {
}
}

void TB6612::Freewheel(void) {
Stop(MOTOR_A);
Stop(MOTOR_B);
SetDC(MOTOR_A, 100);
SetDC(MOTOR_B, 100);
void TB6612::freewheel(void)
{
stop(MOTOR_A);
stop(MOTOR_B);
setDutyCycle(MOTOR_A, 100);
setDutyCycle(MOTOR_B, 100);
}

} // namespace tb6612
51 changes: 27 additions & 24 deletions tb6612.h
Expand Up @@ -3,51 +3,54 @@

#include "mbed.h"

namespace tb6612 {

namespace tb6612
{
static const float vdrop = 0.1;

typedef enum {
typedef enum
{
REVERSE = -1,
UNKNOWN_DIR = 0,
FORWARD = 1
} Direction_T;
} Direction;

typedef enum {
typedef enum
{
MOTOR_A = 0,
MOTOR_B
} Motor_Id_T;
} MotorId;

typedef enum {
typedef enum
{
REVERSED = -1,
STANDARD = 1
} Motor_Polarity_T;
} MotorPolarity;

class TB6612 {
class TB6612
{
public:
TB6612(PinName pwm_a, PinName pwm_b, PinName a_in1, \
PinName a_in2, PinName b_in1, PinName b_in2,
Motor_Polarity_T a_polarity, Motor_Polarity_T b_polarity);
void SetPWMPeriod(float period_s);
void SetDirection(Motor_Id_T motor, Direction_T new_dir);
Direction_T GetDirection(Motor_Id_T motor);
void SetDC(Motor_Id_T motor, uint8_t percent);
void Stop(Motor_Id_T motor);
void Freewheel(void);

TB6612(PinName pwm_a, PinName pwm_b, PinName a_in1, PinName a_in2, PinName b_in1, PinName b_in2,
MotorPolarity a_polarity, MotorPolarity b_polarity);
void setPWMPeriod(float period_s);
void setDirection(MotorId motor, Direction new_dir);
Direction getDirection(MotorId motor);
void setDutyCycle(MotorId motor, uint8_t percent);
void stop(MotorId motor);
void freewheel(void);

private:
Direction_T HandlePolarity(Motor_Polarity_T polarity, Direction_T new_dir);
Direction handlePolarity(MotorPolarity polarity, Direction new_dir);

DigitalOut a_in1_;
DigitalOut a_in2_;
DigitalOut b_in1_;
DigitalOut b_in2_;
PwmOut pwm_a_;
PwmOut pwm_b_;
Motor_Polarity_T a_polarity_;
Motor_Polarity_T b_polarity_;
Direction_T motor_a_dir_;
Direction_T motor_b_dir_;
MotorPolarity a_polarity_;
MotorPolarity b_polarity_;
Direction motor_a_dir_;
Direction motor_b_dir_;
};

} // namespace tb6612
Expand Down

0 comments on commit 24f1076

Please sign in to comment.