To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

intensityLogger.cpp 5.89 KB
Newer Older
remartin's avatar
remartin committed
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
//    Copyright (C) 2018, ETH Zurich, D-ITET
//          Martin Reinhard         remartin       @ee.ethz.ch
//
//    This file is part of D-FaLL-System.
//    
//    D-FaLL-System 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.
//    
//    D-FaLL-System 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 D-FaLL-System.  If not, see <http://www.gnu.org/licenses/>.
//    
//
//    ----------------------------------------------------------------------------------
//    DDDD        FFFFF        L     L           SSSS  Y   Y   SSSS  TTTTT  EEEEE  M   M
//    D   D       F      aaa   L     L          S       Y Y   S        T    E      MM MM
//    D   D  ---  FFFF  a   a  L     L     ---   SSS     Y     SSS     T    EEE    M M M
//    D   D       F     a  aa  L     L              S    Y        S    T    E      M   M
//    DDDD        F      aa a  LLLL  LLLL       SSSS     Y    SSSS     T    EEEEE  M   M
//
//
//    DESCRIPTION:
//	  Writes intensity values delivered by a sensor on an Arduino to a .txt file. 
30
//    Run with 'bash cookIntensityLogger.sh'. 
remartin's avatar
remartin committed
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


#include "intensityLogger.h"

using namespace std; 

int main(int argc, char* argv[]){

	intensityLogger iL; 
	iL.init(); 
	iL.run(); 

	return 0; 
}

intensityLogger::intensityLogger(){
	_isConnected = false;
	_port = "";
}

void intensityLogger::run(){

	ofstream intensityLog; 
56
	intensityLog.open("loggedIntensityValues_C.txt", ofstream::out | ofstream::trunc);
57
	intensityLog << "time\tNormed Intensity\n"; 
58
59
60
61
62
	intensityLog.flush(); 

	printf("In detection mode. \n"); 

	bool parsing = false; 
63
	string storing; 
64
	char hua; 
65

66
	long timestamp; 
67
	std::chrono::high_resolution_clock::time_point startTime; // = std::chrono::high_resolution_clock::now();
68
69

	while(_isConnected){
70
71
		if(read(tty_fd, &hua, 1) > 0){
			//printf("Received something: %c \n", hua); 
72
73
			if(!parsing && hua == 'G'){
				parsing = true; 
74
				//printf("Starting to save stuff. \n"); 
75
			}else if(parsing){ // receiving good stuff
76
77
78
79
80
				if(hua == 'G'){ // save. 

					chrono::high_resolution_clock::time_point cbTime = chrono::high_resolution_clock::now();
					timestamp = chrono::duration_cast<chrono::nanoseconds>(cbTime-startTime).count();

81
					// writing to log 
82
					intensityLog << timestamp << "\t" << storing << "\n"; 
83
84
85
					intensityLog.flush();  
					storing = ""; 
					parsing = false;  
86
					//printf("Ending saving procedure. \n"); 
87
				}else{
88
89
90
91
92
93
94
					if(isdigit(hua)){// avoid being out of sync ('between' flags). 
						storing.push_back(hua); 
					}else{
						printf("Was out of sync. Correcting that. \n"); 
						storing = ""; 
						parsing = false; 						
					}
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
				}
			}else{ // Rubbish received. 
			}
		}
	}
}

void intensityLogger::init(){

	// The device could be located at ttyACM0 or ttyACM1. 
	// Need to determine which. 
	printf("Searching for the /dev file linked to the Atmel Arduino.\n"); 

	// The command: get device infos, suppress error stream, filter for arduino, count lines. 
	char command[] = "udevadm info /dev/ttyACM0 2> /dev/null | grep Atmel | wc -l > rubbishBin.txt";
	char path[] = "/dev/ttyACM0"; 

	std::ifstream reader; // use a pipe instead? 
	int response = 0; 
	bool foundDevice = false; 
 
	for(int gg = 0; gg<10 && !foundDevice; gg++){

		command[24] = '0' + gg ; // ACM1 etc. 
		system(command); 

		// get the result of the command
    	reader.open("rubbishBin.txt");
		reader >> response; 
    	reader.close();
		remove("rubbishBin.txt"); 

		if(response > 0){
			path[11] = '0' + gg; 
			printf("Found Atmel Arduino /dev file port at %s. \n", path); 
			foundDevice = true; 
		}

	}

	if(!foundDevice){printf("Can't find the Atmel Arduino. Is it plugged in? \n");return;}

	startConnection(path);
	usleep(500000);
}

int intensityLogger::startConnection(const char* port){
	//printf("Attempting to connect to Serial port %s...\n", port);

	_port = "";
	_isConnected = false;

	tty_fd = open(port, O_RDWR | O_NONBLOCK | O_NOCTTY | O_DSYNC); 
	// both read and write, open nonblockingly
	// some sync on file. Change O_SYNC to O_DSYNC? 

	if (tty_fd < 0) {
		printf("%sError opening port: %s\n", K_ERROR, strerror(errno));
		return -1;
	}else{
		printf("Opening port should've worked.\n");
	}

	/* Setting other Port Stuff */
	tio.c_cflag     &=  ~PARENB;            // no parity bits
	tio.c_cflag     &=  ~CSTOPB;				// not two stop bits
	tio.c_cflag     &=  ~CSIZE;
	tio.c_cflag     &=  ~CRTSCTS;           // no flow control
	tio.c_cflag     |=  CS8;
	tio.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines

	tio.c_cc[VMIN]   =  1;                  // read doesn't block
	tio.c_cc[VTIME]  =  5;                  // 0.5 seconds read timeout

	tio.c_iflag = 0;
	tio.c_oflag = 0;
	//tio.c_cflag = CS8 | CREAD | CLOCAL | HUPCL; // char size 8, enable read, ignore modem control lines, lower timeout stuff
	tio.c_lflag = 0;
	//tio.c_cc[VMIN] = 0; //1
	//tio.c_cc[VTIME] = 5;

	/* Make raw */
	cfmakeraw(&tio);

	cfsetospeed(&tio, SERIAL_ILogger_BAUD_RATE);
	cfsetispeed(&tio, SERIAL_ILogger_BAUD_RATE);

	if (tcsetattr(tty_fd, TCSANOW, &tio) != 0){ // "TCSANOW: change occurs immediately"
		printf("%sError from tcsetattr: %s\n", K_ERROR, strerror(errno));
		return -1;
	}
	printf("Set baud rate and stuff. \n"); 

	_port = port;
	_isConnected = true;

	return 1;
}

void intensityLogger::closeConnection(){
	if (_port != "" && _isConnected)
		close(tty_fd); // Port release 
}

bool intensityLogger::isConnected(){
	return _isConnected; 
}