chiark / gitweb /
board and object for segments
[trains.git] / hostside / safety.h
1 /**/
2
3 #ifndef SAFETY_H
4 #define SAFETY_H
5
6 /*========== basic types etc. ==========*/
7
8 typedef unsigned short TrainNum;
9 typedef unsigned short SegmentNum;
10 typedef unsigned short LocationNum;
11 typedef unsigned short MovPosComb;
12 typedef unsigned char Small;
13 typedef short TimeInterval;
14 typedef short Distance;
15 typedef char Speed; /* non-negative, units of 4mm/s */
16
17 /*========== state of the layout ==========*/
18
19 typedef struct {
20   SegmentNum foredetect;   /* train's detectable part is at most maxinto   */
21   Distance maxinto, uncertainty;   /*   into foredetect but train may be   */
22   unsigned                         /*   uncertainty less far advanced      */
23     backwards:1; /* train is moving backwards wrt its own front and back */
24   Speed speed;
25 } TrainState;
26
27 typedef struct {
28   unsigned
29     owned:1, /* this track segment is reserved for a train */
30     tr_backwards:1, /* train's motion is (would be) backwards wrt track */
31     movfeat_moving:1, /* feature(s) have been told to change to movposcomb */
32     cm_autostop:1, /* train should stop on detection */
33     seg_inverted:1, /* polarity is inverted */
34     tr_updated:1; /* for use by safety.c:lay_train etc.; otherwise 0 */
35   TimeInterval until_here, /* ) nonnegative; */  /* ) always valid but */
36     until_detect;          /* ) 0 if already */  /* )  only meaningful */
37   TrainNum owner;                                /* )  iff owned       */
38   MovPosComb movposcomb;
39   /*polarity?*/
40 } SegmentState;
41
42 typedef struct {
43   unsigned next_backwards:1;
44   SegmentNum next;
45 } SegmentLinkInfo;
46
47 typedef struct {
48   const char *pname;
49   Small posns;
50   MovPosComb weight;
51 } MovFeatInfo;
52
53 typedef struct {
54   SegmentLinkInfo backwards, forwards;
55   Distance dist;
56 } SegPosCombInfo;
57
58 typedef struct {
59   const char *pname;
60   unsigned invertible:1;
61   Small n_movfeats;
62   const MovFeatInfo *movfeats;
63   MovPosComb n_poscombs;
64   const SegPosCombInfo *poscombs;
65   Small board, object;
66 } SegmentInfo;
67
68 typedef struct {
69   Speed maxspeed;
70   Distance tail, detectable, head;
71   const char *pname;
72 } TrainInfo;
73
74 extern const TrainInfo info_trains[NUM_TRAINS];
75 extern const SegmentInfo info_segments[NUM_SEGMENTS];
76
77 typedef struct {
78   TrainState trains[NUM_TRAINS];
79   SegmentState segments[NUM_SEGMENTS];
80 } State;
81
82 extern State s;
83
84 /*========== safety.c ==========*/
85 /*
86  * safety.c is responsible for ensuring that things don't go
87  * physically wrong (eg, collisions, derailments, short circuits,
88  * etc.).
89  */
90
91 void safety_emergencystop(TranNum);
92   /* Callable directly in response to application command. */
93
94 void safety_requestspeed(TrainNum tran, long newspeed);
95   /* To be called only by the speed manager, thus indirectly from
96    * user request.
97    * Will result in a call to speedmanager_speedchange_notify (and of
98    * course to actual_setspeed.  Speed manager must apply accel/decel
99    * curve so that if safety.c agrees the command, the actual speed of
100    * the train changes straight away (at least for decel).
101    */
102
103 void safety_notify_detection(SegmentNum segn);
104   /* To be called by actual.c when new train detection occurs. */
105
106 /*========== speedmgr.c ==========*/
107
108 void speedmanager_speedchange_notify(TrainNum tran);
109   /* To be called only by safety.c, whenever speed is actually set.
110    * New speed has already been recorded in State. */
111
112 /*========== actual.c ==========*/
113 /* actual.c should only be called from safety.c.
114  * It is responsible for communicating with the PICs, including
115  * repeating the NMRA commands and redacting detection information.
116  *
117  * In general, when State information is shared between actual.c
118  * and safety.c, actual.c is responsible for modifying State, and
119  * will then call an actual_... function to notify the change.
120  */
121
122 void actual_setspeed(TrainNum tran);
123
124 void actual_inversions_start(void);
125 void actual_inversions_segment(SegmentNum);
126 void actual_inversions_done(void);
127   /* safety.c will call these in this order: first start, then segment
128    * for 0 or more segments (whose inversion may or may not have
129    * changed), and finally done.  At done, the PICs should be told to
130    * (de)invert the segments as specified.
131    */
132   
133 /*
134  *
135  * Entrypoints are:                  Called from, and as a result of:
136  *   actual_setspeed                    safety.c
137  *   actual_emergencystop               safety.c
138
139
140 /*========== utils.c ==========*/
141
142 typedef struct TrackLocation TrackLocation;
143 struct TrackLocation { /* transparent, and manipulable by trackloc_... fns */
144   SegmentNum segn; /* current segment */
145   long into; /* distance from start of segment */
146   unsigned backwards:1; /* if 1, into is positive and measured from end */
147 };
148
149 long trackloc_remaininseg(const TrackLocation *tloc);
150   /* Returns dist that tloc can advance before it goes into next segment. */
151
152 void trackloc_further(TrackLocation *tloc, long *remain_io);
153   /* Advances tloc, decrementing *remain_io, until either
154    * *remain_io becomes zero, or tloc->segn changes. */
155
156 void trackloc_reverse(TrackLocation *tloc);
157   /* Reverses tloc without changing its actual location. */
158
159 const SegmentLinkInfo *trackloc_segmentlink_near(const TrackLocation *tloc);
160 const SegmentLinkInfo *trackloc_segmentlink_far(const TrackLocation *tloc);
161
162 #endif /*SAFETY_H*/