Skip to content

Commit 39520fd

Browse files
committed
Shell script automatically called in the code to compress all output files into one file after record mode and to de-crompress and the compressed file before proceeding to visualize in offline mode
1 parent c04e3b9 commit 39520fd

File tree

5 files changed

+116
-50
lines changed

5 files changed

+116
-50
lines changed

candump.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ int can_main(int argc, char **argv)
520520
setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FILTER,
521521
rfilter, numfilter * sizeof(struct can_filter));
522522

523-
free(rfilter);
523+
//free(rfilter);
524+
delete [] rfilter;
524525

525526
} /* if (nptr) */
526527

data_structures.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#ifndef DATA_STRUCTURES_CPP
2+
#define DATA_STRUCTURES_CPP
13
struct fire_data {
24
uint16_t block_id;
35
double azimuth;
@@ -9,4 +11,5 @@ struct data_packet {
911
uint8_t header[42];
1012
fire_data payload[12];
1113
uint8_t footer[6];
12-
};
14+
};
15+
#endif

functions.cpp

+58-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#ifndef FUNCTIONS_CPP
2+
#define FUNCTIONS_CPP
13
#include <pcl/visualization/cloud_viewer.h>
24
#include <pcl/visualization/pcl_visualizer.h>
35
#include <pcl/io/io.h>
@@ -26,10 +28,14 @@
2628
#include <vector>
2729
#include "data_structures.cpp"
2830
#include "video.cpp"
31+
#include <cstdlib>
2932

3033
#include <sys/types.h>
3134
#include <sys/mman.h>
3235

36+
#include "lib.c"
37+
#include "candump.cpp"
38+
3339
#define PI 3.14159265
3440
// list elevation angles corresponding to each of the 32 laser beams for the HDL-32
3541
// const double elev_angles[32] = {-30.67, -9.33, -29.33, -8, -28, -6.66,
@@ -49,7 +55,46 @@ const int cycle_num = 50;
4955
const int delay_us = 50000;
5056
int user_data;
5157

58+
/* -------------------------------------------------------------
59+
-------------Functions to compress files into tar------------
60+
-------------------------------------------------------------*/
61+
inline
62+
void compressFunct(){
63+
if(system("tar -czvf data.tar.gz Sample_1.pcap Sample_2.pcap out.avi ") < 0){
64+
cout << "error in compressing files" << endl;
65+
exit(0);
66+
}
67+
if(system("rm Sample_1.pcap Sample_2.pcap out.avi") < 0){
68+
cout << "error in deleting pcap files" << endl;
69+
exit(0);
70+
}
71+
}
72+
73+
void compressFiles(int signum){
74+
exit_thread = true;
75+
cout << "\nCompressing files into data.tar.gz file ..." << endl;
76+
sleep(1);
77+
compressFunct();
78+
exit(0);
79+
}
80+
81+
void deleteFiles(int signum){
82+
exit_thread = true;
83+
cout << "deleting pcap and video files" << endl;
84+
if( system("rm Sample_1.pcap Sample_2.pcap out.avi") < 0){
85+
cout << "error in deleting files" << endl;
86+
}
87+
cout << "done!" << endl;
88+
exit(0);
89+
}
5290

91+
inline
92+
void canData(){
93+
char *myargv[4] = {"./candump", "-tz", "vcan0", NULL};
94+
int myargc = 3;
95+
int can_return = can_main(myargc, myargv);
96+
cout << "return from can_main function: " << can_return << endl;
97+
}
5398

5499
void keyboardEventOccurred (const pcl::visualization::KeyboardEvent &event,
55100
void* viewer_void)
@@ -74,7 +119,7 @@ void mouseEventOccurred (const pcl::visualization::MouseEvent &event,
74119
if (event.getButton () == pcl::visualization::MouseEvent::LeftButton &&
75120
event.getType () == pcl::visualization::MouseEvent::MouseButtonPress)
76121
{
77-
std::cout << "Left mouse button released at position (" << event.getX () << ", " << event.getY () << ")" << std::endl;
122+
//std::cout << "Left mouse button released at position (" << event.getX () << ", " << event.getY () << ")" << std::endl;
78123

79124
char str[512];
80125
//sprintf (str, "text#%03d", text_id ++);
@@ -107,8 +152,6 @@ void viewerPsycho (pcl::visualization::PCLVisualizer& viewer)
107152
user_data++;
108153
}
109154

110-
/* ------------------------------------------------------------*/
111-
112155
/* -------------------------------------------------------------
113156
-------------data_structure_builder--------------------------
114157
-------------------------------------------------------------*/
@@ -166,11 +209,11 @@ namespace data_structure
166209
if (pkthdr->len != pkthdr->caplen)
167210
printf("Warning! Capture size different than packet size: %ld bytes\n", (long)pkthdr->len);
168211

169-
// return an empty struct if the packet length is not 1248 bytes
170-
// if(pkthdr -> len != 1248){
171-
// processed_packet = (const struct data_packet){0};
172-
// return;
173-
// }
212+
// // return an empty struct if the packet length is not 1248 bytes
213+
if(pkthdr -> len != 1248){
214+
processed_packet = (const struct data_packet){0};
215+
return;
216+
}
174217

175218
for(int i = 0; i < 42; i++){
176219
processed_packet.header[i] = data[i]; // fill in the header
@@ -381,8 +424,9 @@ namespace live
381424

382425
//end the program if the viewer was closed by the user
383426
if(viewer->wasStopped()){
384-
cout << "Viewer Stopped" << endl;
385-
exit(0);
427+
//cout << "Viewer Stopped" << endl;
428+
//exit(0);
429+
return;
386430
}
387431
}
388432

@@ -403,8 +447,9 @@ namespace live
403447

404448
//end the program if the viewer was closed by the user
405449
if(viewer->wasStopped()){
406-
cout << "Viewer Stopped" << endl;
407-
exit(0);
450+
//cout << "Viewer Stopped" << endl;
451+
//exit(0); //disabled temporarily; making child exit which makes parent to wait for child forever;
452+
return;
408453
}
409454
}
410455
}
@@ -492,5 +537,6 @@ namespace offline
492537

493538
}
494539

540+
#endif
495541

496542

main.cpp

+46-35
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
//#include "data_structures.cpp"
1515
#include "functions.cpp"
1616
#include <signal.h>
17-
#include "lib.c"
18-
#include "candump.cpp"
1917
#include <fstream>
2018

2119
using namespace std;
@@ -69,8 +67,14 @@ int main(int argc, char *argv[])
6967
}
7068
else {
7169

70+
int pid1 = fork();
71+
if(pid1 == 0){
72+
canData();
73+
}
74+
else{
7275
pcl::visualization::CloudViewer viewer("Data from eth10");
7376
std::thread t1(video::playback_video, 1);
77+
//std::thread t2(canData);
7478
descr = pcap_open_live(eth_port_2, 1248, 1, 1, errbuf);
7579
if (descr == NULL) {
7680
cout << "pcap_open_live() failed: " << errbuf << endl;
@@ -79,12 +83,17 @@ int main(int argc, char *argv[])
7983
viewer.runOnVisualizationThreadOnce (viewerOneOff);
8084
viewer.runOnVisualizationThread (viewerPsycho);
8185
pcap_loop(descr, 0, live::packetHandler_II, (u_char *) &viewer);
82-
int w = wait(NULL);
86+
87+
//t2.join();
8388
t1.join();
84-
while(!viewer.wasStopped()){
85-
//do nothing
86-
}
89+
int w = wait(NULL);
90+
int w1 = wait(NULL);
91+
// while(!viewer.wasStopped()){
92+
// //do nothing
93+
// }
94+
8795
}
96+
}
8897
}
8998

9099
if(argv[1] == s[1]) // record mode
@@ -95,41 +104,42 @@ int main(int argc, char *argv[])
95104
exit(0);
96105
}
97106
else if(ret == 0){
98-
close(1);
107+
108+
close(1); //redirecting the ouput
99109
int fd = open("canData.txt", O_WRONLY | O_CREAT | O_TRUNC, 0660);
100110
if(fd < 0){
101111
cout << "cannot open file canData.txt" << endl;
102112
exit(0);
103113
}
104-
/*char *myargs[3];
105-
myargs[0] = (const char *) "candump";
106-
myargs[1] = (const char *) "can0";
107-
myargs[2] = NULL;
108-
int exec_return = execvp(myargs[0], myargs);
109-
*/
110-
114+
111115
char *myargv[4] = {"./candump", "-tz", can_port, NULL};
112116
int myargc = 3;
113117
int can_return = can_main(myargc, myargv);
114118
cout << "return from can_main function: " << can_return << endl;
115119

116120
}
117121
else{
122+
signal(SIGINT, compressFiles);
118123
thread t1(video::capture_video);
119124
thread t2(record::save_pcap, eth_port_1, "Sample_1.pcap");
120125
thread t3(record::save_pcap, eth_port_2, "Sample_2.pcap");
121126

122127
t2.join();
123128
t3.join();
124129
t1.join();
125-
int w1 = wait(NULL);
130+
int w = wait(NULL);
131+
compressFunct();
126132
}
127133

128134
}
129135

130136
if(argv[1] == s[2]) // offline mode
131137
{
132138
cout << "Offline Mode Entered" << endl;
139+
if(system("tar -xzvf data.tar.gz") < 0){
140+
cout << "error in extracting the data from tar file" << endl;
141+
return 0;
142+
}
133143

134144
// Fill 2 giant vectors with the contents of the 2 pcap files
135145
pcap_t *descr_I;
@@ -140,26 +150,26 @@ int main(int argc, char *argv[])
140150
cout << "pcap_open_offline() failed: " << errbuf << endl;
141151
return 1;
142152
}
143-
vector<struct data_packet> giant_vector_I;
144-
pcap_loop(descr_I, 0, offline::pcap_copier_I, (u_char *) &giant_vector_I);
153+
vector<struct data_packet> giant_vector_I;
154+
pcap_loop(descr_I, 0, offline::pcap_copier_I, (u_char *) &giant_vector_I);
145155

146-
descr_II = pcap_open_offline("Sample_2.pcap", errbuf);
147-
if (descr_II == NULL) {
148-
cout << "pcap_open_offline() failed: " << errbuf << endl;
149-
return 1;
150-
}
151-
vector<struct data_packet> giant_vector_II;
152-
pcap_loop(descr_II, 0, offline::pcap_copier_II, (u_char *) &giant_vector_II);
156+
descr_II = pcap_open_offline("Sample_2.pcap", errbuf);
157+
if (descr_II == NULL) {
158+
cout << "pcap_open_offline() failed: " << errbuf << endl;
159+
return 1;
160+
}
161+
vector<struct data_packet> giant_vector_II;
162+
pcap_loop(descr_II, 0, offline::pcap_copier_II, (u_char *) &giant_vector_II);
153163

154-
int pid = fork();
155-
if(pid < 0){
156-
cout << "fork error" << endl;
157-
exit(0);
158-
}
164+
int pid = fork();
165+
if(pid < 0){
166+
cout << "fork error in offline mode" << endl;
167+
exit(0);
168+
}
159169

160170
else if(pid == 0){
161171
pcl::visualization::CloudViewer viewer("Sample_1");
162-
//viewer.registerMouseCallback (mouseEventOccurred, (void*) &viewer);
172+
viewer.registerMouseCallback (mouseEventOccurred, (void*) &viewer);
163173
viewer.registerKeyboardCallback (keyboardEventOccurred, (void*) &viewer);
164174
viewer.runOnVisualizationThreadOnce (viewerOneOff);
165175
viewer.runOnVisualizationThread (viewerPsycho);
@@ -175,7 +185,7 @@ int main(int argc, char *argv[])
175185
cout << "fork error" << endl;
176186
exit(0);
177187
}
178-
else if(pid1 == 0){
188+
else if(pid1 == 0){ //displaying canData on terminal
179189

180190
ifstream canData("canData.txt");
181191
string line, tempStrTime, strTime;
@@ -200,10 +210,10 @@ int main(int argc, char *argv[])
200210
}
201211

202212
else{
203-
204-
thread t1(video::playback_video, 0);
213+
signal(SIGINT, deleteFiles);
214+
thread t1(video::playback_video, 0);
205215
pcl::visualization::CloudViewer viewer("Sample_2");
206-
//viewer.registerMouseCallback (mouseEventOccurred, (void*) &viewer);
216+
viewer.registerMouseCallback (mouseEventOccurred, (void*) &viewer);
207217
viewer.registerKeyboardCallback (keyboardEventOccurred, (void*) &viewer);
208218
viewer.runOnVisualizationThreadOnce (viewerOneOff);
209219
viewer.runOnVisualizationThread (viewerPsycho);
@@ -212,7 +222,8 @@ int main(int argc, char *argv[])
212222
//do nothing
213223
}
214224
t1.join();
215-
int w = wait(NULL);
225+
int w = wait(NULL);
226+
int w1 = wait(NULL);
216227
}
217228
}
218229
cout << "------------" << endl;

video.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
#ifndef VIDEO_CPP
2+
#define VIDEO_CPP
13
#include "opencv2/opencv.hpp"
24
volatile unsigned int *pause_sim_kb = 0;
5+
int exit_thread = false;
36
bool play_cloud = true;
47
/* ------------------------------------------------------------*/
58

@@ -24,7 +27,7 @@ namespace video
2427
VideoWriter video("out.avi",CV_FOURCC('M','J','P','G'),10, Size(frame_width,frame_height),true);
2528

2629
for(;;){
27-
30+
if(exit_thread == true){break;} //return to main thread
2831
Mat frame;
2932
cap >> frame; // get a new frame from camera
3033

@@ -54,6 +57,7 @@ namespace video
5457

5558
for(;;){
5659
while(*pause_sim_kb == 1){}
60+
if(exit_thread){break;}
5761
if(playVideo)
5862
cap >> frame; // get a new frame from camera
5963
if(!frame.data) break;
@@ -71,3 +75,4 @@ namespace video
7175

7276
}
7377
}
78+
#endif

0 commit comments

Comments
 (0)