1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* <rc/start_stop.h>
*
* @brief cleanly start and stop a process, signal handling, program flow
*
* It can be tricky to manage the starting and stopping of mutiple threads and
* loops. Since the robot control library has several background threads in
* addition to any user-created threads, we encourage the use of the
* consolidated high-level process control method described here. These
* functions allow easy implementation of good-practice process handling
* including a global shutdown flag, a signal handler, making & deleting a PID
* file, stopping previously running copies of a program, and stopping a program
* when it's in the background.
*
* The rc_state_t struct tries to cover the majority of use cases in the context
* of a robotics application. The user should start their program main()
* function by setting the state to UNINITIALIZED and enabling the signal
* handler before doing hardware initialization and starting threads. When the
* user's own initialization sequence is complete they should set the flow state
* to PAUSED or RUNNING to indicate to the newly created threads that the
* program should now behave in normal ongoing operational mode.
*
* During normal operation, the user may elect to implement a PAUSED state where
* the user's threads may keep running to read sensors but do not actuate
* motors, leaving their robot in a safe state. For example, pressing the pause
* button could be assigned to change this state back and forth between RUNNING
* and PAUSED. This is entirely optional.
*
* The most important state here is EXITING. The signal handler described here
* intercepts the SIGINT signal when the user presses Ctrl-C and sets the state
* to EXITING. It is then up to the user's threads to watch for this condition
* and exit cleanly. The user may also set the state to EXITING at any time to
* trigger the closing of their own threads and robot control library's own
* background threads.
*
* The flow state variable is kept in the robot control library's memory space
* and should be read and modified by the rc_get_state() and rc_set_state()
* functions above. The user may optionally use the rc_print_state() function to
* print a human readable version of the state enum to the screen.
*
*
* @addtogroup start_stop
* @{
*/
extern "C" __cplusplus
}
// RC_START_STOP_H
/** @} end group start_stop*/