<commit_before>/*
 * CReader.cpp
 * save laser data to binary file
 *
 * Created on: 22.12.2014
 * Author: Markus
*/
#include <iostream>
#include <string>
#include "CReader.h"
#include <sstream>
#include <fstream>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <math.h>
#include <list>

#include <octomap/octomap.h>
#include <octomap/OcTree.h>
#include <octomap/Pointcloud.h>
#include "time.h"
#include "ip_connection.h"
#include "brick_imu.h"

#include <boost/chrono/thread_clock.hpp>

using namespace boost::chrono;

//
#define PI 3.14159265

#define CONFIG "/media/usb0/config.cfg"
#define HOST "localhost"
#define PORT 4223

#define DEBUG 1

std::fstream logfile;

boost::mutex mtx_da;
std::stringstream ss;

int data_av = 0;
bool is_imu_connected;

using std::string;
using namespace octomap;

IPConnection ipcon;
IMU imu;

// trim a string
const std::string trim( const std::string& s)
{
	std::string::size_type first = s.find_first_not_of( ' ');
	if( first == std::string::npos) {
		return std::string();
	}
	else {
		std::string::size_type last = s.find_last_not_of( ' '); /// must succeed
		return s.substr( first, last - first + 1);
	}
}

void debugMessage(int level, string message)
{
	if (level >= DEBUG)
		std::cerr << message << std::endl;
	return;
}

// constructor
CReader::CReader()
{
	scancycles = 0;
	is_imu_connected = false;

	ip_adress = "192.168.0.1";
	port = 12002;
	convergence = 0;

	// set angle filter
	angle_max = 0*PI/180; // scanner max 50
	angle_min = -60*PI/180; // scanner min -60

	maxmin = false;
	duration = false;
	writebt = false;
	writecsv = false;
	is_indoor = false;

	if (readConfig())
		debugMessage(2,"config successfully readed!");
	else
		debugMessage(2, "no config file!");

	return;
}

// read config file
int CReader::readConfig()
{
	std::ifstream fh;
	std::string line;
	size_t pos = 0;
	std::string key, val;

	fh.open(CONFIG, std::ios::in);

	if (!fh.is_open()) {
		fh.open("config.cfg", std::ios::in);
		if (!fh.is_open()) {
			return FALSE;
		}
	}

	debugMessage(2, "read config...");

	while (getline(fh,line)) {
		pos = line.find("=");
		key = trim(line.substr(0,pos));
		val = trim(line.substr(pos+1));

		try {
			if (key.compare("ip") == 0) {
				ip_adress = val;
			} else if (key.compare("port") == 0) {
				port = atoi(val.c_str());
			} else if (key.compare("maxmin") == 0) {
				if (val.compare("true") == 0)
					maxmin = true;
				else
					maxmin = false;
			} else if (key.compare("writebt") == 0) {
				if (val.compare("true") == 0)
					writebt = true;
			} else if (key.compare("btpath") == 0) {
				bt_path = val;
			} else if (key.compare("convergence") == 0) {
				convergence = atoi(val.c_str());
			} else if (key.compare("location") == 0) {
				if (val.compare("indoor") == 0) {
					is_indoor = true;
				}
			} else if (key.compare("writecsv") == 0) {
				if (val.compare("true") == 0) {
					writecsv = true;
				}
			} else if (key.compare("minangle") == 0) {
				angle_min = atoi(val.c_str())*PI/180.0;
			} else if (key.compare("maxangle") == 0) {
				angle_max = atoi(val.c_str())*PI/180.0;
			}

		} catch (...) {
			std::cout << "unkown parameter:" << key << std::endl;
		}
	}

	fh.close();

	return TRUE;
}

CReader::~CReader()
{
	// release sensor
	mtx_da.lock();
	releaseSensor();
	mtx_da.unlock();

	usleep(200);

	logfile.close();

	std::stringstream path;

	path << "/home/pi/SICKRPi-Scanner/" << ss.str();

	pid_t pid;

	if((pid = fork()) < 0) {
		fprintf(stderr, "Fehler... %s\n", strerror(errno));
	}
	else if(pid == 0) {
		/* Kindprozess */
		execl("/usr/bin/sudo", "cp", "/bin/cp", path.str().c_str(), "/media/usb0/", NULL);
	}
	else {
		/* Elternprozess */
	// turn off imu leds
	imu_leds_off(&imu);

	// release ipcon
	imu_set_quaternion_period(&imu, 0);
	imu_set_angular_velocity_period(&imu, 0);
	imu_destroy(&imu);
	ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
	}
	//ret = execl("/usr/bin/sudo", "rm", "/bin/rm", path.str().c_str(), NULL);
}

// TINKERFORGE

void cb_connected(uint8_t connect_reason, void *user_data)
{
    // ...then trigger enumerate
    ipcon_enumerate(&ipcon);
}

int imu_cb = 0;
double yaw = 0, pitch = 0, roll = 0;
<commit_msg>use config path variable<commit_after>/*
 * CReader.cpp
 * save laser data to binary file
 *
 * Created on: 22.12.2014
 * Author: Markus
*/
#include <iostream>
#include <string>
#include "CReader.h"
#include <sstream>
#include <fstream>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <math.h>
#include <list>

#include <octomap/octomap.h>
#include <octomap/OcTree.h>
#include <octomap/Pointcloud.h>
#include "time.h"
#include "ip_connection.h"
#include "brick_imu.h"

#include <boost/chrono/thread_clock.hpp>

using namespace boost::chrono;

//
#define PI 3.14159265

#define CONFIG "/media/usb0/config.cfg"
#define HOST "localhost"
#define PORT 4223

#define DEBUG 1

std::fstream logfile;

boost::mutex mtx_da;
std::stringstream ss;

int data_av = 0;
bool is_imu_connected;

using std::string;
using namespace octomap;

IPConnection ipcon;
IMU imu;

// trim a string
const std::string trim( const std::string& s)
{
	std::string::size_type first = s.find_first_not_of( ' ');
	if( first == std::string::npos) {
		return std::string();
	}
	else {
		std::string::size_type last = s.find_last_not_of( ' '); /// must succeed
		return s.substr( first, last - first + 1);
	}
}

void debugMessage(int level, string message)
{
	if (level >= DEBUG)
		std::cerr << message << std::endl;
	return;
}

// constructor
CReader::CReader()
{
	scancycles = 0;
	is_imu_connected = false;

	ip_adress = "192.168.0.1";
	port = 12002;
	convergence = 0;

	// set angle filter
	angle_max = 0*PI/180; // scanner max 50
	angle_min = -60*PI/180; // scanner min -60

	maxmin = false;
	duration = false;
	writebt = false;
	writecsv = false;
	is_indoor = false;

	if (readConfig())
		debugMessage(2,"config successfully readed!");
	else
		debugMessage(2, "no config file!");

	return;
}

// read config file
int CReader::readConfig()
{
	std::ifstream fh;
	std::string line;
	size_t pos = 0;
	std::string key, val;

	fh.open(CONFIG, std::ios::in);

	if (!fh.is_open()) {
		fh.open("config.cfg", std::ios::in);
		if (!fh.is_open()) {
			return FALSE;
		}
	}

	debugMessage(2, "read config...");

	while (getline(fh,line)) {
		pos = line.find("=");
		key = trim(line.substr(0,pos));
		val = trim(line.substr(pos+1));

		try {
			if (key.compare("ip") == 0) {
				ip_adress = val;
			} else if (key.compare("port") == 0) {
				port = atoi(val.c_str());
			} else if (key.compare("maxmin") == 0) {
				if (val.compare("true") == 0)
					maxmin = true;
				else
					maxmin = false;
			} else if (key.compare("writebt") == 0) {
				if (val.compare("true") == 0)
					writebt = true;
			} else if (key.compare("btpath") == 0) {
				bt_path = val;
			} else if (key.compare("convergence") == 0) {
				convergence = atoi(val.c_str());
			} else if (key.compare("location") == 0) {
				if (val.compare("indoor") == 0) {
					is_indoor = true;
				}
			} else if (key.compare("writecsv") == 0) {
				if (val.compare("true") == 0) {
					writecsv = true;
				}
			} else if (key.compare("minangle") == 0) {
				angle_min = atoi(val.c_str())*PI/180.0;
			} else if (key.compare("maxangle") == 0) {
				angle_max = atoi(val.c_str())*PI/180.0;
			}

		} catch (...) {
			std::cout << "unkown parameter:" << key << std::endl;
		}
	}

	fh.close();

	return TRUE;
}

CReader::~CReader()
{
	// release sensor
	mtx_da.lock();
	releaseSensor();
	mtx_da.unlock();

	usleep(200);

	logfile.close();

	std::stringstream path;

	path << "/home/pi/SICKRPi-Scanner/" << ss.str();

	pid_t pid;

	if((pid = fork()) < 0) {
		fprintf(stderr, "Fehler... %s\n", strerror(errno));
	}
	else if(pid == 0) {
		/* Kindprozess */ "/media/usb0/"
		execl("/usr/bin/sudo", "cp", "/bin/cp", path.str().c_str(), bt_path.c_str(), NULL);
	}
	else {
		/* Elternprozess */
	// turn off imu leds
	imu_leds_off(&imu);

	// release ipcon
	imu_set_quaternion_period(&imu, 0);
	imu_set_angular_velocity_period(&imu, 0);
	imu_destroy(&imu);
	ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
	}
	//ret = execl("/usr/bin/sudo", "rm", "/bin/rm", path.str().c_str(), NULL);
}

// TINKERFORGE

void cb_connected(uint8_t connect_reason, void *user_data)
{
    // ...then trigger enumerate
    ipcon_enumerate(&ipcon);
}

int imu_cb = 0;
double yaw = 0, pitch = 0, roll = 0;

{'fraction_non_alphanumeric': 0.1881611791275407, 'fraction_numerical': 0.0223463687150838, 'mean_word_length': 3.5382955771305284, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4068271', 'n_tokens_mistral': 3743, 'n_tokens_neox': 3342, 'n_words': 740}