RBControl
Library for the RB3201-RBControl board with the ESP32 by RoboticsBrno.
RBControl_arm.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <math.h>
5 #include <memory>
6 #include <stdint.h>
7 #include <vector>
8 
9 #include "RBControl_angle.hpp"
10 
11 namespace rb {
12 
13 class ArmBuilder;
14 class Bone;
15 
16 class Arm {
17  friend class ArmBuilder;
18  friend class Bone;
19 
20 public:
21  typedef int32_t CoordType;
22 
23  struct BoneDefinition {
25  this->servo_id = servo_id;
26  this->length = length;
29  calcServoAng = [](Angle, Angle rel) -> Angle { return rel; };
30  calcAbsAng = [](Angle servoAng) -> Angle { return servoAng; };
31  }
32 
33  uint8_t servo_id;
38 
39  std::function<Angle(Angle, Angle)> calcServoAng;
40  std::function<Angle(Angle)> calcAbsAng;
41  };
42 
43  struct Definition {
45  body_height = 0;
46  body_radius = 0;
47 
48  arm_offset_x = 0;
49  arm_offset_y = 0;
50  }
51 
54 
57 
58  std::vector<BoneDefinition> bones;
59  };
60 
61  static Angle clamp(Angle ang);
62 
63  ~Arm();
64 
65  bool solve(Arm::CoordType target_x, Arm::CoordType target_y);
66  void setServos(float speed = 180.f);
67 
68  const Definition& definition() const { return m_def; }
69  const std::vector<Bone>& bones() const { return m_bones; }
70 
71  bool syncBonesWithServos();
72 
73 private:
74  typedef float AngleType;
75 
76  static AngleType clamp(AngleType ang);
77 
78  Arm(const Definition& def);
79  Arm(const Arm&) = delete;
80 
81  template <typename T = CoordType>
82  static T roundCoord(AngleType val);
83 
84  bool solveIteration(CoordType target_x, CoordType target_y, bool& modified);
85  AngleType rotateArm(size_t idx, AngleType rot_ang);
86  void fixBodyCollision();
87  bool isInBody(CoordType x, CoordType y) const;
88  void updateBones();
89 
90  const Definition m_def;
91  std::vector<Bone> m_bones;
92 };
93 
94 class Bone {
95  friend class Arm;
96 
97 public:
99 
102 
104 
105 private:
106  Bone(const Arm::BoneDefinition& def);
107 
108  void updatePos(Bone* prev);
109 };
110 
111 class ArmBuilder;
112 
113 class BoneBuilder {
114  friend class ArmBuilder;
115 
116 public:
117  BoneBuilder(BoneBuilder&& other);
118  ~BoneBuilder();
119 
120  BoneBuilder& relStops(Angle min, Angle max);
121  BoneBuilder& absStops(Angle min, Angle max);
122  BoneBuilder& baseRelStops(Angle min, Angle max);
123  BoneBuilder& calcServoAng(std::function<Angle(Angle abs, Angle rel)> func);
124  BoneBuilder& calcAbsAng(std::function<Angle(Angle servoAng)> func);
125 
126 private:
127  BoneBuilder(std::shared_ptr<Arm::BoneDefinition> def);
128 
129  std::shared_ptr<Arm::BoneDefinition> m_def;
130 };
131 
132 class ArmBuilder {
133 public:
134  ArmBuilder();
135  ~ArmBuilder();
136 
137  ArmBuilder& body(Arm::CoordType height_mm, Arm::CoordType radius_mm);
139  BoneBuilder bone(uint8_t servo_id, Arm::CoordType length_mm);
140 
141  std::unique_ptr<Arm> build();
142 
143 private:
144  Arm::Definition m_def;
145  std::vector<std::shared_ptr<Arm::BoneDefinition>> m_bones;
146 };
147 
148 }; // namespace rb
Definition: RBControl_angle.hpp:11
static const Angle Pi
Definition: RBControl_angle.hpp:14
Definition: RBControl_arm.hpp:132
ArmBuilder()
Definition: RBControl_arm.cpp:19
ArmBuilder & armOffset(Arm::CoordType x_mm, Arm::CoordType y_mm)
Definition: RBControl_arm.cpp:31
ArmBuilder & body(Arm::CoordType height_mm, Arm::CoordType radius_mm)
Definition: RBControl_arm.cpp:25
~ArmBuilder()
Definition: RBControl_arm.cpp:22
std::unique_ptr< Arm > build()
Definition: RBControl_arm.cpp:43
BoneBuilder bone(uint8_t servo_id, Arm::CoordType length_mm)
Definition: RBControl_arm.cpp:37
Definition: RBControl_arm.hpp:16
void setServos(float speed=180.f)
Definition: RBControl_arm.cpp:131
const Definition & definition() const
Definition: RBControl_arm.hpp:68
bool solve(Arm::CoordType target_x, Arm::CoordType target_y)
Definition: RBControl_arm.cpp:159
bool syncBonesWithServos()
Definition: RBControl_arm.cpp:138
~Arm()
Definition: RBControl_arm.cpp:128
int32_t CoordType
Definition: RBControl_arm.hpp:21
const std::vector< Bone > & bones() const
Definition: RBControl_arm.hpp:69
static Angle clamp(Angle ang)
Definition: RBControl_arm.cpp:116
Definition: RBControl_arm.hpp:113
BoneBuilder & calcAbsAng(std::function< Angle(Angle servoAng)> func)
Definition: RBControl_arm.cpp:86
BoneBuilder & baseRelStops(Angle min, Angle max)
Definition: RBControl_arm.cpp:75
BoneBuilder(BoneBuilder &&other)
Definition: RBControl_arm.cpp:56
BoneBuilder & absStops(Angle min, Angle max)
Definition: RBControl_arm.cpp:69
BoneBuilder & relStops(Angle min, Angle max)
Definition: RBControl_arm.cpp:63
~BoneBuilder()
Definition: RBControl_arm.cpp:60
BoneBuilder & calcServoAng(std::function< Angle(Angle abs, Angle rel)> func)
Definition: RBControl_arm.cpp:81
Definition: RBControl_arm.hpp:94
Arm::CoordType x
Definition: RBControl_arm.hpp:101
Angle servoAng() const
Definition: RBControl_arm.hpp:103
const Arm::BoneDefinition & def
Definition: RBControl_arm.hpp:98
Angle relAngle
Definition: RBControl_arm.hpp:100
Angle absAngle
Definition: RBControl_arm.hpp:100
Arm::CoordType y
Definition: RBControl_arm.hpp:101
The base namespace. Contains some logging functions, too.
Definition: half_duplex_uart.cpp:53
Definition: RBControl_arm.hpp:23
std::function< Angle(Angle)> calcAbsAng
Definition: RBControl_arm.hpp:40
uint8_t servo_id
Definition: RBControl_arm.hpp:33
CoordType length
Definition: RBControl_arm.hpp:34
Angle abs_min
Definition: RBControl_arm.hpp:36
std::function< Angle(Angle, Angle)> calcServoAng
Definition: RBControl_arm.hpp:39
BoneDefinition(uint8_t servo_id, CoordType length)
Definition: RBControl_arm.hpp:24
Angle base_rel_max
Definition: RBControl_arm.hpp:37
Angle rel_min
Definition: RBControl_arm.hpp:35
Angle base_rel_min
Definition: RBControl_arm.hpp:37
Angle abs_max
Definition: RBControl_arm.hpp:36
Angle rel_max
Definition: RBControl_arm.hpp:35
Definition: RBControl_arm.hpp:43
Definition()
Definition: RBControl_arm.hpp:44
CoordType arm_offset_y
Definition: RBControl_arm.hpp:56
CoordType body_radius
Definition: RBControl_arm.hpp:53
std::vector< BoneDefinition > bones
Definition: RBControl_arm.hpp:58
CoordType arm_offset_x
Definition: RBControl_arm.hpp:55
CoordType body_height
Definition: RBControl_arm.hpp:52