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

Commit

Permalink
Merge branch 'release/1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed Sep 26, 2015
2 parents a391a99 + 1d474cd commit 0d752b3
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 77 deletions.
9 changes: 1 addition & 8 deletions camera/Camera.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "camera/Camera.h"

#include <cstdlib>
#include <cstdio>

#include <string>
#include <thread>
Expand Down Expand Up @@ -181,13 +180,7 @@ bool Camera::stop()

bool Camera::is_really_recording() const
{
FILE* process = popen("pgrep raspivid", "r");
char response[5];
fgets(response, 5, process);
pclose(process);

if (process == NULL) this->logger->log("Error checking if raspivid is really recording");
return (process != NULL && response != NULL);
return (0 == system("pidof -x raspivid > /dev/null"));
}

int os::get_file_count(const string& path)
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.68])
AC_INIT(OpenStratos, 1.0-rc1, https://github.com/OpenStratos/server/issues)
AC_INIT(OpenStratos, 1.0, https://github.com/OpenStratos/server/issues)
AC_CONFIG_SRCDIR([openstratos.cc])
AM_INIT_AUTOMAKE([subdir-objects])
AC_CONFIG_HEADERS([config.h])
Expand Down
2 changes: 1 addition & 1 deletion gps/GPS.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ bool GPS::is_valid(string frame)

void GPS::parse(const string& frame)
{
if (frame.length() > 1)
if (frame.length() > 1 && is_valid(frame))
{
this->frame_logger->log(frame);
string frame_type = frame.substr(1, frame.find_first_of(',')-1);
Expand Down
63 changes: 33 additions & 30 deletions gsm/GSM.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ bool GSM::send_SMS(const string& message, const string& number)
this->serial->read_line(60); // Eat prompt (timeout 60 seconds)

// Read +CMGS response
response = this->serial->read_line();
string response = this->serial->read_line();
this->command_logger->log("Received: '"+response+"'");
if (response.find("+CMGS") == string::npos)
{
Expand All @@ -170,7 +170,7 @@ bool GSM::send_SMS(const string& message, const string& number)
this->serial->read_line(); // Eat new line

// Read OK (timeout 10 seconds)
string response = this->serial->read_line(10);
response = this->serial->read_line(10);
this->command_logger->log("Received: '"+response+"'");
if (response != "OK")
{
Expand Down Expand Up @@ -236,7 +236,14 @@ bool GSM::get_location(double& latitude, double& longitude)
return false;
}

if (this->send_command_read("AT+SAPBR=1,1") != "OK")
this->serial->println("AT+SAPBR=1,1");
this->command_logger->log("Sent: 'AT+SAPBR=1,1'");
this->serial->read_line(2); // Eat message echo

string response = this->serial->read_line();
this->command_logger->log("Received: '"+ response +"'");

if (response != "OK")
{
this->logger->log("Error getting location on 'AT+SAPBR=1,1' response.");
if (this->send_command_read("AT+SAPBR=0,1") != "OK")
Expand All @@ -248,9 +255,25 @@ bool GSM::get_location(double& latitude, double& longitude)
return false;
}

string response = this->send_command_read("AT+CIPGSMLOC=1,1");
this->serial->println("AT+CIPGSMLOC=1,1");
this->serial->read_line(10); // Eat message echo
response = this->serial->read_line();
this->command_logger->log("Received: '"+ response +"'");

stringstream ss(response);
string data;
vector<string> s_data;

// We put all fields in a vector
while(getline(ss, data, ',')) s_data.push_back(data);

latitude = stod(s_data[2]);
longitude = stod(s_data[1]);

this->serial->read_line(); // Eat new line
if (response == "ERROR" || this->serial->read_line() != "OK")
response = this->serial->read_line();
this->command_logger->log("Received: '"+ response +"'");
if (response == "ERROR" || response != "OK")
{
this->logger->log("Error getting location on 'AT+CIPGSMLOC=1,1' response.");
if (this->send_command_read("AT+SAPBR=0,1") != "OK")
Expand All @@ -262,23 +285,16 @@ bool GSM::get_location(double& latitude, double& longitude)
return false;
}

if (this->send_command_read("AT+SAPBR=0,1") != "OK")
this->serial->println("AT+SAPBR=0,1");
this->serial->read_line(2); // Eat message echo
response = this->serial->read_line();
this->command_logger->log("Received: '"+ response +"'");
if (response != "OK")
this->logger->log("Error turning GPRS down.");
else
this->logger->log("GPRS off.");

this->occupied = false;

stringstream ss(response);
string data;
vector<string> s_data;

// We put all fields in a vector
while(getline(ss, data, ',')) s_data.push_back(data);

latitude = stod(s_data[2]);
longitude = stod(s_data[1]);

return true;
}

Expand Down Expand Up @@ -385,19 +401,6 @@ bool GSM::turn_off() const
}
}

bool GSM::init_GPRS() const
{
return (this->send_command_read("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"") == "OK" &&
this->send_command_read("AT+SAPBR=3,1,\"APN\",\"" + string(GSM_LOC_SERV) + ";") == "OK" &&
this->send_command_read("AT+SAPBR=1,1") == "OK" &
this->send_command_read("AT+SAPBR=2,1") == "OK");
}

bool GSM::tear_down_GPRS() const
{
return this->send_command_read("AT+SAPBR=0,1") == "OK";
}

const string GSM::send_command_read(const string& command) const
{
this->command_logger->log("Sent: '"+command+"'");
Expand Down
91 changes: 56 additions & 35 deletions openstratos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ void os::safe_mode()
logger->log("Mayday messages sent.");

logger->log("Initializing GPS...");
count = 0;
while ( ! GPS::get_instance().initialize() && ++count < 5)
logger->log("GPS initialization error.");

Expand Down Expand Up @@ -290,7 +291,9 @@ void os::main_while(Logger* logger, State* state)
}
else if (*state == FIX_ACQUIRED)
{
this_thread::sleep_for(5min); //
this_thread::sleep_for(2min);
logger->log("Sleeping 2 minutes for fix stabilization.");

start_recording(logger);
send_init_sms(logger);
*state = set_state(WAITING_LAUNCH);
Expand Down Expand Up @@ -394,7 +397,8 @@ void os::initialize(Logger* logger, tm* now)

logger->log("Checking batteries...");
double main_battery, gsm_battery;
if ( ! GSM::get_instance().get_battery_status(main_battery, gsm_battery))
if ( ! GSM::get_instance().get_battery_status(main_battery, gsm_battery) &&
! GSM::get_instance().get_battery_status(main_battery, gsm_battery))
{
logger->log("Error checking batteries.");

Expand All @@ -421,7 +425,7 @@ void os::initialize(Logger* logger, tm* now)
logger->log("Batteries checked => Main battery: "+ (main_battery > -1 ? to_string(main_battery*100)+"%" : "disconnected") +
" - GSM battery: "+ to_string(gsm_battery*100) +"%");

if ((main_battery < 0.95 && main_battery > -1) || gsm_battery < 0.95)
if ((main_battery < 0.9 && main_battery > -1) || gsm_battery < 0.85)
{
logger->log("Error: Not enough battery.");

Expand Down Expand Up @@ -563,11 +567,11 @@ void os::send_init_sms(Logger* logger)
"Init: OK\r\nAlt: "+ to_string((int) GPS::get_instance().get_altitude()) +
" m\r\nLat: "+ to_string(GPS::get_instance().get_latitude()) +"\r\n"+
"Lon: "+ to_string(GPS::get_instance().get_longitude()) +"\r\n"+
(bat_status ? "Main bat: "+ to_string((int) main_battery*100) +"%\r\n"+
"GSM bat: "+ to_string((int) gsm_battery*100) +"%\r\n" : "Bat: ERR\r\n") +
(bat_status ? "Main bat: "+ to_string((int) (main_battery*100)) +"%\r\n"+
"GSM bat: "+ to_string((int) (gsm_battery*100)) +"%\r\n" : "Bat: ERR\r\n") +
"Fix: "+ (GPS::get_instance().is_fixed() ? "OK" : "ERR") +
"Sat: "+ to_string(GPS::get_instance().get_satellites()) +
"Waiting Launch", SMS_PHONE))
"\r\nSat: "+ to_string(GPS::get_instance().get_satellites()) +
"\r\nWaiting Launch", SMS_PHONE))
{
logger->log("Error sending initialization SMS.");

Expand Down Expand Up @@ -611,7 +615,7 @@ void os::wait_launch(Logger* logger, double& launch_altitude)
this_thread::sleep_for(2min);
#endif
#ifdef REAL_SIM
this_thread::sleep_for(20min);
this_thread::sleep_for(10min);
#endif

while ( ! has_launched(launch_altitude))
Expand All @@ -636,10 +640,10 @@ void os::go_up(Logger* logger, double launch_altitude)
"Launch\r\nAlt: "+ to_string((int) launch_altitude) +
" m\r\nLat: "+ to_string(GPS::get_instance().get_latitude()) +"\r\n"+
"Lon: "+ to_string(GPS::get_instance().get_longitude()) +"\r\n"+
(bat_status ? "Main bat: "+ to_string((int) main_battery*100) +"%\r\n"+
"GSM bat: "+ to_string((int) gsm_battery*100) +"%\r\n" : "Bat: ERR\r\n") +
(bat_status ? "Main bat: "+ to_string((int) (main_battery*100)) +"%\r\n"+
"GSM bat: "+ to_string((int) (gsm_battery*100)) +"%\r\n" : "Bat: ERR\r\n") +
"Fix: "+ (GPS::get_instance().is_fixed() ? "OK" : "ERR") +
"Sat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
"\r\nSat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
{
logger->log("Error sending launch confirmation SMS.");
}
Expand Down Expand Up @@ -673,10 +677,19 @@ void os::go_up(Logger* logger, double launch_altitude)
"Alt: "+ to_string((int) GPS::get_instance().get_altitude()) +
" m\r\nLat: "+ to_string(GPS::get_instance().get_latitude()) +"\r\n"+
"Lon: "+ to_string(GPS::get_instance().get_longitude()) +"\r\n"+
(bat_status ? "Main bat: "+ to_string((int) main_battery*100) +"%\r\n"+
"GSM bat: "+ to_string((int) gsm_battery*100) +"%\r\n" : "Bat: ERR\r\n") +
(bat_status ? "Main bat: "+ to_string((int) (main_battery*100)) +"%\r\n"+
"GSM bat: "+ to_string((int) (gsm_battery*100)) +"%\r\n" : "Bat: ERR\r\n") +
"Fix: "+ (GPS::get_instance().is_fixed() ? "OK" : "ERR") +
"Sat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
"\r\nSat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE) &&
// Second attempt
! GSM::get_instance().send_SMS(
"Alt: "+ to_string((int) GPS::get_instance().get_altitude()) +
" m\r\nLat: "+ to_string(GPS::get_instance().get_latitude()) +"\r\n"+
"Lon: "+ to_string(GPS::get_instance().get_longitude()) +"\r\n"+
(bat_status ? "Main bat: "+ to_string((int) (main_battery*100)) +"%\r\n"+
"GSM bat: "+ to_string((int) (gsm_battery*100)) +"%\r\n" : "Bat: ERR\r\n") +
"Fix: "+ (GPS::get_instance().is_fixed() ? "OK" : "ERR") +
"\r\nSat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
{
logger->log("Error sending \"going up\" SMS.");
}
Expand Down Expand Up @@ -961,10 +974,10 @@ void os::go_down(Logger* logger)
"Alt: "+ to_string((int) GPS::get_instance().get_altitude()) +
" m\r\nLat: "+ to_string(GPS::get_instance().get_latitude()) +"\r\n"+
"Lon: "+ to_string(GPS::get_instance().get_longitude()) +"\r\n"+
(bat_status ? "Main bat: "+ to_string((int) main_battery*100) +"%\r\n"+
"GSM bat: "+ to_string((int) gsm_battery*100) +"%\r\n" : "Bat: ERR\r\n") +
(bat_status ? "Main bat: "+ to_string((int) (main_battery*100)) +"%\r\n"+
"GSM bat: "+ to_string((int) (gsm_battery*100)) +"%\r\n" : "Bat: ERR\r\n") +
"Fix: "+ (GPS::get_instance().is_fixed() ? "OK" : "ERR") +
"Sat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
"\r\nSat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
{
logger->log("Error sending first SMS.");
}
Expand Down Expand Up @@ -1023,10 +1036,10 @@ void os::go_down(Logger* logger)
"Alt: "+ to_string((int) GPS::get_instance().get_altitude()) +
" m\r\nLat: "+ to_string(GPS::get_instance().get_latitude()) +"\r\n"+
"Lon: "+ to_string(GPS::get_instance().get_longitude()) +"\r\n"+
(bat_status ? "Main bat: "+ to_string((int) main_battery*100) +"%\r\n"+
"GSM bat: "+ to_string((int) gsm_battery*100) +"%\r\n" : "Bat: ERR\r\n") +
(bat_status ? "Main bat: "+ to_string((int) (main_battery*100)) +"%\r\n"+
"GSM bat: "+ to_string((int) (gsm_battery*100)) +"%\r\n" : "Bat: ERR\r\n") +
"Fix: "+ (GPS::get_instance().is_fixed() ? "OK" : "ERR") +
"Sat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
"\r\nSat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
{
logger->log("Error sending second SMS.");
}
Expand Down Expand Up @@ -1085,10 +1098,10 @@ void os::go_down(Logger* logger)
"Alt: "+ to_string((int) GPS::get_instance().get_altitude()) +
" m\r\nLat: "+ to_string(GPS::get_instance().get_latitude()) +"\r\n"+
"Lon: "+ to_string(GPS::get_instance().get_longitude()) +"\r\n"+
(bat_status ? "Main bat: "+ to_string((int) main_battery*100) +"%\r\n"+
"GSM bat: "+ to_string((int) gsm_battery*100) +"%\r\n" : "Bat: ERR\r\n") +
(bat_status ? "Main bat: "+ to_string((int) (main_battery*100)) +"%\r\n"+
"GSM bat: "+ to_string((int) (gsm_battery*100)) +"%\r\n" : "Bat: ERR\r\n") +
"Fix: "+ (GPS::get_instance().is_fixed() ? "OK" : "ERR") +
"Sat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
"\r\nSat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
{
logger->log("Error sending third SMS.");
}
Expand Down Expand Up @@ -1125,7 +1138,8 @@ void os::land(Logger* logger)
bool bat_status = false;

logger->log("Getting battery values...");
if (bat_status = GSM::get_instance().get_battery_status(main_battery, gsm_battery))
if (bat_status = (GSM::get_instance().get_battery_status(main_battery, gsm_battery) ||
GSM::get_instance().get_battery_status(main_battery, gsm_battery)))
logger->log("Battery status received.");
else
logger->log("Error getting battery status.");
Expand All @@ -1135,10 +1149,10 @@ void os::land(Logger* logger)
"Landed\r\nAlt: "+ to_string((int) GPS::get_instance().get_altitude()) +
" m\r\nLat: "+ to_string(GPS::get_instance().get_latitude()) +"\r\n"+
"Lon: "+ to_string(GPS::get_instance().get_longitude()) +"\r\n"+
(bat_status ? "Main bat: "+ to_string((int) main_battery*100) +"%\r\n"+
"GSM bat: "+ to_string((int) gsm_battery*100) +"%\r\n" : "Bat: ERR\r\n") +
(bat_status ? "Main bat: "+ to_string((int) (main_battery*100)) +"%\r\n"+
"GSM bat: "+ to_string((int) (gsm_battery*100)) +"%\r\n" : "Bat: ERR\r\n") +
"Fix: "+ (GPS::get_instance().is_fixed() ? "OK" : "ERR") +
"Sat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
"\r\nSat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE))
{
logger->log("Error sending landed SMS. Trying again in 10 minutes...");
}
Expand All @@ -1149,28 +1163,35 @@ void os::land(Logger* logger)

this_thread::sleep_for(10min);

logger->log("Getting battery values...");
if (bat_status = (GSM::get_instance().get_battery_status(main_battery, gsm_battery) ||
GSM::get_instance().get_battery_status(main_battery, gsm_battery)))
logger->log("Battery status received.");
else
logger->log("Error getting battery status.");

logger->log("Sending second landed SMS...");
while ( ! GSM::get_instance().send_SMS(
while (( ! GSM::get_instance().send_SMS(
"Landed\r\nAlt: "+ to_string((int) GPS::get_instance().get_altitude()) +
" m\r\nLat: "+ to_string(GPS::get_instance().get_latitude()) +"\r\n"+
"Lon: "+ to_string(GPS::get_instance().get_longitude()) +"\r\n"+
(bat_status ? "Main bat: "+ to_string((int) main_battery*100) +"%\r\n"+
"GSM bat: "+ to_string((int) gsm_battery*100) +"%\r\n" : "Bat: ERR\r\n") +
(bat_status ? "Main bat: "+ to_string((int) (main_battery*100)) +"%\r\n"+
"GSM bat: "+ to_string((int) (gsm_battery*100)) +"%\r\n" : "Bat: ERR\r\n") +
"Fix: "+ (GPS::get_instance().is_fixed() ? "OK" : "ERR") +
"Sat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE) ||
! GPS::get_instance().is_fixed() &&
"\r\nSat: "+ to_string(GPS::get_instance().get_satellites()), SMS_PHONE) ||
! GPS::get_instance().is_fixed()) &&
(main_battery >= 0 || main_battery < -1) && gsm_battery >= 0)
{
logger->log("Error sending second SMS or GPS without fix, trying again in 5 minutes.");
this_thread::sleep_for(5min);
GSM::get_instance().get_battery_status(main_battery, gsm_battery);
}

if ((main_battery < 0.05 && main_battery > -1) || gsm_battery < 0.05)
if ((main_battery < 0 && main_battery > -1) || gsm_battery < 0)
{
logger->log("Not enough battery.");
logger->log("Main battery: "+ to_string(main_battery) +
"% - GSM battery: "+ to_string(gsm_battery) +"%");
logger->log("Main battery: "+ to_string(main_battery*100) +
"% - GSM battery: "+ to_string(gsm_battery*100) +"%");
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ echo "[`date`] Starting OpenStratos..." >> /root/control.log
sleep 5

while true; do
(pgrep procname && sleep 60) ||
(echo "[`date`] Process not running, restarting..." >> /root/control.log; shutdown -r now)
(pgrep openstratos && sleep 60) ||
(echo "[`date`] Process not running, restarting..." >> /root/control.log;
shutdown -r now; sleep 5)
done

0 comments on commit 0d752b3

Please sign in to comment.