Skip to content

Commit c49df2e

Browse files
committed
dynamic alloc array, printing data works now
1 parent 0f0acfe commit c49df2e

File tree

2 files changed

+83
-41
lines changed

2 files changed

+83
-41
lines changed

src/MPU6050.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ MPU6050::MPU6050(int sda_pin = 4, int scl_pin = 5) {
44
// 400kHz baud rate
55
i2c_init(i2c_default, 400 * 1000);
66

7-
gpio_set_function(sda_pin, GPIO_FUNC_I2C); // 16
8-
gpio_set_function(scl_pin, GPIO_FUNC_I2C); // 17
7+
gpio_set_function(sda_pin, GPIO_FUNC_I2C);
8+
gpio_set_function(scl_pin, GPIO_FUNC_I2C);
99
gpio_pull_up(sda_pin);
1010
gpio_pull_up(scl_pin);
1111

@@ -69,7 +69,6 @@ acc_3D<uint16_t> MPU6050::get_acc_offset() {
6969
// Reset the MPU6050 and then wake it up
7070
void MPU6050::reset() {
7171
// Two byte reset. First byte register, second byte data
72-
// There are a load more options to set up the device in different ways that could be added here
7372
uint8_t reset[] = {0x6B, 0x80};
7473
i2c_write_blocking(i2c_default, address, reset, 2, false);
7574
sleep_ms(200);

src/main.cpp

Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,84 @@
33
#include "hardware/gpio.h"
44

55
// Arbirtary, the ANN has this many input nodes per acceleration recording
6-
#define MAX_RECORD_LENGTH 83
6+
#define MAX_RECORD_LEN 83
77

8-
bool record;
9-
uint8_t current_recording;
8+
bool record, print_once;
9+
uint8_t curr_row;
1010

1111
// 83 data points, for 3 axes
12-
float recorded_data[MAX_RECORD_LENGTH][3];
12+
acc_3D<float> *rec_data;
1313

14-
// Pins numbers
14+
// Pin numbers for each function,
15+
// set according to wiring
1516
enum Pin {
1617
// buttons
1718
b_send = 13u,
1819
b_record,
1920

21+
// I2C pins
22+
mpu_sda = 16u,
23+
mpu_scl,
24+
2025
// LEDs
21-
26+
2227
};
2328

29+
template<typename T>
30+
void struct_memset(acc_3D<T> *acc, T val, size_t size) {
31+
for (int i = 0; i < size; i++)
32+
acc[i] = {
33+
.x = val,
34+
.y = val,
35+
.z = val
36+
};
37+
}
38+
39+
void print_data() {
40+
printf(" +-------+-------+-------+\n"
41+
" | acc_x | acc_y | acc_z |\n"
42+
" +-------+-------+-------+\n");
43+
for (int row = 0; row < 5; row++)
44+
printf("%2d. |%7.3f|%7.3f|%7.3f|\n", row+1,
45+
rec_data[row].x,
46+
rec_data[row].y,
47+
rec_data[row].z
48+
);
49+
50+
printf(" +-------+-------+-------+\n"
51+
" ... \n"
52+
" +-------+-------+-------+\n");
53+
for (int row = MAX_RECORD_LEN - 5; row < MAX_RECORD_LEN; row++)
54+
printf("%2d. |%7.3f|%7.3f|%7.3f|\n", row+1,
55+
rec_data[row].x,
56+
rec_data[row].y,
57+
rec_data[row].z
58+
);
59+
60+
printf(" +-------+-------+-------+\n");
61+
}
62+
2463

2564
void recording(uint gpio, uint32_t events) {
2665
switch (events) {
2766

2867
case GPIO_IRQ_EDGE_RISE:
2968
printf("Button pressed down\n");
30-
for (int i = 0; i < MAX_RECORD_LENGTH; i++)
31-
recorded_data[i][0] = recorded_data[i][0] =
32-
recorded_data[i][0] = .0f;
33-
current_recording = 0;
69+
70+
struct_memset<float>(rec_data, .0f, MAX_RECORD_LEN);
71+
curr_row = 0;
3472
record = true;
3573
break;
3674

3775
case GPIO_IRQ_EDGE_FALL:
3876
printf("Button released\n");
77+
3978
record = false;
40-
printf("+-------+-------+-------+\n"
41-
"| acc_x | acc_y | acc_z |\n"
42-
"+-------+-------+-------+\n");
43-
for (int row = 10; row < MAX_RECORD_LENGTH; row++) {
44-
printf("|%7.3f|%7.3f|%7.3f|\n", recorded_data[row][0], recorded_data[row][1], recorded_data[row][2]);
45-
// sleep_ms(50);
46-
}
47-
printf("+-------+-------+-------+\n\n");
79+
print_once = true;
4880
break;
4981

5082
default:
51-
printf("Invalid mode of triggering interrupt on pin: %d, event no.: %d", gpio, events);
83+
printf("Invalid mode of triggering interrupt on pin: %d, event no.: %d\n", gpio, events);
5284
break;
5385
}
5486
}
@@ -58,7 +90,7 @@ int main() {
5890
// setup phase
5991
stdio_init_all();
6092
// init MPU6050 library and wake
61-
MPU6050 mpu(16, 17);
93+
MPU6050 mpu(Pin::mpu_sda, Pin::mpu_scl);
6294

6395
// irq setup
6496
gpio_init(Pin::b_record);
@@ -67,31 +99,42 @@ int main() {
6799
gpio_set_irq_enabled_with_callback(Pin::b_record, GPIO_IRQ_EDGE_RISE, true, &recording);
68100
gpio_set_irq_enabled_with_callback(Pin::b_record, GPIO_IRQ_EDGE_FALL, true, &recording);
69101

70-
record = false;
71-
current_recording = 0;
72-
for (int i = 0; i < MAX_RECORD_LENGTH; i++)
73-
recorded_data[i][0] = recorded_data[i][0] =
74-
recorded_data[i][0] = .0f;
102+
record = print_once = false;
103+
curr_row = 0;
104+
rec_data = new acc_3D<float>[MAX_RECORD_LEN];
105+
struct_memset<float>(rec_data, .0f, MAX_RECORD_LEN);
106+
75107

76108
// Job loop
77109
while (1) {
78-
if (current_recording < 83) {
110+
if (rec_data == nullptr) {
111+
printf("Could not allocate struct array\r");
112+
sleep_ms(1000);
113+
continue;
114+
}
115+
116+
if (print_once) {
117+
print_once = false;
118+
print_data();
119+
}
120+
121+
if (curr_row < 83) {
79122
if (record) {
80-
acc_3D<float> read = mpu.read_acceleration();
81-
recorded_data[current_recording][0] = read.x;
82-
recorded_data[current_recording][1] = read.y;
83-
recorded_data[current_recording][2] = read.z;
84-
printf("Accelecration: x: %6.3f y: %6.3f z: %6.3f\n",
85-
recorded_data[current_recording][0],
86-
recorded_data[current_recording][1],
87-
recorded_data[current_recording][2]
88-
);
89-
current_recording++;
123+
rec_data[curr_row] = mpu.read_acceleration();
124+
125+
// Print slow enough to read values
126+
if (curr_row % 5 == 0)
127+
printf("Acceleration: x: %6.3f y: %6.3f z: %6.3f\r",
128+
rec_data[curr_row].x,
129+
rec_data[curr_row].y,
130+
rec_data[curr_row].z
131+
);
132+
curr_row++;
90133
}
91-
} else {
134+
} else if (record) {
92135
printf("Overflow!!!\n");
93136
}
94137

95-
sleep_ms(50);
138+
sleep_ms(20);
96139
}
97140
}

0 commit comments

Comments
 (0)