-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
163 lines (141 loc) · 4.32 KB
/
main.cpp
File metadata and controls
163 lines (141 loc) · 4.32 KB
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
/* main.cpp
* Main loop that runs on the Raspberry Pi 4b.
*/
#include "control.h"
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <signal.h>
#include <chrono>
#include <thread>
//States
#define IDLE 0
#define OBJ_DETECT 1
#define TARGET_WARNING 2
#define TARGET_FIRE 3
//Functions
std::shared_ptr<spdlog::logger> InitializeLogger();
//Globals
std::shared_ptr<spdlog::logger> LOG;
void SigHandle(int sig)
{
if (sig == SIGINT || sig == SIGTERM || sig == SIGSEGV)
{
//Exiting
printf("\nExiting via sig handle...\n");
DestructCL();
exit(1);
}
}
void printUsage(char **argv)
{
printf("Usage: sudo %s [OPTIONS]\n", argv[0]);
printf("[-p float] Proportional gain Kp\n");
printf("[-d float] Derivative gain Kd\n");
printf("[-n ] No Fire (disable spool and fire for autonomous mode)\n");
printf("[-s ] Show camera image (X11 forwarding only)\n");
}
int main(int argc, char **argv)
{
//Validate sudo
if (geteuid() != 0)
{
fprintf(stderr, "You must run the program with 'sudo' privileges\n");
return 1;
}
//Parse input args
int opt;
float Kp = 0.32; //Proportional Gain
float Kd = 0.2; //Derivative Gain
char *endptr;
bool no_fire = false;
bool show_image = false;
// Parse command-line options using getopt
while ((opt = getopt(argc, argv, "hp:d:ns")) != -1)
{
switch (opt) {
case 'p':
Kp = strtof(optarg, &endptr);
if (*endptr != '\0')
{
fprintf(stderr, "Invalid float value for option 'p' Proportional gain: %s\n", optarg);
return 1;
}
break;
case 'd':
Kd = strtof(optarg, &endptr);
if (*endptr != '\0')
{
fprintf(stderr, "Invalid float value for option 'd' Derivative gain: %s\n", optarg);
return 1;
}
break;
case 'n':
no_fire = true;
break;
case 's':
show_image = true;
break;
case 'h':
printUsage(argv);
return 1;
default:
printUsage(argv);
return 1;
}
}
//Setup signal handling
signal(SIGINT, SigHandle);
signal(SIGTERM, SigHandle);
signal(SIGSEGV, SigHandle);
//Initialization
LOG = InitializeLogger();
LOG->debug("System initializing...");
InitCL(Kp, Kd, no_fire, show_image);
int state = IDLE;
bool loop = true;
while(loop)
{
if (!RUN_MANUAL)
{
if (state == IDLE) state += RunIdle();
else if (state == OBJ_DETECT) state += RunObjDetect();
else if (state == TARGET_WARNING) state += RunTargetWarn();
else if (state == TARGET_FIRE) state += RunTargetFire();
else
{
//Should never occur
LOG->error("Invalid state!");
loop = false;
}
}
else
{
std::this_thread::sleep_for(std::chrono::seconds(1)); //Wait for a mode toggle
state = IDLE;
}
}
LOG->flush();
return 0;
}
/* Function to initialize the logger with console and file sinks
*/
std::shared_ptr<spdlog::logger> InitializeLogger()
{
//Create a stdout sink (console output with color support)
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
//Create a file sink (logs to a file)
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/logfile.log", true); // Append to file if it exists
// Create a logger with both console and file sinks
auto logger = std::make_shared<spdlog::logger>("multi_sink", spdlog::sinks_init_list({console_sink, file_sink}));
//Set the log level for both sinks and register
logger->set_level(spdlog::level::debug);
spdlog::register_logger(logger);
//Set desired settings
spdlog::flush_every(std::chrono::seconds(3));
spdlog::set_pattern("[%H:%M:%S %^%l%$] %v");
return logger;
}