This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
216 lines (173 loc) · 5.75 KB
/
main.cpp
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
/*
* This application measures distances with SF11/C Lidar.
* Copyright (C) 2017 TerraClear, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <chrono>
//wiringPi
#include <wiringPi.h>
//locals
#include "pipinmap.hpp"
#include "altimeter.hpp"
#include "thread_serialrx.hpp"
//Pins for LED's and Trigger line.
#define LED_OK GPIO_12
#define LED_ERR GPIO_24
#define PIN_TRIGGER GPIO_21
#define DEBOUNCE_MS 100
//clock to measure time elapsed between triggers.
std::chrono::steady_clock::time_point _lastmeasure;
uint32_t elapse_tof = 0;
//serial thread
thread_serialrx* pThreadRX = nullptr;
//sequence number of trigger
uint32_t _seqno = 0;
//Interrupt code.
void trigger_pulse()
{
//get milliseconds since previous trigger
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
uint32_t millicount = std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastmeasure).count();
//restrict serial calls incase there is noise on the line.
if (millicount > DEBOUNCE_MS)
{
_lastmeasure = std::chrono::steady_clock::now();
//reset millis for 1st entry
if (_seqno == 0)
{
millicount = 0;
elapse_tof = 0;
}
else
{
elapse_tof += millicount;
}
//create altimetry log request.
pThreadRX->create_request(_seqno, elapse_tof);
//increase seq numbers.
_seqno++;
}
}
//check if file exists..
bool fexists(const std::string& filename)
{
std::ifstream ifile(filename.c_str());
return (bool)ifile;
}
//generete next log file name in numbered sequence
std::string generate_filename(std::string file_name, std::string file_ext)
{
int file_seq = 1;
int max_tries = 1000;
std::string default_file = file_name + "." + file_ext;
std::string tmp_file = default_file;
do
{
std::stringstream strstrm;
strstrm << file_name << "_" << file_seq << "." << file_ext;
tmp_file = strstrm.str();
file_seq++;
max_tries --;
//max file sequence generation reached, use default and overwrite.
if (max_tries <= 0)
{
tmp_file = default_file;
break;
}
} while (fexists(tmp_file));
return tmp_file;
}
int main(int argc, char** argv)
{
//initiate the clock
_lastmeasure = std::chrono::steady_clock::now();
//Default output file
std::string outfile_name = "altimetry";
std::string outfile_ext = "txt";
//Default output debug log file
std::string outfile_debug_name = "debug_altimetry";
//Default serial
std::string serial_path = "/dev/ttyS0";
//Default Baud Rate
uint32_t serial_baud = 115200;
//if port and path is supplied..
if (argc > 1)
{
serial_path = argv[1];
}
if (argc > 2)
{
std::stringstream strstrm;
strstrm << argv[2] << "/" << outfile_name;
outfile_name = strstrm.str();
}
// if (argc > 3)
// {
// std::stringstream strstrm;
// strstrm << argv[3] << "/" << outfile_debug_name;
// outfile_debug_name = strstrm.str();
// }
std::string outfile_full = generate_filename(outfile_name, outfile_ext);
std::string outfile_debug_full = generate_filename(outfile_name + "debug", outfile_ext);
//create altimeter
altimeter* pAltimeter = new altimeter(outfile_full, outfile_debug_full);
//create & start serial port comms
std::cout << ">>> START SERIAL THREAD..." << std::endl;
pThreadRX = new thread_serialrx(pAltimeter, serial_path, serial_baud);
pThreadRX->thread_start("serialrx");
//setup wiringPi in GPIO pin numbering mode..
std::cout << ">>> STARTUP PI GPIO..." << std::endl;
wiringPiSetupGpio();
//set IO pin state
pinMode(LED_OK, OUTPUT);
pinMode(LED_ERR, OUTPUT);
pinMode(PIN_TRIGGER, INPUT);
//set LED Start state
std::cout << ">>> DEFAULT LED STATES..." << std::endl;
digitalWrite(LED_ERR, HIGH);
digitalWrite(LED_OK, LOW);
//wire up interrupt on trigger pin
std::cout << ">>> WIRING ISR..." << std::endl;
wiringPiISR(PIN_TRIGGER, INT_EDGE_FALLING, &trigger_pulse);
//Flash LED
bool toggle = false;
while(1)
{
if (pThreadRX->altimeter_ok())
{
digitalWrite(LED_OK, HIGH);
digitalWrite(LED_ERR, LOW);
}
else
{
//flash ERR LED
toggle = !toggle;
digitalWrite(LED_OK, LOW);
digitalWrite(LED_ERR, toggle);
}
usleep(250000);
}
//stop and delete thread.
pThreadRX->thread_stopwait();
delete pAltimeter;
pAltimeter = nullptr;
delete pThreadRX;
pThreadRX = nullptr;
return 0;
}