libyggdrasil  v1.0.0
motor_driver.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Yggdrasil - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 #include <cpp/common/types.hpp>
30 #include <cpp/common/utils.hpp>
31 
32 #include <cstdlib>
33 #include <cstdio>
34 
35 
36 namespace bsp::ygg::prph {
37 
41  class MotorDriver {
42  public:
43  MotorDriver() = delete;
44 
48  enum class Channel : u8 {
49  A = 0,
50  B = 1,
51  };
52 
59  static bool init() {
60  TC78Mode = true; // Set the mode as dual DC motor driver
61  core::delay(1);
62  TC78Stby = false; // Enables the motor driver
63 
64  bsp::TimerBCHA.startPwm();
65  bsp::TimerBCHB.startPwm();
66  bsp::TimerBCHC.startPwm();
67  bsp::TimerBCHD.startPwm();
68 
69  return true;
70  }
71 
77  static void standby(bool stby) {
78  if(stby) {
79  TC78Stby = true; // Set the driver to standby
80 
81  bsp::TimerBCHA.stopPwm();
82  bsp::TimerBCHB.stopPwm();
83  bsp::TimerBCHC.stopPwm();
84  bsp::TimerBCHD.stopPwm();
85  }
86  else {
87  TC78Stby = false; // Wake the motor driver
88 
89  bsp::TimerBCHA.startPwm();
90  bsp::TimerBCHB.startPwm();
91  bsp::TimerBCHC.startPwm();
92  bsp::TimerBCHD.startPwm();
93  }
94  }
95 
96 
103  static void setSpeed(Channel ch, float speed) {
104  bool rotation = speed > 0; // Get the rotation for the selected channel
105  speed = std::abs(speed); // Get the absolute duty for the selected channel
106 
107  switch(ch){
108  case Channel::A:
109  TimerBCHD.setPolarityHigh(rotation); // Set the rotation fpr channel A
110  TimerBCHB.setDutyCycle(speed); // Set the pwm for channel A
111  break;
112  case Channel::B:
113  TimerBCHA.setPolarityHigh(rotation); // Set the rotation fpr channel A
114  TimerBCHC.setDutyCycle(speed); // Set the pwm for channel B
115  break;
116 
117  }
118  }
119 
125  static bool getError() {
126  return TC78Err;
127  }
128 
129  };
130 
131 }
bsp::ygg::prph::MotorDriver::setSpeed
static void setSpeed(Channel ch, float speed)
Controls the speed and rotation of each channel.
Definition: motor_driver.hpp:103
utils.hpp
Commonly used helper functions.
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::ygg::prph::MotorDriver::Channel::A
@ A
Channel A.
types.hpp
Commonly used type definitions and helper templates.
bsp::ygg::prph::MotorDriver
Dual DC Motor driver driver TC78H660FTG.
Definition: motor_driver.hpp:41
bsp::ygg::prph::MotorDriver::standby
static void standby(bool stby)
set the motor driver to standby
Definition: motor_driver.hpp:77
bsp::ygg::prph::MotorDriver::MotorDriver
MotorDriver()=delete
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::ygg::prph::MotorDriver::Channel::B
@ B
Channel B.
bsp::ygg::prph::MotorDriver::Channel
Channel
Channels.
Definition: motor_driver.hpp:48
bsp::ygg::prph::MotorDriver::getError
static bool getError()
get the Status from the motor driver
Definition: motor_driver.hpp:125
bsp::core::delay
ALWAYS_INLINE void delay(u32 ms)
Delays execution by a certain number of milliseconds.
Definition: cortex.hpp:39
bsp::ygg::prph
Definition: color_sensor.hpp:32
bsp::ygg::prph::MotorDriver::init
static bool init()
init function for the motor as a dual channel dc driver
Definition: motor_driver.hpp:59