Chassis v2.1.1
Chassisはロボコンでの足回り制御を行うためのC++ライブラリである。
Loading...
Searching...
No Matches
Motor.h
Go to the documentation of this file.
1
2#ifndef CHASSIS_MOTOR_H_
3#define CHASSIS_MOTOR_H_
4
5#ifdef __MBED__
6
7#include <Pwm.h>
8#include <mbed.h>
9
10namespace rct {
11
14struct Motor : mbed::NonCopyable<Motor> {
18 Motor(const PinName pinA, const PinName pinB) : outA_{pinA}, outB_{pinB} {
19 outA_.period_us(100);
20 outB_.period_us(100);
21 }
22
26 void set_pwm(const float power) {
27 pwm_ = power;
28 }
30 void write() {
31 outA_ = pwm_[0];
32 outB_ = pwm_[1];
33 }
35 void print() {
36 printf("%d\t%d\t", int(pwm_[0] * 100), int(pwm_[1] * 100));
37 }
42 float operator=(const float val) {
43 set_pwm(val);
44 write();
45 return val;
46 }
47 private:
48 mbed::PwmOut outA_;
49 mbed::PwmOut outB_;
50 Pwm pwm_ = {};
51};
52
53} // namespace rct
54
55#elif defined(ARDUINO) // __MBED__
56
57namespace rct {
58
60struct Motor {
64 Motor(const int pinA, const int pinB) : pin_{pinA, pinB} {
65 pinMode(pinA, OUTPUT);
66 pinMode(pinB, OUTPUT);
67 }
68 Motor(const Motor&) = delete;
69 Motor(Motor&&) = delete;
72 void set_pwm(const int power) {
73 pwm_[0] = power > 0 ? power : 0;
74 pwm_[1] = power > 0 ? 0 : -power;
75 }
77 void write() {
78 analogWrite(pin_[0], pwm_[0]);
79 analogWrite(pin_[1], pwm_[1]);
80 }
82 void print() {
83 Serial.print(pwm_[0]);
84 Serial.print('\t');
85 Serial.print(pwm_[1]);
86 Serial.print('\t');
87 }
91 int operator=(const int val) {
92 set_pwm(val);
93 write();
94 return val;
95 }
96 private:
97 int pin_[2];
98 uint8_t pwm_[2] = {};
99};
100
101} // namespace rct
102
103#endif // defined(ARDUINO)
104
105#endif // CHASSIS_MOTOR_H_
Pwm クラスを提供する。
robot control library
Definition Chassis.h:16