-
Notifications
You must be signed in to change notification settings - Fork 1
/
aip_functions.h
226 lines (188 loc) · 7.89 KB
/
aip_functions.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
//
// Copyright ULB BEAMS-EE
// Author: François QUITIN
//
#include <iostream>
#include <string>
#include "/usr/local/include/libserial/SerialPort.h"
using namespace LibSerial ;
/***********************************************************************
* Auxiliary functions for serial communications with mmWave array
**********************************************************************/
// Create string of instructions containing configuration of array operations
std::string* create_register_list(std::string degrees, std::string direction, int* gain_list, int gain, std::string* active_list, int mode)
{
// Convert the active_list to hexadecimal equivalent for the AiP
std::string* active_list_hex ;
active_list_hex = active_list_to_hex(active_list);
// Convert the gain_list to hexadecimal equivalent for the AiP
std::string* gain_list_hex ;
gain_list_hex = gain_list_to_hex(gain_list);
// Convert the angles to hexadecimal equivalent for the AiP
std::string* angle_list_hex ;
angle_list_hex = angle_to_reg(degrees, direction);
// Create final register list
std::string* register_list = new std::string[4];
for (int i=0; i<4; i++){
register_list[i].append("000");
register_list[i].append(std::to_string(mode));
register_list[i].append(active_list_hex[i]);
register_list[i].append(std::to_string(gain));
register_list[i].append(gain_list_hex[i]);
// !!! THERE SEEMS TO BE A BUG IN THE EXAMPLES WE RECEIVED FROM AMOTECH. THEY SEEM TO HAVE INVERSED THE ORDER OF THE REGISTERS. HOWEVER, THIS DOES NOT SEEM TO MATTER IN THE END.
register_list[i].append(angle_list_hex[3-i]);
}
//std::cout << boost::format(" REGISTER LIST: {%s, %s, %s, %s}") % register_list[0] % register_list[1] % register_list[2] % register_list[3] << std::endl;
return register_list;
}
// Write string on serial port and read response
std::string write_read_serial(SerialPort* my_serial_port, std::string my_string, int ver_aip)
{
int timeout_ms = 20; // timeout value in milliseconds
char next_char; // variable to store the read result
std::string rx_string;
int timeout = 0;
// Write to serial port
my_serial_port->Write( my_string );
if(ver_aip){
std::cout << boost::format(" -- to serial port: %s") % my_string << std::endl;
}
// Read from serial port until timeout
rx_string = "";
while (!(timeout)){
try
{
my_serial_port->ReadByte( next_char, timeout_ms );
//std::cout << boost::format(" -- from serial port: %s") % next_char << std::endl;
rx_string.push_back( next_char );
if (next_char == '\r'){
my_serial_port->ReadByte( next_char, timeout_ms );
//std::cout << boost::format(" -- found CARRIAGE RETURN to end message" ) << std::endl;
timeout = 1;
}
}
catch (const ReadTimeout&)
{
timeout = 1;
}
}
if(ver_aip){
std::cout << boost::format(" -- response from serial port: %s" ) % rx_string << std::endl;
}
return rx_string;
}
/***********************************************************************
* Functions to control mmWave array
**********************************************************************/
// Send command to mmWave AiP
void send_to_aip(SerialPort* my_serial_port, std::string degrees, std::string direction, int* gain_list, int gain, std::string* active_list, int mode, int ver_aip)
{
std::string my_string;
std::string* register_list = create_register_list(degrees, direction, gain_list, gain, active_list, mode);
// Initialize the mmWave array package
//std::cout << boost::format("Initialize mmWave array...") << std::endl;
write_read_serial(my_serial_port, "AT+DUT=0158\r\0", ver_aip);
write_read_serial(my_serial_port, "AT+AIPCONFIG=0202\r\0", ver_aip);
write_read_serial(my_serial_port, "AT+ADRNUM=001\r\0", ver_aip);
// TODO: check if all responses = AMO_OK
// Initialize chip registers
my_string = "AT+REG=";
my_string.append(REG1);
my_string.append("\r\0");
for (int i=0; i<4; i++){
write_read_serial(my_serial_port, my_string, ver_aip);
}
// TODO: check if all responses = CHIP_OK
write_read_serial(my_serial_port, "AT+SEND?\r\0", ver_aip);
// Write insctructions for each chip
for (int i=0; i<4; i++){
my_string = "AT+REG=";
my_string.append(register_list[i]);
my_string.append("\r\0");
write_read_serial(my_serial_port, my_string, ver_aip);
}
// TODO: check if all responses = CHIP_OK
write_read_serial(my_serial_port, "AT+SEND?\r\0", ver_aip);
// Read temperature of each chip
my_string = "AT+REG=";
my_string.append(REG_TEMP);
my_string.append("\r\0");
for (int i=0; i<4; i++){
write_read_serial(my_serial_port, my_string, ver_aip);
}
// TODO: check if all responses = CHIP_OK
write_read_serial(my_serial_port, "AT+SEND?\r\0", ver_aip);
// Enable Tx or Rx
if (mode == 1){
//std::cout << boost::format("Enabling Tx..") << std::endl;
write_read_serial(my_serial_port, "AT+TXEN=1\r\0", ver_aip);
}
else if (mode == 2){
//std::cout << boost::format("Enabling Rx..") << std::endl;
write_read_serial(my_serial_port, "AT+RXEN=1\r\0", ver_aip);
}
else if (mode == 0){
//std::cout << boost::format("Disabling Tx and Rx of AiP..") << std::endl;
write_read_serial(my_serial_port, "AT+TXEN=0\r\0", ver_aip);
write_read_serial(my_serial_port, "AT+RXEN=0\r\0", ver_aip);
}
// TODO: check if all responses = AMO_OK
}
// Send command to mmWave AiP
void init_aip(SerialPort* my_serial_port, int ver_aip)
{
std::string my_string;
// Initialize the mmWave array package
//std::cout << boost::format("Initialize mmWave array...") << std::endl;
write_read_serial(my_serial_port, "AT+DUT=0158\r\0", ver_aip);
write_read_serial(my_serial_port, "AT+AIPCONFIG=0202\r\0", ver_aip);
write_read_serial(my_serial_port, "AT+ADRNUM=001\r\0", ver_aip);
// TODO: check if all responses = AMO_OK
// Initialize chip registers
my_string = "AT+REG=";
my_string.append(REG1);
my_string.append("\r\0");
for (int i=0; i<4; i++){
write_read_serial(my_serial_port, my_string, ver_aip);
}
// TODO: check if all responses = CHIP_OK
write_read_serial(my_serial_port, "AT+SEND?\r\0", ver_aip);
}
// Send command to mmWave AiP
void send_to_aip_fast(SerialPort* my_serial_port, std::string degrees, std::string direction, int* gain_list, int gain, std::string* active_list, int mode, int ver_aip)
{
std::string my_string;
std::string* register_list = create_register_list(degrees, direction, gain_list, gain, active_list, mode);
// Write insctructions for each chip
for (int i=0; i<4; i++){
my_string = "AT+REG=";
my_string.append(register_list[i]);
my_string.append("\r\0");
write_read_serial(my_serial_port, my_string, ver_aip);
}
// TODO: check if all responses = CHIP_OK
write_read_serial(my_serial_port, "AT+SEND?\r\0", ver_aip);
// Enable Tx or Rx
if (mode == 1){
//std::cout << boost::format("Enabling Tx..") << std::endl;
write_read_serial(my_serial_port, "AT+TXEN=1\r\0", ver_aip);
}
else if (mode == 2){
//std::cout << boost::format("Enabling Rx..") << std::endl;
write_read_serial(my_serial_port, "AT+RXEN=1\r\0", ver_aip);
}
else if (mode == 0){
//std::cout << boost::format("Disabling Tx and Rx of AiP..") << std::endl;
write_read_serial(my_serial_port, "AT+TXEN=0\r\0", ver_aip);
write_read_serial(my_serial_port, "AT+RXEN=0\r\0", ver_aip);
}
// TODO: check if all responses = AMO_OK
}
// Disable Tx/Rx of mmWave AiP
void disable_aip(SerialPort* my_serial_port, int ver_aip)
{
std::cout << boost::format("Disabling Tx and Rx of AiP..") << std::endl;
write_read_serial(my_serial_port, "AT+TXEN=0\r\0", ver_aip);
write_read_serial(my_serial_port, "AT+RXEN=0\r\0", ver_aip);
// TODO: check if all responses = AMO_OK
}