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

Commit 6fe7ae58 authored by remartin's avatar remartin
Browse files

Program to save intensity data alongside a timestamp.

parent 587aca5e
//#define LEDPIN 11 //LED brightness (PWM) writing
#define LIGHTSENSORPIN A0 //Ambient light sensor reading
void setup() {
......@@ -12,8 +12,11 @@ void loop() {
float square_ratio = reading / 1023.0; //Get percent of maximum value (1023)
square_ratio = pow(square_ratio, 2.0); //Square to make response more obvious
// analogWrite(LEDPIN, 255.0 * square_ratio); //Adjust LED brightness relatively
Serial.println(reading); //Display reading in serial monitor
Serial.print("G"); // Flag pair to get them values.
Serial.print(reading); //Display reading in serial monitor
Serial.println("G");
delay(500);
// delay(500);
// delay(500);
}
......
#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;
intensityLog.open("loggedIntensityValues.txt", ofstream::out | ofstream::trunc);
intensityLog << "Stuff detected by the light sensor. \n";
intensityLog.flush();
printf("In detection mode. \n");
bool parsing = false;
string storing = "";
int something;
char hua;
long timestamp;
while(_isConnected){
something = read(tty_fd, &hua, 1);
if(something){
if(!parsing && hua == 'G'){
parsing = true;
//printf("Got something. \n");
}else if(parsing){ // receiving good stuff
if(hua == 'G'){
// writing to log
//std::chrono::high_resolution_clock::time_point cbTime = std::chrono::high_resolution_clock::now();
//timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(cbTime - 0).count();
//timestamp =
intensityLog << time(0) << "\t" << storing << "\n";
intensityLog.flush();
storing = "";
parsing = false;
}else{
storing += hua;
}
}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;
}
#pragma once
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h> //Timer
#include <fcntl.h> //Device settings
#include <termios.h> //For serial comm
#include <sys/ioctl.h> //For ioctl
#include <string.h> // needed for memset
#include <string>
#include <vector>
#include <ctime>
#include <fstream>
//SERIAL COMMUNICATION PARAMETERS
#define MAX_SERIAL_BUFFER_SIZE 180
#define SERIAL_ILogger_BAUD_RATE B9600
#define K_ERROR "\x1B[31m"
class intensityLogger{
public:
intensityLogger();
// Opens the connection. Needs to detect the
// correct port first.
void init();
void run();
void closeConnection();
bool isConnected();
private:
int startConnection(const char* port);
int tty_fd;
struct termios tio;
std::string _port;
bool _isConnected;
};
\ No newline at end of file
# Copyright (C) 2018, ETH Zurich, D-dddITET
# 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:
# Compiles the spotlight files only. Used for testing purposes.
# Run with 'bash customCooker.sh'.
#
# ----------------------------------------------------------------------------------
rm intensityLogger
g++ intensityLogger.cpp -o intensityLogger
./intensityLogger
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment