ArPiRobot-CoreLib C++
C++ library for ArPiRobot robots
AdafruitMotorHatMotor.hpp
1 /*
2  * Copyright 2021 Marcus Behel
3  *
4  * This file is part of ArPiRobot-CoreLib.
5  *
6  * ArPiRobot-CoreLib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * ArPiRobot-CoreLib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with ArPiRobot-CoreLib. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include <arpirobot/core/device/MotorController.hpp>
23 #include <arpirobot/core/io/IoDevice.hpp>
24 
25 #include <cstdint>
26 #include <mutex>
27 #include <memory>
28 #include <unordered_map>
29 #include <vector>
30 
31 namespace arpirobot{
32 
33  namespace internal{
41  class AdafruitMotorHat : public IoDevice{
42  public:
43  // Motor commands
44  enum class MotorCommand {FORWARD, BACKWARD, BRAKE, RELEASE};
45 
46  AdafruitMotorHat(uint8_t address, uint8_t bus = 1);
47  AdafruitMotorHat(const AdafruitMotorHat &other) = delete;
48 
50 
51  AdafruitMotorHat &operator=(const AdafruitMotorHat &other) = delete;
52 
57  public:
58  LowLevelDCMotor(AdafruitMotorHat *hat, int num);
59 
64  void run(MotorCommand cmd);
65 
70  void setSpeed(double speed);
71 
72  private:
73  void kill();
74 
75  uint8_t pwm, in1, in2;
76  bool canRun = true;
77  std::mutex lock;
78  AdafruitMotorHat *hat;
79  };
80 
81  std::shared_ptr<LowLevelDCMotor> getMotor(int index);
82 
83  protected:
84  void close() override;
85 
86  private:
87  void startup();
88 
89 
90  void setPin(uint8_t pin, bool isHigh);
91  void setPWMFreq(int freq);
92  void setPWM(uint8_t channel, int on, int off);
93  void setAllPWM(int on, int off);
94 
95  int handle = -1;
96  std::shared_ptr<LowLevelDCMotor> motors[4];
97 
98  // Registers
99  const uint8_t __MODE1 = 0x00;
100  const uint8_t __MODE2 = 0x01;
101  const uint8_t __SUBADR1 = 0x02;
102  const uint8_t __SUBADR2 = 0x03;
103  const uint8_t __SUBADR3 = 0x04;
104  const uint8_t __PRESCALE = 0xFE;
105  const uint8_t __LED0_ON_L = 0x06;
106  const uint8_t __LED0_ON_H = 0x07;
107  const uint8_t __LED0_OFF_L = 0x08;
108  const uint8_t __LED0_OFF_H = 0x09;
109  const uint8_t __ALL_LED_ON_L = 0xFA;
110  const uint8_t __ALL_LED_ON_H = 0xFB;
111  const uint8_t __ALL_LED_OFF_L = 0xFC;
112  const uint8_t __ALL_LED_OFF_H = 0xFD;
113 
114  // Bits
115  const uint8_t __RESTART = 0x80;
116  const uint8_t __SLEEP = 0x10;
117  const uint8_t __ALLCALL = 0x01;
118  const uint8_t __INVRT = 0x10;
119  const uint8_t __OUTDRV = 0x04;
120  };
121  }
122 
129  public:
130 
131  static const int ADAFRUIT_ADDR;
132  static const int GEEKWORM_ADDR;
133  static const int DETECT_ADDR;
134 
142  AdafruitMotorHatMotor(int motorNum, int address = DETECT_ADDR, int bus = -1, bool remapNumbers = true);
143 
144  AdafruitMotorHatMotor(const AdafruitMotorHatMotor &other) = delete;
145  AdafruitMotorHatMotor &operator=(const AdafruitMotorHatMotor &other) = delete;
146 
147  protected:
148  void begin() override;
149 
150  void run() override;
151 
152  private:
153  static int remapMotorNumber(int hatAddress, int motorNum);
154  static int doDetectAddress(int bus);
155 
156  static std::unordered_map<int, std::shared_ptr<internal::AdafruitMotorHat>> hatMap;
157 
158  int motorNum;
159  int hatAddress;
160  int hatBus;
161  bool remapNumbers;
162  std::shared_ptr<internal::AdafruitMotorHat::LowLevelDCMotor> motor;
163  };
164 
165 }
Definition: AdafruitMotorHatMotor.hpp:128
AdafruitMotorHatMotor(int motorNum, int address=DETECT_ADDR, int bus=-1, bool remapNumbers=true)
Definition: IoDevice.hpp:27
Definition: MotorController.hpp:32
Definition: AdafruitMotorHatMotor.hpp:56
Definition: AdafruitMotorHatMotor.hpp:41
Definition: ArduinoDevice.hpp:27