1 /****************************************************************************
  2  Module
  3    d:\me218bTNG\Lectures\Lecture31\SMTemplate.c
  4 
  5  Revision
  6    1.0.1
  7 
  8  Description
  9    This is a template file for implementing state machines.
 10 
 11  Notes
 12 
 13  History
 14  When           Who     What/Why
 15  -------------- ---     --------
 16  02/13/10 14:29 jec      revised Start and run to add new kind of entry function
 17                          to make implementing history entry cleaner
 18  02/13/10 12:29 jec      added NewEvent local variable to During function and
 19                          comments about using either it or Event as the return
 20  02/11/10 15:54 jec      more revised comments, removing last comment in during
 21                          function that belongs in the run function
 22  02/09/10 17:21 jec      updated comments about internal transitions on During funtion
 23  02/18/09 10:14 jec      removed redundant call to RunLowerlevelSM in EV_Entry
 24                          processing in During function
 25  02/20/07 21:37 jec      converted to use enumerated type for events & states
 26  02/13/05 19:38 jec      added support for self-transitions, reworked
 27                          to eliminate repeated transition code
 28  02/11/05 16:54 jec      converted to implment hierarchy explicitly
 29  02/25/03 10:32 jec      converted to take a passed event parameter
 30  02/18/99 10:19 jec      built template from MasterMachine.c
 31  02/14/99 10:34 jec      Began Coding
 32 ****************************************************************************/
 33 /*----------------------------- Include Files -----------------------------*/
 34 /* include header files for this state machine as well as any machines at the
 35    next lower level in the hierarchy that are sub-machines to this machine
 36 */
 37 #include "Search_And_DestroySM.h"
 38 
 39 #include <hidef.h>      /* common defines and macros */
 40 #include <stdlib.h>
 41 #include "SMEVENTS.h"
 42 #include "Movement.h"
 43 #include "Gun.h"
 44 #include "FiringSM.h"
 45 #include "Constants.h"
 46 #include "DeadReckoning.h"
 47 #include <stdio.h>
 48 
 49 
 50 /*---------------------------- Module Functions ---------------------------*/
 51 /* prototypes for private functions for this machine, things like during
 52    functions, entry & exit functions.They should be functions relevant to the
 53    behavior of this state machine
 54 */
 55 static Event_t DuringRotateToShoot( Event_t Event);
 56 static Event_t DuringFire1( Event_t Event);
 57 //static Event_t DuringFire2( Event_t Event);
 58 //static Event_t DuringSearchAndDestroyIdle( Event_t Event);
 59 
 60 /*---------------------------- Module Variables ---------------------------*/
 61 // everybody needs a state variable, you may need others as well
 62 static SearchAndDestroyState CurrentState;
 63 static char AngleFlag=1;
 64 //static char FireFlag;
 65 
 66 /*------------------------------ Module Code ------------------------------*/
 67 /****************************************************************************
 68  Function
 69     RunSearchAndDestroySM
 70 
 71  Parameters
 72    Event_t: the event to process
 73 
 74  Returns
 75    Event_t: an event to return
 76 
 77  Description
 78    add your description here
 79  Notes
 80    uses nested switch/case to implement the machine.
 81  Author
 82    J. Edward Carryer, 2/11/05, 10:45AM
 83 ****************************************************************************/
 84 Event_t RunSearchAndDestroySM( Event_t CurrentEvent )
 85 {
 86    unsigned char MakeTransition = FALSE;/* are we making a state transition? */
 87    SearchAndDestroyState NextState = CurrentState;
 88    //  Event_t NewEvent;  //  ????  What does this do.  Does this do history??
 89    Event_t EntryEventKind = EV_ENTRY;
 90 
 91    switch ( CurrentState )
 92    {
 93        case RotateToShoot :       // If current state is RotateToShoot
 94          // Execute During function for state one. EV_ENTRY & EV_EXIT are
 95          // processed here
 96          //NewEvent = DuringStateOne(CurrentEvent);  prob dont need this
 97          DuringRotateToShoot(CurrentEvent);
 98          //process any events
 99          if ( CurrentEvent != EV_NO_EVENT )        //If an event is active
100          {
101             switch (CurrentEvent)
102             {
103                case EV_ROTATE_DONE : //If event is Rotate_Done
104 
105                   DuringRotateToShoot(CurrentEvent);
106                   // Execute action function for state one : event one
107                   NextState = Fire1;//Decide what the next state will be
108                   // for internal transitions, skip changing MakeTransition
109                   MakeTransition = TRUE; //mark that we are taking a transition
110                   // if transitioning to a state with history change kind of entry
111                   // // EntryEventKind - EV_ENTRY_HISTORY;
112                   // optionally, consume or re-map this event for the upper
113                   // level state machine
114                   // // CurrentEvent = NewEvent;
115                   break;
116                 // repeat cases as required for relevant events
117             }
118          }
119          break;
120        case Fire1 :       // If current state is Fire1
121          // Execute During function for state one. EV_ENTRY & EV_EXIT are
122          // processed here
123          //NewEvent = DuringStateOne(CurrentEvent);  prob dont need this
124          DuringFire1(CurrentEvent);
125          //process any events
126          if ( CurrentEvent != EV_NO_EVENT )        //If an event is active
127          {
128             //Local Dead-End, exit via higher level state change
129          }
130          break;
131 
132       // repeat state pattern as required for other states
133     }
134     //   If we are making a state transition
135     if (MakeTransition == TRUE)
136     {
137        //   Execute exit function for current state
138        RunSearchAndDestroySM(EV_EXIT);
139        CurrentState = NextState; //Modify state variable
140        //   Execute entry function for new state
141        (void)RunSearchAndDestroySM(EntryEventKind);
142      }
143      return(CurrentEvent);
144 }
145 /****************************************************************************
146  Function
147      StartTemplateSM
148 
149  Parameters
150      None
151 
152  Returns
153      None
154 
155  Description
156      Does any required initialization for this state machine
157  Notes
158 
159  Author
160      J. Edward Carryer, 2/18/99, 10:38AM
161 ****************************************************************************/
162 void StartSearchAndDestroySM (void)
163 {
164   //No history
165 
166         CurrentState = RotateToShoot;
167    // call the entry function (if any) for the ENTRY_STATE
168    RunSearchAndDestroySM(EV_ENTRY);
169 }
170 
171 /****************************************************************************
172  Function
173      QueryTemplateSM
174 
175  Parameters
176      None
177 
178  Returns
179      TemplateState_t The current state of the Template state machine
180 
181  Description
182      returns the current state of the Template state machine
183  Notes
184 
185  Author
186      J. Edward Carryer, 2/11/05, 10:38AM
187 ****************************************************************************/
188 SearchAndDestroyState QuerySearchAndDestroySM ( void )
189 {
190    return(CurrentState);
191 }
192 
193 
194 /****************************************************************************
195  Function
196      QueryAngleFlag
197 
198  Parameters
199      None
200 
201  Returns
202      true false flag from testing for expired state on timer
203 
204  Description
205      returns the current state of the oven timer expired flag
206  Notes
207 
208  Author
209      J. Edward Carryer, 2/21/05, 4:02PM
210 ****************************************************************************/
211 char QueryAngleFlag ( void )
212 {
213   static char OldFlag;
214   char NewFlag, change=0;
215   NewFlag = AngleFlag;
216 
217   if(NewFlag == 0 && OldFlag == 1)
218     change=1;
219 
220   OldFlag = NewFlag;
221 
222   return change;
223 }
224 
225 
226 /***************************************************************************
227  private functions
228  ***************************************************************************/
229 
230 static Event_t DuringRotateToShoot( Event_t Event)
231 {
232     Event_t NewEvent;
233     static signed int TargetAngle;
234 
235     // process EV_ENTRY, EV_ENTRY_HISTORY & EV_EXIT events
236     // // if ( (Event == EV_ENTRY) || (Event == EV_ENTRY_HISTORY) )
237     if ( Event == EV_ENTRY)
238     {
239       (void)printf("RotateToShoot\n\r");
240         // implement any entry actions required for this state machine
241         // after that start any lower level machines that run in this state
242         // only call Start if the lower level machine does not have a history
243         // // StartLowerLevelSM( Event );  // We dont have any
244         // repeat the StartxxxSM() functions for concurrent state machines
245         // on the lower level
246         //GunSpeed(50);
247 
248         //  RotateToTargetFAKE();  // We really dont have an entry function
249 
250         //Get TargetAngle
251         TargetAngle = CalculateTargetAngle();
252         (void)printf("Target Angle: %f   CurrentAngle:  %f", (TargetAngle*(360.0/TicksPerRotation) ), (GetAngle()*(360.0/TicksPerRotation)) );
253 
254         //Set the speed once you reach firing location;
255         //allowing time for the gun speed to adjust
256         GunSpeed(GetGunSpeed());
257         //TargetAngle = TicksPerRotation/5;
258     }
259     else if ( Event == EV_EXIT )
260     {
261         // on exit, give the lower levels a chance to clean up first
262         //Stop Wheels
263         WheelSpeed(0,0);
264 
265     }else
266     // do the 'during' function for this state
267     {
268         AngleFlag = SettingAngle(TargetAngle);
269 
270 
271         // repeat for any concurrent lower level machines
272         // do any activity that is repeated as long as we are in this state
273     }
274     // return either Event, if you don't want to allow the lower level machine
275     // to remap the current event, or NewEvent if you do want to allow it.
276     return(Event);
277 }
278 
279 
280 static Event_t DuringFire1( Event_t Event)
281 {
282 
283     // process EV_ENTRY, EV_ENTRY_HISTORY & EV_EXIT events
284     // // if ( (Event == EV_ENTRY) || (Event == EV_ENTRY_HISTORY) )
285     if ( Event == EV_ENTRY)
286     {
287       (void)printf("Fire");
288         // implement any entry actions required for this state machine
289         // after that start any lower level machines that run in this state
290         // only call Start if the lower level machine does not have a history
291         StartFiringSM( );
292         // repeat the StartxxxSM() functions for concurrent state machines
293         // on the lower level
294     }
295     else if ( Event == EV_EXIT )
296     {
297         // on exit, give the lower levels a chance to clean up first
298         // repeat for any concurrently running state machines
299         // now do any local exit functionality
300     }else
301     // do the 'during' function for this state
302     {
303         // run any lower level state machine
304         //FireFlag = Release_Ball2();
305         // repeat for any concurrent lower level machines
306         // do any activity that is repeated as long as we are in this state
307 
308      //GunSpeed(50);
309 
310     }
311     // return either Event, if you don't want to allow the lower level machine
312     // to remap the current event, or NewEvent if you do want to allow it.
313     return(Event);
314 }