#include "safety.h"
+State safety_actual;
-void safety_notify_detection(State *s, SegmentIx seg) {
- if (s->seg[seg].detectable_now) return;
- if (!s->seg[seg].detectable_future)
- panic(NONE, seg, "unexpected detection");
+void safety_train_changed(TrainIx trai) {
+ State *s = &safety_actual;
+ TrainState *tra = s->tras[trai];
+ for (segi =
+s->trais[trai]
+ SegmentState *seg = s->seg[segi];
+
+void safety_notify_detection(SegmentIx segi) {
+ State *s = &safety_actual;
+ SegmentState *seg = s->seg[segi];
+ TrainIx trai = segs->owner;
+ TrainState *tra = s->tras[trai];
+
+ if (seg->detectable_now) return;
+ if (!seg->detectable_future)
+ safety_panic(NONE, segi, "unexpected detection");
+
+ tra->foredetect= segi;
+ tra->justarrived= 0;
+
+ if (seg->autostop) {
+ actual_setspeed(trai, 0);
+ tra->justarrived= 1;
+ }
+
+ ec= safety_train_changed(trai);
+ if (ec) {
+ logmsg(trai, segi, "emergency stop");
+ actual_emergencystop(trai);
+ tra->justarrived= 0;
+ ec= safety_train_changed(s, trai);
+ if (ec) panic(trai, segi, "emergency stop insufficient!");
+ }
+}
+
+
+
+
+
+
+ own
int main(void) {
typedef unsigned short TrainIx;
typedef unsigned short SegmentIx;
+typedef unsigned short Distance;
typedef unsigned char Speed; /* non-negative, units of 4mm/s */
typedef struct {
- SegmentIx foremost;
+ SegmentIx foredetect;
unsigned justarrived:1, reverse:1;
Speed speed;
} TrainState;
typedef struct {
unsigned present_now:1, present_future:1,
detectable_now:1, detectable_future:1,
- sideways:1;
+ sideways:1, autostop:1;
TrainIx owner;
/*polarity?*/
} SegmentState;
typedef struct {
unsigned next_rev:1;
SegmentIx next;
-
+ Distance dist;
} SegmentLink;
typedef struct {
- SegmentIx backwards, forwards, sideways;
+ SegmentLink backwards, forwards, sideways;
} SegmentInfo;
typedef struct {
Speed maxspeed;
-
+ int n_detections;
+ const Distance *detections; /* incremental distances from front */
} TrainInfo;
typedef struct {
- TrainState traNUM_TRAINS];
- SegmentState seg[NUM_SEGMENTS];
+ TrainState trasNUM_TRAINS];
+ SegmentState segs[NUM_SEGMENTS];
} State;
#endif /*SAFETY_H*/