Skip to content

Commit

Permalink
Modified the Robot state's variables: the method setState saves the t…
Browse files Browse the repository at this point in the history
…ime stamp in lastTimeActive.
  • Loading branch information
gustavoiha committed Feb 24, 2016
1 parent 6287131 commit 984c1bd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
25 changes: 12 additions & 13 deletions commander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Thread threadSerial(threadSerial_run, 50);
void threadNRF_run();
Thread threadNRF(threadNRF_run, 1000); // Starts listening after 1sec, then set itself to 5ms

long lastMessageTimestamp = 0;

// ====================================
// INITIALIZATION
Expand Down Expand Up @@ -201,7 +200,7 @@ void threadIddleDetection_run(){
if(Robot::state != ACTIVE)
return;

if(millis() - lastMessageTimestamp > RADIO_TIMEOUT_TO_IDDLE){
if(millis() - Robot::lastTimeActive > RADIO_TIMEOUT_TO_IDDLE){
Robot::setState(IDDLE);
Robot::doBeep(Robot::getRobotID(), 50);
}
Expand Down Expand Up @@ -270,17 +269,17 @@ void threadNRF_run(){
radio->send(radioBufferOut, 3);
radio->waitPacketSent();

// Got message. Robot is now Active if message was an action message
if(Robot::alarm == NONE){
if(Robot::state == IDDLE && activate){
Robot::setState(ACTIVE);
Robot::doBeep(1, 100);
}
// Got message. Robot is now Active if message was an action message
if(Robot::alarm == NONE){
if(Robot::state == IDDLE && activate){
Robot::setState(ACTIVE);
Robot::doBeep(1, 100);
}

// Save timestamp of message
lastMessageTimestamp = millis();
// Serial.print(" ");
// Serial.print(lastMessageTimestamp - start);
// Serial.print(" ms\n");
// Save timestamp of message
Robot::lastTimeActive = millis();
// Serial.print(" ");
// Serial.print(lastMessageTimestamp - start);
// Serial.print(" ms\n");
}
}
6 changes: 5 additions & 1 deletion robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ int Robot::getRobotID(){

RobotState Robot::state = IDDLE;
RobotAlarm Robot::alarm = NONE;
Robot::lastTimeActive = 0;

// Sets the robot's state
void Robot::setState(RobotState _state){
state = _state;

if(state == IDDLE)
if(_state == ACTIVE)
Robot::lastTimeActive = millis();
else if(state == IDDLE)
Motors::stop();
};

Expand Down
5 changes: 5 additions & 0 deletions robot.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class Robot{
*/
static RobotState state;

/*
Last time when robot was Active
*/
static unsigned long lastTimeActive;

/*
Current alarm of the robot (Errors)
*/
Expand Down

0 comments on commit 984c1bd

Please sign in to comment.