Code for autonomous rover for Sparkfun AVC. DataBus won 3rd in 2012 and the same code was used on Troubled Child, a 1986 Jeep Grand Wagoneer to win 1st in 2014.
Dependencies: mbed Watchdog SDFileSystem DigoleSerialDisp
Schedule/Schedule.h@25:bb5356402687, 2018-11-30 (annotated)
- Committer:
- shimniok
- Date:
- Fri Nov 30 16:11:53 2018 +0000
- Revision:
- 25:bb5356402687
- Parent:
- 0:a6a169de725f
Initial publish of revised version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:a6a169de725f | 1 | typedef unsigned int tick; |
shimniok | 0:a6a169de725f | 2 | typedef int value; |
shimniok | 0:a6a169de725f | 3 | typedef char flag; |
shimniok | 0:a6a169de725f | 4 | |
shimniok | 0:a6a169de725f | 5 | /** Simple library for scheduling events by polling time, that is, avoiding interrupts. |
shimniok | 0:a6a169de725f | 6 | * Can schedule up to 64 events. The schedule is defined by the number of ticks making |
shimniok | 0:a6a169de725f | 7 | * up the initial period of the schedule, and either a start and end value or values |
shimniok | 0:a6a169de725f | 8 | * at specific tick values. |
shimniok | 0:a6a169de725f | 9 | */ |
shimniok | 0:a6a169de725f | 10 | class Schedule { |
shimniok | 0:a6a169de725f | 11 | public: |
shimniok | 0:a6a169de725f | 12 | /** Sets the behavior of getNext() after the initial schedule period has exceeded. |
shimniok | 0:a6a169de725f | 13 | * |
shimniok | 0:a6a169de725f | 14 | * @param repeat means the schedule values are repeated |
shimniok | 0:a6a169de725f | 15 | * @param hold means the last schedule value is provided after the schdule is done |
shimniok | 0:a6a169de725f | 16 | */ |
shimniok | 0:a6a169de725f | 17 | enum { repeat=0x02, hold=0x04 }; |
shimniok | 0:a6a169de725f | 18 | |
shimniok | 0:a6a169de725f | 19 | /** Creates an empty schedule |
shimniok | 0:a6a169de725f | 20 | */ |
shimniok | 0:a6a169de725f | 21 | Schedule(); |
shimniok | 0:a6a169de725f | 22 | |
shimniok | 0:a6a169de725f | 23 | /** Creates a schedule based on a linear function. See set() |
shimniok | 0:a6a169de725f | 24 | * |
shimniok | 0:a6a169de725f | 25 | * @param scale |
shimniok | 0:a6a169de725f | 26 | * @param max the maximum tick value of the clock, sets the period |
shimniok | 0:a6a169de725f | 27 | * @param start is the value returned at tick == 0 |
shimniok | 0:a6a169de725f | 28 | * @param stop is the value returned at tick == ticks-1 |
shimniok | 0:a6a169de725f | 29 | * @param m selects the mode / behavior of the schedule when getNext() called after period exceeded |
shimniok | 0:a6a169de725f | 30 | */ |
shimniok | 0:a6a169de725f | 31 | Schedule(unsigned int scale, tick max, value start, value stop, flag m); |
shimniok | 0:a6a169de725f | 32 | |
shimniok | 0:a6a169de725f | 33 | /** Sets a ratio of time to ticks. See clockTicked() |
shimniok | 0:a6a169de725f | 34 | * |
shimniok | 0:a6a169de725f | 35 | * @param timePerTick specifies the number of time units per tick |
shimniok | 0:a6a169de725f | 36 | */ |
shimniok | 0:a6a169de725f | 37 | void scale(unsigned int scale); |
shimniok | 0:a6a169de725f | 38 | |
shimniok | 0:a6a169de725f | 39 | /** Sets the total number of ticks to run the loop |
shimniok | 0:a6a169de725f | 40 | */ |
shimniok | 0:a6a169de725f | 41 | void max(tick max); |
shimniok | 0:a6a169de725f | 42 | |
shimniok | 0:a6a169de725f | 43 | /** Sets behavior of getNext() when called after tickCount exceeded |
shimniok | 0:a6a169de725f | 44 | */ |
shimniok | 0:a6a169de725f | 45 | void mode(flag m); |
shimniok | 0:a6a169de725f | 46 | |
shimniok | 0:a6a169de725f | 47 | /** sets the value at the specified tick |
shimniok | 0:a6a169de725f | 48 | * |
shimniok | 0:a6a169de725f | 49 | * @param t specifies the scheduled tick |
shimniok | 0:a6a169de725f | 50 | * @param v specifies the value to return when tick==whichTick |
shimniok | 0:a6a169de725f | 51 | */ |
shimniok | 0:a6a169de725f | 52 | void set(tick t, value v); |
shimniok | 0:a6a169de725f | 53 | |
shimniok | 0:a6a169de725f | 54 | /** Set schedule based on a linear function |
shimniok | 0:a6a169de725f | 55 | * |
shimniok | 0:a6a169de725f | 56 | * @param ticks total number of ticks over which the schedule is valid |
shimniok | 0:a6a169de725f | 57 | * @param startValue is the value returned at tick == 0 |
shimniok | 0:a6a169de725f | 58 | * @param stopValue is the value returned at tick == ticks-1 |
shimniok | 0:a6a169de725f | 59 | */ |
shimniok | 0:a6a169de725f | 60 | void set(unsigned int scale, tick max, value start, value stop, flag m); |
shimniok | 0:a6a169de725f | 61 | |
shimniok | 0:a6a169de725f | 62 | /** get the next value for schedule's current time. Use with ticked() |
shimniok | 0:a6a169de725f | 63 | * |
shimniok | 0:a6a169de725f | 64 | * @returns the value at the current schedule's time |
shimniok | 0:a6a169de725f | 65 | */ |
shimniok | 0:a6a169de725f | 66 | value get(); |
shimniok | 0:a6a169de725f | 67 | |
shimniok | 0:a6a169de725f | 68 | /** increment the clock and get the next value in the schedule |
shimniok | 0:a6a169de725f | 69 | * |
shimniok | 0:a6a169de725f | 70 | * @returns the value at the schedule's next clock tick |
shimniok | 0:a6a169de725f | 71 | */ |
shimniok | 0:a6a169de725f | 72 | value next(); |
shimniok | 0:a6a169de725f | 73 | |
shimniok | 0:a6a169de725f | 74 | /** Pass in some unit of time and determine if the 'clock' has ticked. |
shimniok | 0:a6a169de725f | 75 | * Suppose timePerTick == 20 and you pass in the elapsed time in milliseconds |
shimniok | 0:a6a169de725f | 76 | * then this function returns true every 20ms. |
shimniok | 0:a6a169de725f | 77 | * |
shimniok | 0:a6a169de725f | 78 | * @param time the integer corresponding to elapsed time |
shimniok | 0:a6a169de725f | 79 | * @returns true if the elapsed time % timePerTick == 0 |
shimniok | 0:a6a169de725f | 80 | */ |
shimniok | 0:a6a169de725f | 81 | bool ticked(unsigned int time); |
shimniok | 0:a6a169de725f | 82 | |
shimniok | 0:a6a169de725f | 83 | /** Are we done with the schedule? |
shimniok | 0:a6a169de725f | 84 | * |
shimniok | 0:a6a169de725f | 85 | * @returns true if schedule is done; see max() |
shimniok | 0:a6a169de725f | 86 | */ |
shimniok | 0:a6a169de725f | 87 | bool done(); |
shimniok | 0:a6a169de725f | 88 | |
shimniok | 0:a6a169de725f | 89 | private: |
shimniok | 0:a6a169de725f | 90 | unsigned int _scale; |
shimniok | 0:a6a169de725f | 91 | tick _max; |
shimniok | 0:a6a169de725f | 92 | tick _clock; |
shimniok | 0:a6a169de725f | 93 | flag _mode; |
shimniok | 0:a6a169de725f | 94 | int _schedule[64]; |
shimniok | 0:a6a169de725f | 95 | bool _validTick(tick t); |
shimniok | 0:a6a169de725f | 96 | }; |
shimniok | 0:a6a169de725f | 97 |