]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/arducontrol.h
React to overcurrent events
[r2c2.git] / source / libr2c2 / arducontrol.h
1 #ifndef LIBR2C2_ARDUCONTROL_H_
2 #define LIBR2C2_ARDUCONTROL_H_
3
4 #include <msp/core/mutex.h>
5 #include <msp/core/thread.h>
6 #include <msp/datafile/objectloader.h>
7 #include <msp/fs/path.h>
8 #include <msp/io/serial.h>
9 #include <msp/time/timedelta.h>
10 #include <msp/time/timestamp.h>
11 #include "driver.h"
12
13 namespace R2C2 {
14
15 class ArduControl: public Driver
16 {
17 public:
18         class Loader: public Msp::DataFile::ObjectLoader<ArduControl>
19         {
20         public:
21                 Loader(ArduControl &);
22
23         private:
24                 void mfx_announce_serial(unsigned);
25                 void mfx_locomotive(unsigned);
26         };
27
28 private:
29         enum Command
30         {
31                 POWER_ON = 0x01,
32                 POWER_OFF = 0x02,
33                 READ_POWER_STATE = 0x03,
34                 READ_TRACK_CURRENT = 0x08,
35                 SET_OVERCURRENT_LIMIT = 0x09,
36                 READ_INPUT_VOLTAGE = 0x0A,
37                 MOTOROLA_SPEED = 0x11,
38                 MOTOROLA_REVERSE = 0x12,
39                 MOTOROLA_SPEED_DIRECTION = 0x13,
40                 MOTOROLA_SPEED_FUNCTION = 0x14,
41                 MOTOROLA_SOLENOID = 0x15,
42                 MFX_SET_STATION_ID = 0x21,
43                 MFX_ANNOUNCE = 0x22,
44                 MFX_SEARCH = 0x23,
45                 MFX_ASSIGN_ADDRESS = 0x24,
46                 MFX_PING = 0x25,
47                 MFX_SPEED = 0x28,
48                 MFX_SPEED_FUNCS8 = 0x29,
49                 MFX_SPEED_FUNCS16 = 0x2A,
50                 S88_READ = 0x30,
51                 SET_BAUD_RATE = 0x70,
52                 COMMAND_OK = 0x80,
53                 RECEIVE_OVERRUN = 0x81,
54                 FRAMING_ERROR = 0x82,
55                 INVALID_COMMAND = 0x83,
56                 LENGTH_ERROR = 0x84,
57                 INVALID_VALUE = 0x85,
58                 OVERCURRENT = 0xA0,
59                 BAUD_CHANGE_FAILED = 0xA1,
60                 TRACK_CURRENT = 0xC0,
61                 INPUT_VOLTAGE = 0xC1,
62                 POWER_STATE = 0xC2,
63                 S88_DATA = 0xD0,
64                 MFX_SEARCH_FEEDBACK = 0xD1,
65                 MFX_PING_FEEDBACK = 0xD2
66         };
67
68         struct Tag
69         {
70                 enum Type
71                 {
72                         NONE,
73                         GENERAL,
74                         LOCOMOTIVE,
75                         ACCESSORY,
76                         SENSOR
77                 };
78
79                 Type type;
80                 unsigned char command;
81                 unsigned short serial;
82                 unsigned id;
83
84                 Tag();
85
86                 operator bool() const { return type!=NONE; }
87         };
88
89         enum GeneralCommand
90         {
91                 POWER,
92                 NEW_LOCO
93         };
94
95         enum Protocol
96         {
97                 MM,
98                 MFX
99         };
100
101         struct ProtocolInfo
102         {
103                 unsigned max_address;
104                 unsigned max_speed;
105                 unsigned max_func;
106         };
107
108         template<typename T>
109         struct ControlledVariable
110         {
111                 T current;
112                 T pending;
113                 unsigned short serial;
114
115                 ControlledVariable(): current(), pending(), serial(0) { }
116                 ControlledVariable(T v): current(v), pending(v), serial(0) { }
117
118                 bool set(T v) { if(v==pending) return false; pending = v; ++serial; return true; }
119                 bool commit(unsigned short s) { if(s!=serial) return false; current = pending; return true; }
120                 void rollback() { pending = current; ++serial; }
121
122                 operator T() const { return current; }
123         };
124
125         struct Locomotive
126         {
127                 enum Command
128                 {
129                         SPEED,
130                         REVERSE,
131                         FUNCTIONS
132                 };
133
134                 unsigned id;
135                 Protocol proto;
136                 unsigned address;
137                 ControlledVariable<unsigned> speed;
138                 ControlledVariable<bool> reverse;
139                 ControlledVariable<unsigned> funcs;
140                 unsigned last_change_age;
141
142                 Locomotive(Protocol, unsigned);
143
144                 unsigned create_speed_dir_command(char *) const;
145                 unsigned create_speed_func_command(unsigned, char *) const;
146         };
147
148         struct MfxInfo: public DetectedLocomotive
149         {
150                 class Loader: public Msp::DataFile::ObjectLoader<MfxInfo>
151                 {
152                 public:
153                         Loader(MfxInfo &);
154                 };
155
156                 unsigned id;
157         };
158
159         struct Accessory
160         {
161                 enum Kind
162                 {
163                         TURNOUT,
164                         SIGNAL
165                 };
166
167                 enum Command
168                 {
169                         ACTIVATE,
170                         DEACTIVATE
171                 };
172
173                 Kind kind;
174                 unsigned address;
175                 unsigned bits;
176                 ControlledVariable<unsigned> state;
177                 unsigned uncertain;
178                 unsigned target;
179                 Msp::Time::TimeDelta active_time;
180
181                 Accessory(Kind, unsigned, unsigned);
182
183                 unsigned create_state_command(unsigned, bool, char *) const;
184         };
185
186         struct Sensor
187         {
188                 enum Command
189                 {
190                         STATE
191                 };
192
193                 unsigned address;
194                 ControlledVariable<bool> state;
195
196                 Sensor(unsigned);
197         };
198
199         struct PendingCommand
200         {
201                 Tag tag;
202                 char command[15];
203                 unsigned char length;
204                 unsigned repeat_count;
205
206                 PendingCommand();
207                 PendingCommand(GeneralCommand);
208                 PendingCommand(Locomotive &, Locomotive::Command, unsigned = 0);
209                 PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
210         };
211
212         template<typename T>
213         class Queue
214         {
215         private:
216                 std::list<T> items;
217                 Msp::Mutex mutex;
218
219         public:
220                 void push(const T &);
221                 bool pop(T &);
222                 bool empty() const;
223         };
224
225         class Task
226         {
227         protected:
228                 Task() { }
229         public:
230                 virtual ~Task() { }
231
232                 virtual bool get_work(PendingCommand &) = 0;
233                 virtual void process_reply(const char *, unsigned) { }
234         };
235
236         class CommandQueueTask: public Task
237         {
238         private:
239                 Queue<PendingCommand> queue;
240
241         public:
242                 virtual bool get_work(PendingCommand &);
243         };
244
245         class RefreshTask: public Task
246         {
247         private:
248                 typedef std::list<Locomotive *> LocomotivePtrList;
249
250                 LocomotivePtrList cycle;
251                 LocomotivePtrList::iterator next;
252                 unsigned round;
253                 Locomotive *loco;
254                 unsigned phase;
255                 Msp::Mutex mutex;
256
257         public:
258                 RefreshTask();
259
260                 virtual bool get_work(PendingCommand &);
261
262                 void add_loco(Locomotive &);
263                 void remove_loco(Locomotive &);
264         private:
265                 Locomotive *get_next_loco();
266                 void advance();
267         };
268
269         class S88Task: public Task
270         {
271         private:
272                 ArduControl &control;
273                 unsigned n_octets;
274                 unsigned octets_remaining;
275                 unsigned delay;
276
277         public:
278                 S88Task(ArduControl &);
279
280                 virtual bool get_work(PendingCommand &);
281                 virtual void process_reply(const char *, unsigned);
282
283                 void set_n_octets(unsigned);
284                 void grow_n_octets(unsigned);
285         };
286
287         class MfxAnnounceTask: public Task
288         {
289         private:
290                 unsigned serial;
291                 Msp::Time::TimeStamp next;
292
293         public:
294                 MfxAnnounceTask();
295
296                 virtual bool get_work(PendingCommand &);
297
298                 void set_serial(unsigned);
299                 unsigned get_serial() const { return serial; }
300         };
301
302         class MfxSearchTask: public Task
303         {
304         private:
305                 ArduControl &control;
306                 unsigned next_address;
307                 Msp::Time::TimeStamp next;
308                 unsigned size;
309                 unsigned bits;
310                 unsigned misses;
311                 Queue<MfxInfo> queue;
312
313         public:
314                 MfxSearchTask(ArduControl &);
315
316                 virtual bool get_work(PendingCommand &);
317                 virtual void process_reply(const char *, unsigned);
318
319                 void set_next_address(unsigned);
320                 bool pop_info(MfxInfo &);
321         };
322
323         class MonitorTask: public Task
324         {
325         private:
326                 float voltage;
327                 float current;
328                 float base_level;
329                 float peak_level;
330                 Msp::Time::TimeStamp next_poll;
331                 unsigned next_type;
332
333         public:
334                 MonitorTask();
335
336                 virtual bool get_work(PendingCommand &);
337                 virtual void process_reply(const char *, unsigned);
338
339                 float get_voltage() const { return voltage; }
340                 float get_current() const { return current; }
341                 void reset_peak();
342                 float get_peak() const { return peak_level-base_level; }
343         };
344
345         class ControlThread: public Msp::Thread
346         {
347         private:
348                 ArduControl &control;
349                 bool done;
350                 std::vector<Task *> tasks;
351
352         public:
353                 ControlThread(ArduControl &);
354
355                 void exit();
356         private:
357                 virtual void main();
358                 void init_baud_rate();
359                 bool get_work(PendingCommand &);
360                 unsigned do_command(const PendingCommand &, const Msp::Time::TimeDelta &);
361                 unsigned process_reply(const char *, unsigned);
362                 void set_power(bool);
363         };
364
365         typedef std::map<unsigned, Locomotive> LocomotiveMap;
366         typedef std::vector<MfxInfo> MfxInfoArray;
367         typedef std::map<unsigned, Accessory> AccessoryMap;
368         typedef std::list<Accessory *> AccessoryPtrList;
369         typedef std::map<unsigned, Sensor> SensorMap;
370
371         Msp::IO::Serial serial;
372         unsigned debug;
373         Msp::FS::Path state_file;
374
375         ControlledVariable<bool> power;
376         bool halted;
377
378         LocomotiveMap locomotives;
379         MfxInfoArray mfx_info;
380         AccessoryMap accessories;
381         AccessoryPtrList accessory_queue;
382         Accessory *active_accessory;
383         unsigned char active_index;
384         Msp::Time::TimeStamp off_timeout;
385
386         SensorMap sensors;
387
388         Msp::Time::TimeDelta command_timeout;
389         Queue<PendingCommand> command_queue;
390         Queue<Tag> completed_commands;
391         RefreshTask refresh;
392         S88Task s88;
393         MfxAnnounceTask mfx_announce;
394         MfxSearchTask mfx_search;
395         MonitorTask monitor;
396         ControlThread thread;
397
398         static ProtocolInfo protocol_info[2];
399
400 public:
401         ArduControl(const Options &);
402         ~ArduControl();
403
404         virtual void set_power(bool);
405         virtual bool get_power() const { return power; }
406         virtual void halt(bool);
407         virtual bool is_halted() const { return halted; }
408
409         virtual const char *enumerate_protocols(unsigned) const;
410 private:
411         static Protocol map_protocol(const std::string &);
412 public:
413         virtual unsigned get_protocol_speed_steps(const std::string &) const;
414
415         virtual const DetectedLocomotive *enumerate_detected_locos(unsigned) const;
416         virtual unsigned add_loco(unsigned, const std::string &, const VehicleType &);
417 private:
418         MfxInfoArray::iterator add_mfx_info(const MfxInfo &);
419 public:
420         virtual void remove_loco(unsigned);
421         virtual void set_loco_speed(unsigned, unsigned);
422         virtual void set_loco_reverse(unsigned, bool);
423         virtual void set_loco_function(unsigned, unsigned, bool);
424
425         virtual unsigned add_turnout(unsigned, const TrackType &);
426         virtual void remove_turnout(unsigned);
427         virtual void set_turnout(unsigned, unsigned);
428         virtual unsigned get_turnout(unsigned) const;
429
430         virtual unsigned add_signal(unsigned, const SignalType &);
431         virtual void remove_signal(unsigned);
432         virtual void set_signal(unsigned, unsigned);
433         virtual unsigned get_signal(unsigned) const;
434
435 private:
436         unsigned add_accessory(Accessory::Kind, unsigned, unsigned);
437         void remove_accessory(Accessory::Kind, unsigned);
438         void set_accessory(Accessory::Kind, unsigned, unsigned);
439         unsigned get_accessory(Accessory::Kind, unsigned) const;
440
441 public:
442         virtual unsigned add_sensor(unsigned);
443         virtual void remove_sensor(unsigned);
444         virtual void set_sensor(unsigned, bool) { }
445         virtual bool get_sensor(unsigned) const;
446
447         virtual void tick();
448         virtual void flush();
449 private:
450         void save_state() const;
451 };
452
453 } // namespace R2C2
454
455 #endif