Data Structures¶
Let's play with data structures
MPU Data Logger (1p)¶
You are tasked with processing and printing accelerometer data from an MPU (Motion Processing Unit). The MPU records various data, including acceleration, gyroscope readings, and status information. The status can indicate if the data is valid, if the MPU is in low-energy mode, or if there was an error.
The data is provided as an array of structs
mpu_sample_t
, where each sample contains:struct accelerometer_t {
float ax, ay, az;
};
struct gyroscope_t {
float gx, gy, gz;
};
struct mpu_status_t {
int status_code; // Status of the MPU
int battery_level; // Battery level as a percentage
};
struct mpu_sample_t {
uint32_t timestamp;
struct accelerometer_t accel; // Accelerometer data (ax, ay, az)
struct gyroscope_t gyro; // Gyroscope data (gx, gy, gz)
struct mpu_status_t status; // Status information
};
Where
-
-
-
ax
, ay
, and az
represent the accelerometer data, gx
, gy
, and gz
represent the gyroscope data, and status_code
represents the current status of the MPU:-
DATA_VALID
= 2-
LOW_ENERGY
= 1-
DATA_ERROR
= 0Your function does not return any value but prints the values of timestamp, ax, ay, and az when the
status_code
is DATA_VALID
.The printed format is as follows:
timestamp,ax,ay,az
Timestamp
is printed as long integer, while ax, ay, az
as two decimals' float. The function prototype is:
void printcsv(struct mpu_sample_t *samples, int size);
The function will print data for samples where the
status_code
is DATA_VALID
, that is, the value must be 2.An example of the input array of samples:
struct mpu_sample_t samples[5] = {
{1695046347, {0.98, -0.43, 0.12}, {0.01, 0.02, 0.03}, {2, 85}},
{1695046350, {1.02, -0.45, 0.14}, {0.02, 0.03, 0.04}, {1, 90}},
{1695046353, {0.99, -0.40, 0.13}, {0.03, 0.04, 0.05}, {2, 75}},
{1695046356, {1.01, -0.42, 0.15}, {0.05, 0.06, 0.07}, {0, 50}},
{1695046359, {1.03, -0.41, 0.16}, {0.07, 0.08, 0.09}, {1, 60}}
};
The output for this array would be:
1695046347,0.98,-0.43,0.12 1695046353,0.99,-0.40,0.13
Hint. Pay attention to the struct and array handling in C. Only print data for samples where
status_code
is DATA_VALID
.
Hints
Messages
Path calculator (2p)¶
Calculate the distance traveled by an object that moves between points on a path.
The points and a path is given as structs
point
and path
. *points
is a pointer to a singly linked list, which means that each struct point
points to the next point, and the end of the list is signified by a NULL
value.struct point {
int coordinates[3];
struct point *next
};
struct path {
double distance;
struct point *points;
};
The being travels along straight lines through an ideal Euclidean space, so the distance between points is calculated using the Euclidean distance. Every point is a represented as a three dimensional vector.
An example for calculating the path length:
\text{Let there be three points}
a_1 = (1, 2, 3), a_2 = (3, 4, 6)\;\text{and}\;a_3 = (4, 4, 4).
\text{Calculate the distance between subsequent points:}
\begin{aligned}
d(a_1, a_2) &= \sqrt{(a_{11} - a_{21})^2 + (a_{12} - a_{22})^2 + (a_{13} - a_{23})^2}\\
&= \sqrt{(1 - 3)^2 + (2 - 4)^2 + (3 - 6)^2} = \sqrt{4 + 4 + 9} = \sqrt{17}
\end{aligned}
\begin{aligned}
d(a_2, a_3) &= \sqrt{(a_{21} - a_{31})^2 + (a_{22} - a_{32})^2 + (a_{23} - a_{33})^2}\\
&= \sqrt{(3 - 4)^2 + (4 - 4)^2 + (6 - 4)^2} = \sqrt{1 + 0 + 4} = \sqrt{5}
\end{aligned}
\text{Now the length of the path }a_1 \rightarrow a_2 \rightarrow a_3 \text{ is}
d(a_1, a_2) + d(a_2, a_3) = \sqrt{17} + \sqrt{5}.
Use the prototype
void calculate_path_distance(struct path *path);
and write the result to the path
member variable distance
.Hint. Now the function argument is a pointer (not an array). Check how to address the member variables.
Hint. Square roots can be calculated using the
Hint. Square roots can be calculated using the
math.h
library function sqrt().
Hints
Messages
Give feedback on this content
Was this task useful for learning?
Comments about the task?
Give feedback on this content
Comments about the task?