Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Classes | Macros | Functions
LUT.h File Reference
#include <stdbool.h>
#include <stdint.h>
Include dependency graph for LUT.h:

Go to the source code of this file.

Classes

struct  point
 
struct  table
 

Macros

#define TABLE_CAPACITY   64
 

Functions

int point_compare (const void *a, const void *b)
 Compares two points by input. More...
 
bool point_is_between (const point *min, const point *max, float in)
 Determines if the input value is between the input of two points. More...
 
bool table_init (table *table, uint16_t count)
 Initializes the table. More...
 
bool table_add_reference_point (table *table, float in, float out)
 Adds a reference point to the table. More...
 
bool table_is_initialized (table *table)
 Checks if the table has been fully initialized, with all reference points added. More...
 
const pointtable_min_point (table *table)
 Retrieves the point in the table with the least input value. More...
 
const pointtable_max_point (table *table)
 Retrieves the point in the table with the greatest input value. More...
 
bool table_can_sample (table *table, float in)
 Checks if the input input is valid for the table. More...
 
bool table_sample (table *table, float in, float *out)
 Samples a output value from the table for an input value. More...
 

Macro Definition Documentation

◆ TABLE_CAPACITY

#define TABLE_CAPACITY   64

Definition at line 36 of file LUT.h.

Function Documentation

◆ point_compare()

int point_compare ( const void *  a,
const void *  b 
)

Compares two points by input.

Parameters
aThe first point to compare.
bThe second point to compare.
Returns
A negative integer if the first point is before the second point, zero if the first point is equal to the second point, or a positive integer if the first point is greater than the second point.

Definition at line 6 of file LUT.c.

6 {
7 const point *pa = a;
8 const point *pb = b;
9 if (pa->input < pb->input)
10 return -1;
11 if (pa->input > pb->input)
12 return 1;
13 return 0;
14}
Definition: LUT.h:7
float input
Definition: LUT.h:9
Here is the caller graph for this function:

◆ point_is_between()

bool point_is_between ( const point min,
const point max,
float  in 
)

Determines if the input value is between the input of two points.

Parameters
minA point with an input less than the input of max.
maxA point with an input greater than the input of min.
inAn input value to test with the two points.
Returns
True if the input value is between the minimum input and maximum input.

Definition at line 16 of file LUT.c.

16 {
17 return min->input <= in && in <= max->input;
18}
Here is the caller graph for this function:

◆ table_add_reference_point()

bool table_add_reference_point ( table table,
float  in,
float  out 
)

Adds a reference point to the table.

Parameters
tableA pointer to the table to add the point to.
inThe input ("from") value.
outThe output ("to") value at the input value.
Returns
True if the reference point was added to the table.

Definition at line 79 of file LUT.c.

79 {
81 return false;
82 }
83
84 // NOTE This may should not actually be necessary; the only way to get here is
85 // if the table is not initialized already; there may be a lurking logic error
86 table->initialized_ = false;
87
90 table->added_++;
91
92 return true;
93}
bool table_is_initialized(table *table)
Checks if the table has been fully initialized, with all reference points added.
Definition: LUT.c:53
float output
Definition: LUT.h:11
Definition: LUT.h:40
point points_[TABLE_CAPACITY]
Definition: LUT.h:48
bool initialized_
Definition: LUT.h:46
uint16_t added_
Definition: LUT.h:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ table_can_sample()

bool table_can_sample ( table table,
float  in 
)

Checks if the input input is valid for the table.

Parameters
tableA pointer to the table to check.
inThe input value to check.
Returns
True if the input value is valid for the table.

Definition at line 107 of file LUT.c.

107 {
108 const point *min = table_min_point(table);
109 const point *max = table_max_point(table);
110 return can_interpolate(min, max, in);
111}
const point * table_min_point(table *table)
Retrieves the point in the table with the least input value.
Definition: LUT.c:95
bool can_interpolate(const point *min, const point *max, float in)
Definition: LUT.c:21
const point * table_max_point(table *table)
Retrieves the point in the table with the greatest input value.
Definition: LUT.c:101
Here is the call graph for this function:
Here is the caller graph for this function:

◆ table_init()

bool table_init ( table table,
uint16_t  count 
)

Initializes the table.

Parameters
tableA pointer to the table to initialize.
countThe number of reference points in the table.
Returns
True if the table was successfully initialized.

Definition at line 31 of file LUT.c.

31 {
32 if (table == NULL) {
33 return false;
34 }
35
36 // Table must contain at least two reference points
37 if (count < 2 || count > TABLE_CAPACITY) {
38 return false;
39 }
40
41 // Set up table values to expected values
42 table->count_ = count;
43 table->added_ = 0;
44 table->initialized_ = false;
45 for (uint16_t n = 0; n < count; ++n) {
46 table->points_[n].input = 0;
47 table->points_[n].output = 0;
48 }
49
50 return true;
51}
#define TABLE_CAPACITY
Definition: LUT.h:36
uint16_t count_
Definition: LUT.h:42
Here is the caller graph for this function:

◆ table_is_initialized()

bool table_is_initialized ( table table)

Checks if the table has been fully initialized, with all reference points added.

Parameters
tableA pointer to the table to check.
Returns
True if the table has been fully initialized.

Definition at line 53 of file LUT.c.

53 {
54 // Nothing has invalidated the initialized state of the table
55 if (table->initialized_) {
56 return true;
57 }
58
59 bool all_reference_points = table->added_ == table->count_;
60
61 if (!all_reference_points) {
62 table->initialized_ = false;
63 return table->initialized_;
64 }
65
66 bool sorted = true;
67 const point *previous_point = &table->points_[0];
68 for (uint16_t n = 1; n < table->count_; ++n) {
69 const point *point = &table->points_[n];
70 // Found a decrease, which violates table being sorted
71 if (point->input < previous_point->input) {
72 sorted = false;
73 }
74 }
75 table->initialized_ = sorted;
76 return table->initialized_;
77}
Here is the caller graph for this function:

◆ table_max_point()

const point * table_max_point ( table table)

Retrieves the point in the table with the greatest input value.

Parameters
tableA pointer to the table.
Returns
If there is a maximum point, a pointer to the maximum point, otherwise NULL.

Definition at line 101 of file LUT.c.

101 {
103 return NULL;
104 return &table->points_[table->count_ - 1];
105}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ table_min_point()

const point * table_min_point ( table table)

Retrieves the point in the table with the least input value.

Parameters
tableA pointer to the table.
Returns
If there is a minimum point, a pointer to the minimum point, otherwise NULL.

Definition at line 95 of file LUT.c.

95 {
97 return NULL;
98 return &table->points_[0];
99}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ table_sample()

bool table_sample ( table table,
float  in,
float *  out 
)

Samples a output value from the table for an input value.

Parameters
tableA pointer to the table to sample.
inThe input value to sample.
outA pointer to the destination of the output value.
Returns
True if the output value was written, otherwise false.

Definition at line 137 of file LUT.c.

137 {
138 // This is where the constraint that the table contains at least two elements
139 // is derived from
140 const point *min = table_min_point(table);
141 const point *max = table_max_point(table);
142
143 // If can't interpolate between the endpoints, can't interpolate between
144 // intermediate points
145 if (!can_interpolate(min, max, in)) {
146 return false;
147 }
148
149 if (in == min->input) {
150 *out = min->output;
151 return true;
152 }
153
154 if (in == max->input) {
155 *out = max->output;
156 return true;
157 }
158
159 // Search for the first point in the table that has a greater input
160 uint16_t n = table_search(table, in);
161 point low = table->points_[n - 1];
162 point high = table->points_[n];
163
164 // Map the input value to a t-value [0, 1]
165 float t = (in - low.input) / (high.input - low.input);
166
167 *out = low.output + t * (high.output - low.output);
168 return true;
169}
uint16_t table_search(const table *table, double in)
Definition: LUT.c:119
Here is the call graph for this function:
Here is the caller graph for this function: