|
23 | 23 | extern const lv_img_dsc_t img_bulb_gif; |
24 | 24 |
|
25 | 25 | extern "C" { |
26 | | - void braccio_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p); |
27 | | - void read_keypad(lv_indev_drv_t * indev, lv_indev_data_t * data); |
| 26 | + void braccio_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p); |
| 27 | + void read_keypad(lv_indev_drv_t * indev, lv_indev_data_t * data); |
28 | 28 | }; |
29 | 29 |
|
30 | 30 | enum speed_grade_t { |
31 | | - FAST = 10, |
32 | | - MEDIUM = 100, |
33 | | - SLOW = 1000, |
| 31 | + FAST = 10, |
| 32 | + MEDIUM = 100, |
| 33 | + SLOW = 1000, |
34 | 34 | }; |
35 | 35 |
|
36 | 36 | #include <chrono> |
37 | 37 | using namespace std::chrono; |
38 | 38 |
|
39 | 39 | class MotorsWrapper { |
40 | 40 | public: |
41 | | - MotorsWrapper(SmartServoClass & servos, int idx) : _servos(servos), _idx(idx) {} |
42 | | - MotorsWrapper& to(float angle) { |
43 | | - _servos.setPosition(_idx, angle, _speed); |
44 | | - return *this; |
45 | | - } |
46 | | - MotorsWrapper& in(std::chrono::milliseconds len) { |
47 | | - _servos.setTime(_idx, len.count()); |
48 | | - return *this; |
49 | | - } |
50 | | - MotorsWrapper& move() { |
51 | | - return *this; |
52 | | - } |
53 | | - float position() { |
54 | | - return _servos.getPosition(_idx); |
55 | | - } |
56 | | - bool connected() { |
57 | | - return _servos.ping(_idx) == 0; |
58 | | - } |
59 | | - operator bool() { |
60 | | - return connected(); |
61 | | - } |
62 | | - void info(Stream& stream) { |
63 | | - _servos.getInfo(stream, _idx); |
64 | | - } |
65 | | - void disengage() { |
66 | | - _servos.disengage(_idx); |
67 | | - } |
68 | | - void engage() { |
69 | | - _servos.engage(_idx); |
70 | | - } |
71 | | - bool engaged() { |
72 | | - return _servos.isEngaged(_idx); |
73 | | - } |
| 41 | + MotorsWrapper(SmartServoClass & servos, int idx) : _servos(servos), _idx(idx) {} |
| 42 | + MotorsWrapper& to(float angle) { |
| 43 | + _servos.setPosition(_idx, angle, _speed); |
| 44 | + return *this; |
| 45 | + } |
| 46 | + MotorsWrapper& in(std::chrono::milliseconds len) { |
| 47 | + _servos.setTime(_idx, len.count()); |
| 48 | + return *this; |
| 49 | + } |
| 50 | + MotorsWrapper& move() { |
| 51 | + return *this; |
| 52 | + } |
| 53 | + float position() { |
| 54 | + return _servos.getPosition(_idx); |
| 55 | + } |
| 56 | + bool connected() { |
| 57 | + return _servos.ping(_idx) == 0; |
| 58 | + } |
| 59 | + operator bool() { |
| 60 | + return connected(); |
| 61 | + } |
| 62 | + void info(Stream& stream) { |
| 63 | + _servos.getInfo(stream, _idx); |
| 64 | + } |
| 65 | + void disengage() { |
| 66 | + _servos.disengage(_idx); |
| 67 | + } |
| 68 | + void engage() { |
| 69 | + _servos.engage(_idx); |
| 70 | + } |
| 71 | + bool engaged() { |
| 72 | + return _servos.isEngaged(_idx); |
| 73 | + } |
74 | 74 |
|
75 | 75 | private: |
76 | | - SmartServoClass & _servos; |
77 | | - int _idx; |
78 | | - int _speed = 100; |
| 76 | + SmartServoClass & _servos; |
| 77 | + int _idx; |
| 78 | + int _speed = 100; |
79 | 79 | }; |
80 | 80 |
|
81 | 81 | class BraccioClass |
82 | 82 | { |
83 | 83 | public: |
84 | 84 |
|
85 | | - BraccioClass(); |
86 | | - |
87 | | - bool begin(voidFuncPtr customMenu = nullptr); |
88 | | - |
89 | | - // setters |
90 | | - MotorsWrapper move(int joint_index) { |
91 | | - MotorsWrapper wrapper(servos, joint_index); |
92 | | - return wrapper; |
93 | | - } |
94 | | - MotorsWrapper get(int joint_index) { |
95 | | - return move(joint_index); |
96 | | - } |
97 | | - void moveTo(int joint_index, int position) { |
98 | | - //servos.setPosition(joint_index, position, 100); |
99 | | - } |
100 | | - void moveTo(int joint_index, float angle) { |
101 | | - servos.setPosition(joint_index, angle, 100); |
102 | | - } |
103 | | - void moveTo(float a1, float a2, float a3, float a4, float a5, float a6) { |
104 | | - servos.setPositionMode(PositionMode::SYNC); |
105 | | - servos.setPosition(1, a1, runTime); |
106 | | - servos.setPosition(2, a2, runTime); |
107 | | - servos.setPosition(3, a3, runTime); |
108 | | - servos.setPosition(4, a4, runTime); |
109 | | - servos.setPosition(5, a5, runTime); |
110 | | - servos.setPosition(6, a6, runTime); |
111 | | - servos.synchronize(); |
112 | | - servos.setPositionMode(PositionMode::IMMEDIATE); |
113 | | - } |
114 | | - // getters |
115 | | - void positions(float* buffer) { |
116 | | - for (int i = 1; i < 7; i++) { |
117 | | - *buffer++ = servos.getPosition(i); |
118 | | - } |
119 | | - } |
120 | | - void positions(float& a1, float& a2, float& a3, float& a4, float& a5, float& a6) { |
121 | | - // TODO: add check if motors are actually connected |
122 | | - a1 = servos.getPosition(1); |
123 | | - a2 = servos.getPosition(2); |
124 | | - a3 = servos.getPosition(3); |
125 | | - a4 = servos.getPosition(4); |
126 | | - a5 = servos.getPosition(5); |
127 | | - a6 = servos.getPosition(6); |
128 | | - } |
129 | | - float position(int joint_index); |
130 | | - bool connected(int joint_index) { |
131 | | - return _connected[joint_index]; |
132 | | - } |
133 | | - |
134 | | - void speed(speed_grade_t speed_grade) { |
135 | | - runTime = speed_grade; |
136 | | - } |
137 | | - |
138 | | - void disengage(int id = SmartServoClass::BROADCAST) { |
139 | | - servos.disengage(id); |
140 | | - } |
141 | | - |
142 | | - void engage(int id = SmartServoClass::BROADCAST) { |
143 | | - servos.engage(id); |
144 | | - } |
145 | | - |
146 | | - int getKey(); |
147 | | - void connectJoystickTo(lv_obj_t* obj); |
148 | | - |
149 | | - TFT_eSPI gfx = TFT_eSPI(); |
150 | | - |
151 | | - bool ping_allowed = true; |
152 | | - |
153 | | - static BraccioClass& get_default_instance() { |
154 | | - static BraccioClass dev; |
155 | | - return dev; |
156 | | - } |
| 85 | + BraccioClass(); |
| 86 | + |
| 87 | + bool begin(voidFuncPtr customMenu = nullptr); |
| 88 | + |
| 89 | + // setters |
| 90 | + MotorsWrapper move(int joint_index) { |
| 91 | + MotorsWrapper wrapper(servos, joint_index); |
| 92 | + return wrapper; |
| 93 | + } |
| 94 | + MotorsWrapper get(int joint_index) { |
| 95 | + return move(joint_index); |
| 96 | + } |
| 97 | + void moveTo(int joint_index, int position) { |
| 98 | + //servos.setPosition(joint_index, position, 100); |
| 99 | + } |
| 100 | + void moveTo(int joint_index, float angle) { |
| 101 | + servos.setPosition(joint_index, angle, 100); |
| 102 | + } |
| 103 | + void moveTo(float a1, float a2, float a3, float a4, float a5, float a6) { |
| 104 | + servos.setPositionMode(PositionMode::SYNC); |
| 105 | + servos.setPosition(1, a1, runTime); |
| 106 | + servos.setPosition(2, a2, runTime); |
| 107 | + servos.setPosition(3, a3, runTime); |
| 108 | + servos.setPosition(4, a4, runTime); |
| 109 | + servos.setPosition(5, a5, runTime); |
| 110 | + servos.setPosition(6, a6, runTime); |
| 111 | + servos.synchronize(); |
| 112 | + servos.setPositionMode(PositionMode::IMMEDIATE); |
| 113 | + } |
| 114 | + // getters |
| 115 | + void positions(float* buffer) { |
| 116 | + for (int i = 1; i < 7; i++) { |
| 117 | + *buffer++ = servos.getPosition(i); |
| 118 | + } |
| 119 | + } |
| 120 | + void positions(float& a1, float& a2, float& a3, float& a4, float& a5, float& a6) { |
| 121 | + // TODO: add check if motors are actually connected |
| 122 | + a1 = servos.getPosition(1); |
| 123 | + a2 = servos.getPosition(2); |
| 124 | + a3 = servos.getPosition(3); |
| 125 | + a4 = servos.getPosition(4); |
| 126 | + a5 = servos.getPosition(5); |
| 127 | + a6 = servos.getPosition(6); |
| 128 | + } |
| 129 | + float position(int joint_index); |
| 130 | + bool connected(int joint_index) { |
| 131 | + return _connected[joint_index]; |
| 132 | + } |
| 133 | + |
| 134 | + void speed(speed_grade_t speed_grade) { |
| 135 | + runTime = speed_grade; |
| 136 | + } |
| 137 | + |
| 138 | + void disengage(int id = SmartServoClass::BROADCAST) { |
| 139 | + servos.disengage(id); |
| 140 | + } |
| 141 | + |
| 142 | + void engage(int id = SmartServoClass::BROADCAST) { |
| 143 | + servos.engage(id); |
| 144 | + } |
| 145 | + |
| 146 | + int getKey(); |
| 147 | + void connectJoystickTo(lv_obj_t* obj); |
| 148 | + |
| 149 | + TFT_eSPI gfx = TFT_eSPI(); |
| 150 | + |
| 151 | + bool ping_allowed = true; |
| 152 | + |
| 153 | + static BraccioClass& get_default_instance() { |
| 154 | + static BraccioClass dev; |
| 155 | + return dev; |
| 156 | + } |
157 | 157 |
|
158 | 158 | protected: |
159 | | - // ioexpander APIs |
160 | | - void digitalWrite(int pin, uint8_t value); |
| 159 | + // ioexpander APIs |
| 160 | + void digitalWrite(int pin, uint8_t value); |
161 | 161 |
|
162 | | - // default display APIs |
163 | | - void drawMenu(); |
164 | | - void splashScreen(int duration = 1000); |
165 | | - void hideMenu(); |
166 | | - void drawImage(char* image); |
167 | | - void defaultMenu(); |
| 162 | + // default display APIs |
| 163 | + void drawMenu(); |
| 164 | + void splashScreen(int duration = 1000); |
| 165 | + void hideMenu(); |
| 166 | + void drawImage(char* image); |
| 167 | + void defaultMenu(); |
168 | 168 |
|
169 | | - void setID(int id) { |
170 | | - servos.setID(id); |
171 | | - } |
| 169 | + void setID(int id) { |
| 170 | + servos.setID(id); |
| 171 | + } |
172 | 172 |
|
173 | 173 | private: |
174 | 174 |
|
175 | 175 | RS485Class serial485; |
176 | 176 | SmartServoClass servos; |
177 | | - PD_UFP_log_c PD_UFP; |
178 | | - TCA6424A expander; |
179 | | - Backlight bl; |
| 177 | + PD_UFP_log_c PD_UFP; |
| 178 | + TCA6424A expander; |
| 179 | + Backlight bl; |
180 | 180 |
|
181 | | - speed_grade_t runTime; //ms |
| 181 | + speed_grade_t runTime; //ms |
182 | 182 |
|
183 | | - voidFuncPtr _customMenu; |
| 183 | + voidFuncPtr _customMenu; |
184 | 184 |
|
185 | | - const int BTN_LEFT = 3; |
186 | | - const int BTN_RIGHT = 4; |
187 | | - const int BTN_UP = 5; |
188 | | - const int BTN_DOWN = 2; |
189 | | - const int BTN_SEL = A0; |
190 | | - const int BTN_ENTER = A1; |
| 185 | + const int BTN_LEFT = 3; |
| 186 | + const int BTN_RIGHT = 4; |
| 187 | + const int BTN_UP = 5; |
| 188 | + const int BTN_DOWN = 2; |
| 189 | + const int BTN_SEL = A0; |
| 190 | + const int BTN_ENTER = A1; |
191 | 191 |
|
192 | 192 | lv_disp_drv_t disp_drv; |
193 | 193 | lv_indev_drv_t indev_drv; |
194 | | - lv_disp_draw_buf_t disp_buf; |
195 | | - lv_color_t buf[240 * 240 / 10]; |
196 | | - lv_group_t* p_objGroup; |
197 | | - lv_indev_t *kb_indev; |
| 194 | + lv_disp_draw_buf_t disp_buf; |
| 195 | + lv_color_t buf[240 * 240 / 10]; |
| 196 | + lv_group_t* p_objGroup; |
| 197 | + lv_indev_t *kb_indev; |
198 | 198 |
|
199 | 199 | bool _connected[8]; |
200 | 200 |
|
201 | 201 | #ifdef __MBED__ |
202 | | - rtos::EventFlags pd_events; |
203 | | - rtos::Mutex i2c_mutex; |
204 | | - mbed::Ticker pd_timer; |
205 | | - |
206 | | - unsigned int start_pd_burst = 0xFFFFFFFF; |
207 | | - |
208 | | - void unlock_pd_semaphore_irq() { |
209 | | - start_pd_burst = millis(); |
210 | | - pd_events.set(2); |
211 | | - } |
212 | | - |
213 | | - void unlock_pd_semaphore() { |
214 | | - pd_events.set(1); |
215 | | - } |
216 | | - |
217 | | - void setGreen(int i) { |
218 | | - expander.writePin(i * 2 - 1, 0); |
219 | | - expander.writePin(i * 2 - 2, 1); |
220 | | - } |
221 | | - |
222 | | - void setRed(int i) { |
223 | | - expander.writePin(i * 2 - 1, 1); |
224 | | - expander.writePin(i * 2 - 2, 0); |
225 | | - } |
226 | | - |
227 | | - void pd_thread(); |
228 | | - void display_thread(); |
229 | | - void motors_connected_thread(); |
| 202 | + rtos::EventFlags pd_events; |
| 203 | + rtos::Mutex i2c_mutex; |
| 204 | + mbed::Ticker pd_timer; |
| 205 | + |
| 206 | + unsigned int start_pd_burst = 0xFFFFFFFF; |
| 207 | + |
| 208 | + void unlock_pd_semaphore_irq() { |
| 209 | + start_pd_burst = millis(); |
| 210 | + pd_events.set(2); |
| 211 | + } |
| 212 | + |
| 213 | + void unlock_pd_semaphore() { |
| 214 | + pd_events.set(1); |
| 215 | + } |
| 216 | + |
| 217 | + void setGreen(int i) { |
| 218 | + expander.writePin(i * 2 - 1, 0); |
| 219 | + expander.writePin(i * 2 - 2, 1); |
| 220 | + } |
| 221 | + |
| 222 | + void setRed(int i) { |
| 223 | + expander.writePin(i * 2 - 1, 1); |
| 224 | + expander.writePin(i * 2 - 2, 0); |
| 225 | + } |
| 226 | + |
| 227 | + void pd_thread(); |
| 228 | + void display_thread(); |
| 229 | + void motors_connected_thread(); |
230 | 230 | #endif |
231 | 231 |
|
232 | 232 | }; |
|
0 commit comments