Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Functions
LUT.c File Reference
#include "../Inc/Utils/LUT.h"
#include "test.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
Include dependency graph for LUT.c:

Go to the source code of this file.

Functions

float altitude_m_at_pressure_mb (float pressure_mb)
 
void test_uninitialized_table (table *table)
 
void test_initialized_table (table *table)
 
void test_sampling_endpoints (table *table)
 
void test_sampling_reference_point (table *table)
 
void test_sampling_unknown_point (table *table)
 
void lut_main ()
 

Function Documentation

◆ altitude_m_at_pressure_mb()

float altitude_m_at_pressure_mb ( float  pressure_mb)

Definition at line 11 of file LUT.c.

11 {
12 const float SEA_LEVEL_PRESSURE_MB = 1013.25;
13 // TODO Explain how this comes about
14 float result = pow(pressure_mb / SEA_LEVEL_PRESSURE_MB, 1 / 5.255);
15 return 44330 * (1 - result);
16}
Here is the caller graph for this function:

◆ lut_main()

void lut_main ( )

Definition at line 115 of file LUT.c.

115 {
116 table pressure_mb_to_altitude_m;
117 assert(table_init(&pressure_mb_to_altitude_m, 9));
118
119 test_uninitialized_table(&pressure_mb_to_altitude_m);
120
121 // Initializes the table with equally-spaced points using the transfer
122 // function
123 for (unsigned int reference_pressure_mb = 300; reference_pressure_mb <= 1100;
124 reference_pressure_mb += 100) {
126 &pressure_mb_to_altitude_m, reference_pressure_mb,
127 altitude_m_at_pressure_mb(reference_pressure_mb)));
128 }
129
130 test_initialized_table(&pressure_mb_to_altitude_m);
131
132 test_sampling_endpoints(&pressure_mb_to_altitude_m);
133
134 test_sampling_reference_point(&pressure_mb_to_altitude_m);
135
136 test_sampling_unknown_point(&pressure_mb_to_altitude_m);
137}
bool table_init(table *table, uint16_t count)
Initializes the table.
Definition: LUT.c:31
bool table_add_reference_point(table *table, float in, float out)
Adds a reference point to the table.
Definition: LUT.c:79
void test_sampling_reference_point(table *table)
Definition: LUT.c:87
void test_uninitialized_table(table *table)
Definition: LUT.c:20
void test_sampling_endpoints(table *table)
Definition: LUT.c:63
void test_initialized_table(table *table)
Definition: LUT.c:41
float altitude_m_at_pressure_mb(float pressure_mb)
Definition: LUT.c:11
void test_sampling_unknown_point(table *table)
Definition: LUT.c:101
Definition: LUT.h:40
Here is the call graph for this function:

◆ test_initialized_table()

void test_initialized_table ( table table)

Definition at line 41 of file LUT.c.

41 {
42 TEST(initialized_table, {
43 ASSERT(table_is_initialized(table), "table is initalized",
44 "table is uninitialized");
45 ASSERT(table_min_point(table) != NULL, "min is defined",
46 "min is undefined");
48 "min is first point in table", "min is not first point in table");
49 ASSERT(table_max_point(table) != NULL, "max is defined",
50 "max is undefined");
52 "max is last point in table", "max is not last point in table");
54 "cannot sample outside of domain (0 mb)",
55 "can sample outside of domain (0 mb)");
56 ASSERT(table_can_sample(table, 300), "can sample in domain (300 mb)",
57 "cannot sample outside of domain (300 mb)");
58 })
59}
bool table_is_initialized(table *table)
Checks if the table has been fully initialized, with all reference points added.
Definition: LUT.c:53
const point * table_min_point(table *table)
Retrieves the point in the table with the least input value.
Definition: LUT.c:95
bool table_can_sample(table *table, float in)
Checks if the input input is valid for the table.
Definition: LUT.c:107
const point * table_max_point(table *table)
Retrieves the point in the table with the greatest input value.
Definition: LUT.c:101
point points_[TABLE_CAPACITY]
Definition: LUT.h:48
uint16_t count_
Definition: LUT.h:42
#define TEST(NAME, BODY)
Definition: test.h:19
#define ASSERT(BOOL, OK, ERR)
Definition: test.h:27
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_sampling_endpoints()

void test_sampling_endpoints ( table table)

Definition at line 63 of file LUT.c.

63 {
64 float min_reference_pressure_mb = 300;
65 float max_reference_pressure_mb = 1100;
66 float min_reference_altitude_m =
67 altitude_m_at_pressure_mb(min_reference_pressure_mb);
68 float max_reference_altitude_m =
69 altitude_m_at_pressure_mb(max_reference_pressure_mb);
70 float altitude_m = 0.0;
71
72 TEST(sampling_min_reference_point, {
73 ASSERT_OK(table_sample(table, min_reference_pressure_mb, &altitude_m), "sample");
74 ASSERT_EQ(altitude_m, min_reference_altitude_m, "altitude",
75 "reference altitude");
76 })
77
78 TEST(sampling_max_reference_point, {
79 ASSERT_OK(table_sample(table, max_reference_pressure_mb, &altitude_m), "sample");
80 ASSERT_EQ(altitude_m, max_reference_altitude_m, "altitude",
81 "reference altitude");
82 });
83}
bool table_sample(table *table, float in, float *out)
Samples a output value from the table for an input value.
Definition: LUT.c:137
#define ASSERT_OK(BOOL, LABEL)
Definition: test.h:29
#define ASSERT_EQ(GOT, WANT, GOT_LABEL, WANT_LABEL)
Definition: test.h:32
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_sampling_reference_point()

void test_sampling_reference_point ( table table)

Definition at line 87 of file LUT.c.

87 {
88 float reference_pressure_mb = 600;
89 float reference_altitude_m = altitude_m_at_pressure_mb(reference_pressure_mb);
90 float altitude_m = 0.0;
91
92 TEST(sampling_reference_point, {
93 ASSERT_OK(table_sample(table, reference_pressure_mb, &altitude_m), "sample");
94 ASSERT_EQ(altitude_m, reference_altitude_m, "altitude",
95 "reference altitude");
96 })
97}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_sampling_unknown_point()

void test_sampling_unknown_point ( table table)

Definition at line 101 of file LUT.c.

101 {
102 float pressure_mb = 650;
103 float expected_altitude_m = altitude_m_at_pressure_mb(pressure_mb);
104 float altitude_m = 0.0;
105
106 TEST(sampling_unknown_point, {
107 ASSERT_OK(table_sample(table, pressure_mb, &altitude_m), "sample");
108 float percent_error =
109 (altitude_m - expected_altitude_m) / expected_altitude_m * 100;
110 ASSERT_IN_ERROR(altitude_m, expected_altitude_m, "altitude",
111 "expected altitude");
112 })
113}
#define ASSERT_IN_ERROR(GOT, WANT, GOT_LABEL, WANT_LABEL)
Definition: test.h:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_uninitialized_table()

void test_uninitialized_table ( table table)

Definition at line 20 of file LUT.c.

20 {
21 TEST(uninitialized_table, {
22 ASSERT(!table_is_initialized(table), "table is uninitialized",
23 "table is initialized");
24 ASSERT(table_min_point(table) == NULL, "min is undefined",
25 "min is defined");
26 ASSERT(table_max_point(table) == NULL, "max is undefined",
27 "max is defined");
29 "cannot sample outside of domain (0 mb)",
30 "can sample outside of domain (0 mb)");
31 ASSERT(!table_can_sample(table, 300), "cannot sample in domain (300 mb)",
32 "can sample outside of domain (300 mb)");
33 float altitude_m;
34 ASSERT(!table_sample(table, 300, &altitude_m), "sampling is not okay",
35 "sampling is okay");
36 })
37}
Here is the call graph for this function:
Here is the caller graph for this function: