-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrange.h
234 lines (196 loc) · 6.07 KB
/
range.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef RANGE_H
#define RANGE_H
#include "data_conv.h"
#include "mqtt.h"
#include "rtos.h"
#include "hdlc.h"
#include "uart_pkt.h"
#include "main-conf.h"
#define RANGE_TOPIC ("range_info")
#define ONE_SENSOR_MODE 0x60 // 96
#define TWO_SENSOR_MODE 0x61 // 97
#define XOR_SENSOR_MODE 0x62 // 98
#define OMNI_SENSOR_MODE 0x63 // 99
#define RANGE_THR_START 0x63
#define RANGE_THR_COMPLETE 0x64
#define RANGE_DATA_LEN 6
#define RANGE_PORT 5678
#define START_RANGE_THR 139
#define DATA_PER_PKT ((HDLC_MAX_PKT_SIZE - UART_PKT_HDR_LEN - 1) / RANGE_DATA_LEN)
#define MAX_NUM_ANCHORS 10
#define DATA_STRING_SIZE 9
// Range
#define MISSED_PIN_UNMASK 13
#define RF_MISSED 20
#define ULTRSND_MISSED 21
#define MBED_RANGE_PORT 5678
#define NODE_DATA_FLAG 0
#define NODE_DISC_FLAG 1
#define LOAD_DISC_NODE_LENG 3
#define ID_LENGTH 9
/* TODO: where do we put this? */
#define RANGING_DONE 0xA1
/**
* @brief Structure holding metrics measured by ultrasound ranging
*
* This structure is supposed to hold the Time Difference of Arrival
* (TDoA), Orientation Differential (OD) between the TDoA of two sensors,
* and any an pin flag to indicate which pin came first and if a pin had missed a ping.
*
* It can be extended
*/
typedef struct __attribute__((packed)){
uint16_t tdoa; //time difference of arrival
uint16_t orient_diff; //orientation differential
uint8_t status; //pin flag to indicate which pin came first and if a pin had missed a ping
int8_t node_id;
} range_data_t;
typedef struct __attribute__((packed)) {
uint8_t last_pkt;
range_data_t data[DATA_PER_PKT];
} range_hdr_t;
/**
* @brief Structure holding parameters for ultrasound ranging
*
* This structure is supposed to be used to send and interpret
* range request packets between mbed and openmote
*
* It can be extended
*/
typedef struct __attribute__((packed)){
int8_t node_id;
uint8_t ranging_mode;
// add more options in the future?
} range_params_t;
typedef struct __attribute__((packed)) {
int8_t node_id;
uint16_t tdoa;
} node_t;
/**
* @brief Loads the emcute_id to the buffer in range so it can be identified when publishing data
*
* @param buff The buffer containing the emcute_id
*/
void range_load_id(hdlc_buf_t *buff);
/**
* @brief Returns range data as a node_t
*
* @param[in] data The range data as a range_data_t struct
*
* @return The data as a node_t.
*/
node_t get_node(range_data_t data);
/**
* @brief Loads a node data into a given buffer. Wrapper function for load_data.
*
* @param buff The buffer
* @param[in] buff_size The buffer size
* @param[in] node The node
*
* @return { 0 if successful, -1 if not }
*/
int load_node_data(char *buff, size_t buff_size, node_t node);
/**
* @brief Loads all discovered nodes into a given buffer. Wrapper function for load_data.
*
* @param buff The buffer
* @param[in] buff_size The buffer size
*
* @return { 0 if successful, -1 if not }
*/
int load_discovered_nodes(char *buff, size_t buff_size);
/**
* @brief Loads data into a buffer for publishing.
*
* @param buff The data buffer
* @param[in] buff_size The buffer size
* @param[in] node The node_t containing the range data
*
* @return returns 0 on success, otherwise returns -1 on failure
*/
int load_data(char *buff, size_t buff_size, node_t node, int flag);
/**
* @brief clears the data in the data buffer
*
* @param buff The data buffer
* @param[in] buff_size The buffer size
*/
void clear_data(char *buff, size_t buff_size);
/**
* @brief Gets the distance in feet and angle in degrees.
*
* @param time_diffs The range data
* @param[in] ranging_mode The ranging mode
*
* @return The distance and angle in a dist_angle_t struct.
*/
dist_angle_t get_dist_angle(range_data_t *time_diffs, uint8_t ranging_mode);
/**
* @brief Gets the range data by communicating with the openmote over hdlc.
*
* @param[in] params The parameters for ranging
*
* @return The range data.
*/
range_data_t get_range_data(range_params_t params);
/**
* @brief Discovers all nodes that can be ranged with. This is a wrapper function for get range data. The data is stored in a
* static array of node_t data
*
* @param[in] ranging_mode The ranging mode
*/
void discover_nodes(uint8_t ranging_mode);
/**
* @brief Looks for a specific node to range with based on what's given in params. This is a wrapper function for get range data.
*
* @param[in] params The ranging parameters
*
* @return the range data
*/
range_data_t range_node(range_params_t params);
/**
* @brief initializes the range_thread
*
* @param[in] flag: 0 = no thread, 1 = thread
*/
void init_range(int flag);
/**
* @brief triggers the range routine defined in the range_thread
*
* @param params The ranging parameters
* @param msg msg pointer used for triggering
*
* @return 1 if success, 0 if fail
*/
int trigger_range_routine(range_params_t *params, msg_t *msg);
/**
* @brief triggers the range routine defined in the range_thread
*
* @param params The ranging parameters
* @param msg msg pointer used for triggering
*
* @return 1 if success, 0 if fail
*/
int trigger_range_routine_blocking(range_params_t *params, msg_t *msg);
/**
* @brief Determines if the thread is currently ranging.
*
* @return True if ranging, False otherwise.
*/
bool is_ranging();
/**
* @brief Gets the nodes that were discovered.
*
* @return The pointer to an array of node ids.
*/
node_t* get_nodes_discovered();
/**
* @brief Gets the number nodes discovered.
*
* @return The number of nodes discovered.
*/
uint8_t get_num_nodes_discovered();
void trigger_range_routine(range_params_t *params, msg_t *msg,
Mail<msg_t, HDLC_MAILBOX_SIZE> *src_mailbox);
void set_range_riot_port(uint16_t port);
#endif