Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2025 RPi state machine and player control #252

Merged
merged 121 commits into from
Feb 19, 2025
Merged
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
4c0d9a0
slight
chousallen Jan 12, 2025
68e51da
edit custom json
chousallen Jan 12, 2025
80636c3
trivial compile problem of unused function
chousallen Jan 12, 2025
032102f
modify parse command
chousallen Jan 12, 2025
c47577b
add inline to try to avoid compile error
chousallen Jan 12, 2025
f4df907
add all inline
chousallen Jan 12, 2025
707f0bd
comment multiple def of func parse_command
chousallen Jan 12, 2025
840f241
rm inline for parse_command in FSM_Common
chousallen Jan 12, 2025
dacbe91
add extern for parse_command in FSM_Common
chousallen Jan 12, 2025
070b7d3
mv def of parse_command
chousallen Jan 12, 2025
836ee6f
test print
chousallen Jan 13, 2025
734b0f5
test fprintf
Jan 13, 2025
a536c4d
printf and fprintf failed, use fstream
Jan 13, 2025
68d83bb
trivial
Jan 13, 2025
6e438d8
trivial
Jan 13, 2025
9cf1ed6
compress failed
Jan 13, 2025
9b8c555
give up to log getCurrentState
Jan 13, 2025
ce8bfa6
add some err log, monitor by journalctl -f -u player.service
Jan 14, 2025
965cfb5
delayDisplay(true) once when enterDELAY but still delayDisplay(false)…
Jan 15, 2025
14ac83c
check if log file can open
Jan 15, 2025
8e698dc
slightly change
Jan 13, 2025
a561ef6
use log file
Jan 15, 2025
3cca088
compress getCurrentState log
Jan 15, 2025
0821cce
ignore log of getCurrentState
Jan 15, 2025
8f31e45
test compressed getCurrent State log
Jan 13, 2025
9be29e1
remove git identifier
Jan 15, 2025
a56dbad
shellscript for new rpi
Jan 13, 2025
08b089f
test wei's playerctl.cpp
Jan 15, 2025
d921b18
no log file
Jan 16, 2025
62975bd
using namespace std
Jan 16, 2025
2ce8e47
log constructor and parse_command
Jan 16, 2025
93e6827
remove all fsm_log
Jan 16, 2025
e375a1c
start to replace state machine
Jan 16, 2025
c819973
fix cmake for new state machine
Jan 15, 2025
5a3bbd6
playerctl time modified
Williammm0011 Jan 16, 2025
a6d8066
u
Jan 16, 2025
b62d8ea
in FSM_Common, read unit is usec but not msec
Jan 16, 2025
c5d0ecc
cout, printf, cerr,... failed
Jan 13, 2025
83f8812
playerctl time modified
Williammm0011 Jan 16, 2025
aadd4b0
in FSM_Common, read unit is usec but not msec
Jan 16, 2025
79e10b9
solve dropped commit
chousallen Jan 16, 2025
ca04727
change function name
chousallen Jan 16, 2025
0d0ec9d
same for OFPlayer
chousallen Jan 16, 2025
8298efa
some function name
chousallen Jan 16, 2025
a412199
Please enter the commit message for your changes. Lines starting
chousallen Jan 17, 2025
303bdf7
move implemetation of parse_event to state_machine.cpp
chousallen Jan 17, 2025
dcc6bde
who controls the fifo
chousallen Jan 17, 2025
c35a427
define your own fifo path
chousallen Jan 17, 2025
40e20d8
oh, I fucked
chousallen Jan 17, 2025
f4fb862
mv def of fifo path to const.h
chousallen Jan 18, 2025
b7627a0
stderr log for state_machine.cpp
chousallen Jan 18, 2025
07258d0
more log
chousallen Jan 18, 2025
ebbe6f4
fix TAG redef
chousallen Jan 18, 2025
7f1df6a
fix TAG redef
chousallen Jan 18, 2025
998cf68
fix TAG redef
chousallen Jan 18, 2025
bcab09b
fix TAG redef
chousallen Jan 18, 2025
08e85f2
the success or fail on the fifo player_to_cmd should be determined by…
chousallen Jan 18, 2025
3645024
rm boardinfo.cpp
chousallen Jan 18, 2025
7ec2aa0
fix naming problem
chousallen Jan 18, 2025
c7d4a0d
playerctl cout removed
Williammm0011 Jan 19, 2025
6362039
comment of data
chousallen Jan 19, 2025
3aece91
observe cmd_buffer in playLoop to determine where to set data.stoptim…
chousallen Jan 19, 2025
4d5ec72
observe the message playerctl got
chousallen Jan 19, 2025
6b1da28
-ss -to added
Williammm0011 Jan 19, 2025
68638d7
cmd changed to ms-based
Williammm0011 Jan 19, 2025
ba7ae94
merge to wei_test for processing flags -ss and -to
chousallen Jan 19, 2025
a4ba392
fifo: player_to_cmd, fail if cmd cannot match an event or cannot proc…
chousallen Jan 19, 2025
13d3aa0
delay red time
Williammm0011 Jan 20, 2025
9c74cf1
add delay state
chousallen Jan 21, 2025
f43bf2f
readme
chousallen Jan 23, 2025
603c707
readme
chousallen Jan 23, 2025
6fc9a3e
maybe finished
chousallen Jan 24, 2025
a36b2da
add inline to avoid multiple definition
chousallen Jan 24, 2025
ee1ce27
adjust response message
chousallen Jan 24, 2025
aa37720
find reason of killed
chousallen Jan 24, 2025
5afd672
modify millisec_to_timeval()
chousallen Jan 24, 2025
41e2d86
should not resume in enterDELAY()
chousallen Jan 24, 2025
19f0cca
trivial
chousallen Jan 24, 2025
266d25b
check if delay time correct
chousallen Jan 24, 2025
f93bb48
dbg msg
chousallen Jan 24, 2025
b624992
move init msg from stdout to stderr
chousallen Jan 24, 2025
74cdf48
test is sendAll in loop
chousallen Jan 25, 2025
d0e32d5
by the dbg msg of controller, it seems that the status list is empty,…
chousallen Jan 27, 2025
46fa390
fix time_enter_play
chousallen Jan 27, 2025
87ef03e
fix execDELAY
chousallen Jan 27, 2025
35cb4d3
dbg msg in execDELAY
chousallen Jan 27, 2025
7f69814
record enter delay
chousallen Jan 27, 2025
f45dc20
compare with enter delay
chousallen Jan 27, 2025
cbf1430
fix timeval_tools compare error
chousallen Jan 27, 2025
f5ddac3
resume alias
Jan 15, 2025
8215b1c
add resume cmd
chousallen Jan 27, 2025
a90cba4
remove some dbg msg
chousallen Jan 27, 2025
0e0b9b0
cannot delay from pause
chousallen Jan 27, 2025
be7f352
rewrite data
chousallen Jan 27, 2025
932233d
rewrite data
chousallen Jan 27, 2025
201432c
start time stamp
chousallen Jan 27, 2025
e7e4f33
read delay display ratio
chousallen Jan 27, 2025
f05cf57
exec delay display
chousallen Jan 27, 2025
cb62a02
delay display
chousallen Jan 27, 2025
42b4ce9
delay display continuous
chousallen Jan 27, 2025
5fb5cef
timeval tools should */ double
chousallen Jan 27, 2025
4e80c52
check why no delay display
chousallen Jan 27, 2025
c7078a8
operator *
chousallen Jan 27, 2025
1cbd9df
operator
chousallen Jan 27, 2025
fd445fb
dbg timeval *
chousallen Jan 27, 2025
ceb394a
check set delay
chousallen Jan 27, 2025
bc43527
read delay display ratio
chousallen Jan 28, 2025
4cb59e4
good
Jan 15, 2025
40990ce
Merge branch 'wei_test' into test_fsm
chousallen Jan 29, 2025
e88d59f
read by exact delay display time
chousallen Jan 29, 2025
7c2e574
play meets segmentation fault
Jan 15, 2025
8eedca5
size_t, fix seg fault
Williammm0011 Jan 30, 2025
7dc1acb
response success fifo
chousallen Feb 5, 2025
0f7c965
remove excess \n
chousallen Feb 5, 2025
be42dc5
Merge branch 'test_fsm'
chousallen Feb 5, 2025
dfedbdb
remove original include of state machine
chousallen Feb 5, 2025
c5fdd1a
resolve conflict
chousallen Feb 5, 2025
3bad04f
extract definitions os timeval_tools
chousallen Feb 5, 2025
8819938
track timeval_tools.cpp
chousallen Feb 5, 2025
53b0b7b
naming
chousallen Feb 5, 2025
06484eb
move the scripts for setting up a new rpi to scripts dir
chousallen Feb 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions controller/software/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -45,11 +45,14 @@ include_directories(${INCLUDES} ${HARDWARE_INCLUDES})
add_library(playerLIB STATIC
${SOURCE}/player/LEDPlayer.cpp ${SOURCE}/player/OFPlayer.cpp
${SOURCE}/commands/player.cpp )
# ${SOURCE}/commands/player.cpp ${SOURCE}/player/StateMachine.cpp)
add_library(StateMachineLIB STATIC ${SOURCE}/player/StateMachine.cpp ${SOURCE}/player/FSM_Common.cpp)

add_library(StateMachineLIB STATIC ${SOURCE}/player/state_machine.cpp)

add_library(timevalLIB STATIC ${SOURCE}/timeval_tools.cpp)

# target_include_directories(playerLIB PUBLIC ${INCLUDES} ${HARDWARE_INCLUDES})
target_link_libraries(playerLIB StateMachineLIB ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES})
target_link_libraries(StateMachineLIB playerLIB ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES})
target_link_libraries(playerLIB StateMachineLIB timevalLIB ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES})
target_link_libraries(StateMachineLIB playerLIB timevalLIB ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES})

# list
add_executable(
@@ -73,7 +76,7 @@ target_link_libraries(load ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES} playerLIB)
# playerctl
add_executable(playerctl ${SOURCE}/commands/playerctl.cpp)
# target_include_directories(playerctl PUBLIC ${INCLUDES})
target_link_libraries(playerctl ${Boost_LIBRARIES})
target_link_libraries(playerctl timevalLIB ${Boost_LIBRARIES})

# ledtest
# add_executable(
4 changes: 2 additions & 2 deletions controller/software/inc/LEDPlayer.h
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
#include <boost/serialization/version.hpp>

#include "LEDController.h"
#include "StateMachine.h"
#include "state_machine.h"
#include "const.h"

using namespace std;
@@ -65,7 +65,7 @@ class LEDPlayer {
// threading function
void* loop_helper(void *context, StateMachine* fsm);
void loop(StateMachine *fsm);
void delayDisplay(const bool *delayingDisplay);
void delayDisplay(bool delayingDisplay);
void darkAll();
template <class Archive>
void serialize(Archive &archive, const unsigned int version);
4 changes: 2 additions & 2 deletions controller/software/inc/OFPlayer.h
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
#include <boost/serialization/version.hpp>

#include "OFController.h"
#include "StateMachine.h"
#include "state_machine.h"
#include "const.h"

using namespace std;
@@ -67,7 +67,7 @@ class OFPlayer {
// threading function
// void* loop_helper(void* context, StateMachine *fsm);
void loop(StateMachine *fsm);
void delayDisplay(const bool *delayingDisplay);
void delayDisplay(bool delayingDisplay);
void init();
void darkAll();
template <class Archive>
3 changes: 3 additions & 0 deletions controller/software/inc/const.h
Original file line number Diff line number Diff line change
@@ -6,4 +6,7 @@
#define LED_NUM 8
#define UPDATE_INTERVAL 30000 //microseconds(us)

#define FIFO_CMD_TO_PLAYER "/tmp/cmd_to_player"
#define FIFO_PLAYER_TO_CMD "/tmp/player_to_cmd"

#endif
95 changes: 95 additions & 0 deletions controller/software/inc/machine_tools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifndef _MACINE_TOOLS_H_
#define _MACINE_TOOLS_H_

#include <thread>

#include <LEDPlayer.h>
#include <OFPlayer.h>
#include <player.h>
#include <const.h>
#include <utils.h>
#include <timeval_tools.h>

extern const char *TAG;

std::thread led_loop, of_loop;
Player player;
LEDPlayer led_player;
OFPlayer of_player;
int dancer_fd;
const string path_to_dat = string(BASE_PATH) + "data/dancer.dat";

void delayDisplay(bool display)
{
led_player.delayDisplay(display);
of_player.delayDisplay(display);
return;
}

timeval getCalculatedTime(timeval subtrahend) {
timeval currentTime;
gettimeofday(&currentTime, NULL);
timeval time;
time.tv_sec = currentTime.tv_sec - subtrahend.tv_sec;
time.tv_usec = currentTime.tv_usec - subtrahend.tv_usec;
if (time.tv_usec < 0) {
time.tv_sec--;
time.tv_usec += 1000000;
}
return time;
}

bool restart() {
fprintf(stderr, "%srestart\n", TAG);
dancer_fd = tryGetLock(path_to_dat.c_str());
if (dancer_fd == -1) {
fprintf(stderr, "%sDancer is playing! Please stop it first!\n", TAG);
return 0;
} else if (dancer_fd == -2) {
fprintf(stderr, "%sdancer.dat cannot found\n", TAG);
return 0;
}

if (!restorePlayer(player, path_to_dat.c_str())) {
fprintf(stderr, "%scannot restore player\n", TAG);
return false;
}
led_player = player.myLEDPlayer;
led_player.init();
of_player = player.myOFPlayer;
of_player.init();
fprintf(stderr, "%sPlayer loaded\n", TAG);
return true;
}

void resume( StateMachine* fsm ){
led_loop = std::thread(&LEDPlayer::loop, &led_player, fsm);
of_loop = std::thread(&OFPlayer::loop, &of_player, fsm);
//led_loop.detach();
//of_loop.detach();
// pthread_create(&led_loop, NULL,&LEDPlayer::loop_helper, &led_player, fsm);
// pthread_create(&of_loop, NULL,&OFPlayer::loop_helper, &of_player , fsm);
cerr << "[Common] thread running\n";
return;
}

void Loop_Join()
{
if(led_loop.joinable()){
led_loop.join();
fprintf(stderr, "%sled_loop joined\n", TAG);
}
else{
fprintf(stderr, "%sled_loop cannot join\n", TAG);
}
if(of_loop.joinable()){
of_loop.join();
fprintf(stderr, "%sof_loop joined\n", TAG);
}
else{
fprintf(stderr, "%sof_loop cannot join\n", TAG);
}
return;
}

#endif // _MACINE_TOOLS_H_
Original file line number Diff line number Diff line change
@@ -3,25 +3,25 @@

#include <sys/time.h>
#include <iostream>
#include <thread>
#include <thread>
#include <const.h>
#include <StateMachine.h>
#include <oriStateMachine.h>
#include <LEDPlayer.h>
#include <OFPlayer.h>
#include <player.h>
#include <string>


enum CMD { C_PLAY, C_PAUSE, C_STOP, C_RESUME };
extern const std::string cmds[10];
//enum CMD { C_PLAY, C_PAUSE, C_STOP, C_RESUME };
//extern const std::string cmds[10];
extern std::thread led_loop, of_loop;
extern Player player;
extern LEDPlayer led_player;
extern OFPlayer of_player;
extern int dancer_fd;
extern string path ;
extern const char *rd_fifo;
//extern const char *rd_fifo;
extern const char *wr_fifo;

inline void write_fifo(bool success) {
int wr_fd;
std::string msg;
@@ -69,10 +69,11 @@ inline timeval getCalculatedTime(timeval subtrahend) {
}
return time;
}

inline bool restart() {
printf("restart\n");
dancer_fd = tryGetLock(path.c_str());
if (dancer_fd == -1) {
if (dancer_fd == -1) {
cerr << "[Common] Dancer is playing! Please stop it first!\n";
return 0;
} else if (dancer_fd == -2) {
@@ -91,7 +92,8 @@ inline bool restart() {
of_player.init();
cerr << "[Common] Player loaded\n";
return true;
}
}

inline void resume( StateMachine* fsm ){
led_loop = std::thread(&LEDPlayer::loop, &led_player, fsm);
of_loop = std::thread(&OFPlayer::loop, &of_player, fsm);
@@ -103,6 +105,7 @@ inline void resume( StateMachine* fsm ){
return;
}

<<<<<<< HEAD:controller/software/inc/FSM_Common.h
inline int parse_command(StateMachine* fsm,std::string str) {
if (str.length() == 1){
write_fifo(false);
@@ -129,15 +132,15 @@ inline int parse_command(StateMachine* fsm,std::string str) {
} else if (cmd.size() >= 3 && cmd[cmd.size() - 2] == "-d") {
fsm->data.delayTime = std::stoi(cmd[cmd.size() - 1]);//*1000;//saved as us
if (cmd.size() > 3) {
startusec = std::stoi(cmd[1])*1000;
startusec = std::stoi(cmd[1]);
}
if (cmd.size() > 4) {
fsm->data.stopTime = std::stoi(cmd[2]);
fsm->data.stopTimeAssigned = true;
}
} else {
if (cmd.size()>1) {
startusec = std::stoi(cmd[1])*1000;
startusec = std::stoi(cmd[1]);
}
if (cmd.size() > 2) {
fsm->data.stopTime = std::stoi(cmd[2]);
@@ -157,5 +160,7 @@ inline int parse_command(StateMachine* fsm,std::string str) {
write_fifo(false);
return -1;
}
=======
>>>>>>> test_fsm:controller/software/inc/oriFSM_Common.h

#endif
Original file line number Diff line number Diff line change
@@ -12,10 +12,14 @@
#include <string>
#include <thread>
#include <vector>
#include <unordered_map>
#include <unordered_map>
#include <utility>
#include <utils.h>
<<<<<<< HEAD:controller/software/inc/StateMachine.h
#include <iostream>
#include <fstream>
=======
>>>>>>> test_fsm:controller/software/inc/oriStateMachine.h
#define STATE_NUM 3
using namespace std;
enum Event { PLAY, PAUSE, STOP, RESUME,INVALID_CMD=-1}; // Event type
@@ -36,6 +40,7 @@ typedef struct {

class StateMachine{
private:
fstream fsm_log;
int currentState,nextState;
int TransitionTable[3][4]={
{EVENT_IGNORE, S_PAUSE,S_STOP, CANNOT_HAPPEN},
@@ -69,7 +74,7 @@ class StateMachine{
&StateMachine::EN_Pause,
&StateMachine::EN_Stop
};
public:
public:
enum { EVENT_IGNORE = 0xef, CANNOT_HAPPEN = 0xff };
playLoop_Data data;
StateMachine(); //init from initial state, maybe need to add more init parameters?
@@ -81,9 +86,11 @@ class StateMachine{
//void setState(int nextState);
void setData(timeval _baseTime, timeval _playedTime, long _stopTime, long _delayTime, bool _stopTimeAssigned, bool _isLiveEditting);
timeval getPlayedTime();
void Loop_Join();
void Loop_Join();
~StateMachine();

};


int parse_command(string cmd);
int parse_command(StateMachine * fsm, string cmd);
#endif
85 changes: 85 additions & 0 deletions controller/software/inc/state_machine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef _STATE_MACHINE_H_
#define _STATE_MACHINE_H_

#include <sys/time.h>
#include <sstream>

#define DEFAULT_DELAY_DISPLAY_RATIO 0.2

enum STATE
{
STATE_STOP,
STATE_PLAY,
STATE_PAUSE,
STATE_DELAY,
NUM_OF_STATES,
STATE_NULL
};

enum EVENT
{
EVENT_PLAY,
EVENT_STOP,
EVENT_PAUSE,
EVENT_RESUME,
NUM_OF_EVENTS,
EVENT_NULL
};

struct playLoop_Data
{
// specified
timeval start_time_stamp;
timeval stop_time_stamp;
timeval delay_duration;
float delay_display_ratio;
// private record
timeval start_time;
timeval enter_delay_time;
timeval enter_pause_time;
timeval total_pause_time;
timeval curr_time_stamp;
};

class StateMachine
{
private:
STATE m_state;
playLoop_Data data;
static const STATE m_transition_table[NUM_OF_STATES][NUM_OF_EVENTS];
void exitState(STATE state);
void execState(STATE state);
void enterState(STATE state);
void exitSTOP();
void exitPLAY();
void exitPAUSE();
void exitDELAY();
void execSTOP();
void execPLAY();
void execPAUSE();
void execDELAY();
void enterSTOP();
void enterPLAY();
void enterPAUSE();
void enterDELAY();

public:
StateMachine();
~StateMachine();
bool processEvent(EVENT event);
void execCurrState();
timeval getCurrTimeStamp() const;
void setStartTime(timeval _start_time_stamp);
void setStopTime(timeval _stop_time_stamp);
void setDelayTime(timeval _delay_time, float _delay_display_ratio = DEFAULT_DELAY_DISPLAY_RATIO);
STATE getCurrState() const;

friend class LEDPlayer;
friend class OFPlayer;
};

EVENT parse_event(const char *str);
std::string stateToStr(STATE state);
std::string eventToStr(EVENT event);

#endif // _STATE_MACHINE_H_
Loading