Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from OpenStratos/feature/temperature
Browse files Browse the repository at this point in the history
Fixed #30. Tests in Raspberry Pi pass.
  • Loading branch information
Razican committed Feb 21, 2015
2 parents bacdf05 + c192313 commit a798d54
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 100 deletions.
17 changes: 8 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
language: erlang # We are creating ARM emulation on travis, so no need for language features

env: GUEST_DEPENDENCIES="git g++ make sudo autoconf automake m4"
language: cpp
compiler: gcc

before_install:
- git submodule update --init --recursive
- git submodule update --init --recursive > /dev/null
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
- sudo apt-get update -qq
- sudo apt-get install -qq -y qemu-user-static binfmt-support sbuild
- wget https://launchpad.net/ubuntu/+archive/primary/+files/debootstrap_1.0.59ubuntu0.2_all.deb
- sudo dpkg -i debootstrap_1.0.59ubuntu0.2_all.deb

install:
- "bash -ex ./testing/travis-install.sh"
- sudo apt-get install -qq autoconf automake m4 gcc-4.9 g++-4.9 > /dev/null
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 90
- "bash -ex ./install-wiringpi.sh"

script:
- "bash -ex ./testing/travis-test.sh"
- "bash -ex ./build.sh"

branches:
only:
Expand Down
6 changes: 3 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
bin_PROGRAMS = openstratos
openstratos_SOURCES = openstratos.cpp camera/Camera.cpp gps/GPS.cpp serial/Serial.cpp battery/Battery.cpp
openstratos_SOURCES = openstratos.cpp camera/Camera.cpp gps/GPS.cpp serial/Serial.cpp battery/Battery.cpp temperature/Temperature.cpp
openstratos_LDADD = -lwiringPi
openstratos_CPPFLAGS = -std=c++11 -Wno-unused-result

EXTRA_PROGRAMS = openstratosRoot utesting
openstratosRoot_SOURCES = openstratos-root.cpp camera/Camera.cpp gps/GPS.cpp serial/Serial.cpp battery/Battery.cpp
openstratosRoot_SOURCES = openstratos-root.cpp camera/Camera.cpp gps/GPS.cpp serial/Serial.cpp battery/Battery.cpp temperature/Temperature.cpp
openstratosRoot_CPPFLAGS = -std=c++11 -Wno-unused-result

utesting_SOURCES = testing/testing.cpp camera/Camera.cpp gps/GPS.cpp serial/Serial.cpp battery/Battery.cpp
utesting_SOURCES = testing/testing.cpp camera/Camera.cpp gps/GPS.cpp serial/Serial.cpp battery/Battery.cpp temperature/Temperature.cpp
utesting_CPPFLAGS = -std=c++11 -Itesting/bandit -Wno-unused-result -DOS_TESTING
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ automake --add-missing
autoconf
./configure
make utesting

printf "\n\n----- Starting unit tests -----\n\n"
./utesting
1 change: 1 addition & 0 deletions camera/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void Camera::record(int time)
#ifndef RASPIVID
command = "";
#endif

system(command.c_str());
this->recording = true;

Expand Down
1 change: 1 addition & 0 deletions camera/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

bool recording = false;
public:
Camera(Camera& copy) = delete;
static Camera& get_instance();

void record(int time);
Expand Down
3 changes: 0 additions & 3 deletions config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@
/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1

/* Define to 1 if the system has the type `_Bool'. */
/* #undef HAVE__BOOL */

/* Name of package */
#define PACKAGE "openstratos"

Expand Down
1 change: 0 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ AC_LANG([C++])
AX_CXX_COMPILE_STDCXX_11()

# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_SIZE_T

Expand Down
3 changes: 3 additions & 0 deletions constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#define ERR_INT 2
#define ERR_PWD 3

#define TEMP_VIN 5
#define TEMP_R 500

#define BAT_MAX 8.4
#define BAT_MIN 7.4
#define BAT_R1 3300
Expand Down
4 changes: 2 additions & 2 deletions gps/GPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <cstdio>

using namespace std;
using namespace os;

GPS& GPS::get_instance()
Expand All @@ -22,8 +23,7 @@ GPS::~GPS()

void GPS::initialize(const string& serial_URL)
{
Serial serial(serial_URL, 9600, "\r\n", bind(&GPS::parse, this, placeholders::_1));
this->serial = serial;
this->serial.initialize(serial_URL, 9600, "\r\n", bind(&GPS::parse, this, placeholders::_1));

#ifndef OS_TESTING
this->serial.send_frame("$PMTK220,100*2F");
Expand Down
1 change: 1 addition & 0 deletions gps/GPS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
void parse_RMC(const string& frame);

public:
GPS(GPS& copy) = delete;
static GPS& get_instance();

tm* get_time() {return &this->time;}
Expand Down
7 changes: 7 additions & 0 deletions install-wiringpi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

printf "Installing WiringPi\n"
git clone https://github.com/OpenStratos/WiringPi.git > /dev/null
cd WiringPi
./build > /dev/null
cd ..
71 changes: 40 additions & 31 deletions serial/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
#include <functional>
#include <cstdint>
#include <string>
#include <iostream>

using namespace std;
using namespace os;

Serial::Serial(const string& url, int baud, const string endl, function<uint_fast8_t(const string&)>)
void Serial::initialize(const string& url, int baud, const string endl, function<uint_fast8_t(const string&)>)
{
this->listener = listener;
this->endl = endl;
#ifndef OS_TESTING
this->fd = serialOpen(url.c_str(), baud);
this->open = true;

thread t(&Serial::serial_thread, this);
t.detach();
#endif

this->open = true;
this->stopped = false;
thread t(&Serial::serial_thread, this);
t.detach();
}

Serial::~Serial()
Expand All @@ -33,39 +36,41 @@ void Serial::serial_thread()

while(this->open)
{
this_thread::sleep_for(chrono::milliseconds(5));
int available = serialDataAvail(this->fd);
#ifndef OS_TESTING
int available = serialDataAvail(this->fd);

if (available > 0)
{
for (int i = 0; i < available; i++)
if (available > 0)
{
char c = serialGetchar(this->fd);
frame += c;

if (c == this->endl[endl_pos]) ++endl_pos;
if (endl_pos == this->endl.length())
for (int i = 0; i < available; i++)
{
frame = frame.substr(0, frame.length()-endl.length());
char c = serialGetchar(this->fd);
frame += c;

if (this->is_valid(frame)) this->listener(frame);
// TODO decide what to do in case of non valid frame
if (c == this->endl[endl_pos]) ++endl_pos;
if (endl_pos == this->endl.length())
{
frame = frame.substr(0, frame.length()-endl.length());

frame = "";
endl_pos = 0;
this_thread::sleep_for(chrono::milliseconds(50));
if (this->is_valid(frame)) this->listener(frame);
// TODO decide what to do in case of non valid frame

frame = "";
endl_pos = 0;
this_thread::sleep_for(chrono::milliseconds(50));
}
}
}
}
else if (available == 0)
{
this_thread::sleep_for(chrono::milliseconds(25));
}
else if (available < 0)
{
// TODO log error
}
else if (available == 0)
{
this_thread::sleep_for(chrono::milliseconds(25));
}
else if (available < 0)
{
// TODO log error
}
#endif
}
this->stopped = true;
}

uint_fast8_t Serial::send_frame(string frame)
Expand All @@ -84,7 +89,11 @@ uint_fast8_t Serial::send_frame(string frame)
void Serial::close()
{
this->open = false;
serialClose(this->fd);
while( ! this->stopped);

#ifndef OS_TESTING
serialClose(this->fd);
#endif
}

bool Serial::is_valid(string frame)
Expand Down
7 changes: 5 additions & 2 deletions serial/Serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <functional>
#include <cstdint>
#include <string>
#include <atomic>
using namespace std;

namespace os {
Expand All @@ -12,18 +13,20 @@
private:
int fd;
function<uint_fast8_t(const string&)> listener;
bool open;
atomic_bool open;
atomic_bool stopped;
string endl;

void serial_thread();
public:
Serial(const string& serial_URL, int baud, const string endl, function<uint_fast8_t(const string&)>);
Serial() = default;
Serial(Serial& copy) = delete;
~Serial();

uint_fast8_t send_frame(string frame);
void close();
bool is_valid(string frame);
void initialize(const string& serial_URL, int baud, const string endl, function<uint_fast8_t(const string&)>);
};
}
#endif
69 changes: 69 additions & 0 deletions temperature/Temperature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "Temperature.hpp"
#include "../constants.hpp"
#include <wiringPiI2C.h>
#include <string>
#include <thread>
#include <chrono>

using namespace std;
using namespace os;

Temperature::~Temperature()
{
if (this->reading)
this->stop_reading();
}

Temperature::Temperature(const int address)
{
this->reading = false;
this->stopped = true;
#ifndef OS_TESTING
int fh = wiringPiI2CSetup(address);
if (fh != -1)
{
this->address = address;
this->filehandle = fh;
}
else
{
// TODO Log error
//printf("An error ocurred initializing I2C Temperature module\n");
}
#endif
}

void Temperature::start_reading()
{
if ( ! this->reading)
{
this->reading = true;
this->stopped = false;
thread t(&Temperature::read_temperature, this);
t.detach();
}
}

void Temperature::stop_reading()
{
this->reading = false;
while( ! this->stopped);
}

void Temperature::read_temperature()
{
while (this->reading)
{
#ifndef OS_TESTING
int value = wiringPiI2CRead(this->filehandle);
#else
int value = 16000;
#endif

float voltage = value * 5 / 32768; // 2^15
this->temperature = r_to_c(TEMP_R * (TEMP_VIN / voltage - 1));

this_thread::sleep_for(chrono::milliseconds(50));
}
this->stopped = true;
}
35 changes: 35 additions & 0 deletions temperature/Temperature.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef TEMPERATURE_H
#define TEMPERATURE_H

#include <atomic>
using namespace std;

namespace os {
class Temperature
{
private:
int address;
int filehandle;
float temperature;
atomic_bool reading;
atomic_bool stopped;

void read_temperature();
public:
Temperature(const int address);
~Temperature();
Temperature(Temperature& copy) = delete;

int get_temperature() {return this->temperature;}
void start_reading();
void stop_reading();
bool is_reading() {return this->reading;}
};
}

inline float r_to_c(float r)
{
float value = r - 1000;
return (value / 3.91 + value * value / 100000);
}
#endif
4 changes: 4 additions & 0 deletions testing/gps_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
describe("GPS", [](){

before_each([&](){
GPS::get_instance().initialize("");
});

it("Knots to m/s conversion test", [&](){
AssertThat(kt_to_mps(150), Is().EqualToWithDelta(77.1666667, 0.005));
AssertThat(kt_to_mps(75), Is().EqualToWithDelta(38.58333, 0.005));
Expand Down
Loading

0 comments on commit a798d54

Please sign in to comment.