Skip to content

Commit 9bf2033

Browse files
committed
Fix: Prevent sample array (values) from overflowing.
1 parent 7070733 commit 9bf2033

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

examples/Braccio_Learn_and_Repeat/Braccio_Learn_and_Repeat.ino

+17-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ enum states {
1313

1414
int state = ZERO_POSITION;
1515

16-
float values[10000];
16+
static int const MAX_SAMPLES = 6*1000*2; /* 20 seconds. */
17+
18+
float values[MAX_SAMPLES];
1719
float* idx = values;
1820
float* final_idx = 0;
21+
int sample_cnt = 0;
1922
float homePos[6] = {157.5, 157.5, 157.5, 157.5, 157.5, 90.0};
2023

2124
static lv_obj_t * counter;
@@ -42,6 +45,7 @@ static void eventHandlerMenu(lv_event_t * e) {
4245
}
4346

4447
idx = values;
48+
sample_cnt = 0;
4549

4650
switch (id) {
4751
case 0: // if the button pressed is the first one
@@ -141,25 +145,32 @@ void setup() {
141145

142146
void loop() {
143147
if (state == RECORD) {
148+
149+
/* Check if we still have space for samples. */
150+
if (sample_cnt >= MAX_SAMPLES) {
151+
state = ZERO_POSITION;
152+
Serial.println("ZERO_POSITION");
153+
btnm_map[0] = "RECORD"; // reset the label of the first button back to "RECORD"
154+
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
155+
}
156+
/* Capture those samples. */
144157
Braccio.positions(idx);
145158
idx += 6;
159+
sample_cnt += 6;
146160
}
147161
if (state == REPLAY) {
148162
Braccio.moveTo(idx[0], idx[1], idx[2], idx[3], idx[4], idx[5]);
149163
idx += 6;
164+
sample_cnt += 6;
150165
if (idx >= final_idx) {
151166
Serial.println("REPLAY done");
152167
state = ZERO_POSITION;
153168
btnm_map[2] = "REPLAY"; // reset the label of the first button back to "REPLAY"
154169
lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED);
155170
}
156171
}
157-
if (idx - values >= sizeof(values)) {
158-
Serial.println("ZERO_POSITION");
159-
state = ZERO_POSITION;
160-
}
161172
delay(100);
162173
if (state != ZERO_POSITION) {
163-
lv_label_set_text_fmt(counter, "Counter: %d" , idx - values);
174+
lv_label_set_text_fmt(counter, "Counter: %d" , sample_cnt);
164175
}
165176
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* @brief Activating the "MOVE" button by pressing
3+
* the joystick enables a waving motion of the arm.
4+
*/
5+
6+
/**************************************************************************************
7+
* INCLUDE
8+
**************************************************************************************/
9+
10+
#include <Braccio++.h>
11+
12+
#include <PacketSerial.h>
13+
14+
/**************************************************************************************
15+
* TYPEDEF
16+
**************************************************************************************/
17+
18+
union Braccio2ROSMessage
19+
{
20+
struct __attribute__((packed, aligned(4)))
21+
{
22+
float joint_position_deg[SmartServoClass::NUM_MOTORS];
23+
} data;
24+
uint8_t buf[24];
25+
};
26+
static_assert(sizeof(Braccio2ROSMessage) == 24);
27+
28+
/**************************************************************************************
29+
* FUNCTION DECLARATION
30+
**************************************************************************************/
31+
32+
void onPacketReceived(const uint8_t* buffer, size_t size);
33+
34+
/**************************************************************************************
35+
* GLOBAL VARIABLES
36+
**************************************************************************************/
37+
38+
SLIPPacketSerial packet_serial;
39+
40+
/**************************************************************************************
41+
* SETUP/LOOP
42+
**************************************************************************************/
43+
44+
void setup()
45+
{
46+
Braccio.begin();
47+
48+
packet_serial.begin(115200);
49+
packet_serial.setPacketHandler(&onPacketReceived);
50+
}
51+
52+
void loop()
53+
{
54+
packet_serial.update();
55+
56+
static auto prev = millis();
57+
if (auto const now = millis(); (now - prev) > 50)
58+
{
59+
prev = now;
60+
Braccio2ROSMessage msg;
61+
Braccio.positions(msg.data.joint_position_deg);
62+
packet_serial.send(msg.buf, sizeof(msg.buf));
63+
}
64+
}
65+
66+
/**************************************************************************************
67+
* FUNCTION DEFINITION
68+
**************************************************************************************/
69+
70+
void onPacketReceived(const uint8_t* buffer, size_t size)
71+
{
72+
/* TODO */
73+
}

0 commit comments

Comments
 (0)