You are on page 1of 4

Pseudo-Code for the Zener module

InitZener
Takes a priority number, returns false if error, true otherwise
Initialize ThisEvent variable
Set MyPriority variable with the passed in parameter.
Post the initial transition event

PostZener
Takes an event to post to the queue
Returns false if operation failed, true otherwise
Calls ES_PostToService with priority number and input event

RunZener
Takes the event to process
Returns ES_NO_EVENT if no error, ES_ERROR otherwise
Initalize event with type ES_NO_EVENT
Switch statement based on ThisEvent.EventType:
EventType is ES_INIT_ZENER:
Initialize deferral queue
Call initUART to initialize UART
Call initJoystick to initialize joystick
Call initTimer5 for position calculations
Break;
EventType is ES_NEWGLOBAL
Call InitGlobalPacket to initialize global packet with static value
Call FillGlobalPacket to fill it with module level variables
Call CompleteGlobalPacket to calculate checksum and add to packet
Use void pointer to write packet to tx pin
After Global Packet has been sent post ES_NEWLOCAL to send local packets
Break;
EventType is ES_NEWLOCAL
Call InitLocalPacket to initialize global packet with static value
Call FillLocalPacket to fill it with module level variables + TVS address
Call CompleteLocalPacket to calculate checksum and add to packet
Use void pointer to write packet to tx

Call InitLocalPacket to initialize global packet with static value


Call FillLocalPacket to fill it with module level variables + OPAMPa address
Call CompleteLocalPacket to calculate checksum and add to packet
Use void pointer to write packet to tx
Clear ZenerHitFlag
Break;
Default
Break;
Return the ReturnEvent
FillGlobalPacket
Takes a pointer to a global packet struct, return nothing
Set status to team 6, alive, weapon not fired as default
If hull strength is zero -> mark our team dead by setting alive flag low
If weapon was fired set weaponfiredflag in status byte high and clear the module
weaponfired flag
Grab the following module level variables and fill the bytes in packet:
Hull strength
ShipX
ShipY
BeamDx
BeamDy
Shield Strength
FillLocalPacket
Takes a pointer to a local packet struct, return nothing
Set the correct destination address for OPAMP or TVS
Set status byte
Grab the following module level variables and fill the bytes in the packet:
HullStr
BeamDx
BeamDy
LocalWeaponsPower
LocalNavPower
LocalWeaponsCap
LocalShieldCap

GetDist2Beam
Takes following parameter: beam vector x, beam vector, ourX, ourY, shipX, shipY
Returns: the shortest distance from our ship to the beam
wrap around calcs: a point (x,y)in the 2D map (x in [0,512000], y in [0,256000]
can actually be any of the 9 points in the donut,
which is the set {(mH + x, nW + y)} where H=512,000, W=256,000. m = -1,0,1, n = -1,0,1
Case 1: m,n = 0
Case 2: m = -1, n = -1
Case 3: m = -1, n = 0
Case 4: m = -1, n=1
Case 5: m = 0, n = -1
Case 6: m = 0, n = 1
Case 7: m = 1, n= -1
Case 8: m = 1, n = 0
Case 9: m = 1, n =1
For each case calculate the shortest distance and the shortest distance is selected

DecodePacket
If Packet is localPacket
Get source from bytes 4 and 5
Get status from byte 8
Decode status into assumecommand, transmissiontype, weaponfired, shiphit
Get BeamDx from byte 10 and 11
Get BeamDy from byte 12 and 13
Get weapons power from byte 14
If packet is from TVS
Scale beamDx and beamDy for screen
If weapon was fired
Set weaponfired flag high
endif
Endif (TVSpacket)
If packet is from OPAMP
Get nav power from byte 15
Get hull strength from byte 9
Get shield strength from 17
Endif (OPAMP packet)
Endif (localPacket)
If packet is global packet
Get status from byte 8
Decode status into team number, alive/dead, weaponfired
Get ship x from byte 10 and 11
Get ship y from byte 12 and 13
Get beam dx from byte 14 and 15
Get beam dy from byte 16 and 17
Calculate distance to beam
If weaponfiredflag is high and distance to beam is < 1250 km
Set ZenerHitFlag high
Turn on LED
Else
Turn off LED
Endif
If team number is not 6 (our team)
If (alivedead flag is set to alive)
Draw ship to screen
Else (dead)
Remove ship from screen
End
endif
Endif (globalpacket)

Timer5ResponseFunc
Calculates position and velocity
Takes no parameters, returns nothing
Clear timer 5 interrupt flag
Read value form ADC to get x and y position of joystick
Create a deadzone in the middle of the joystick position
If in deadzone
Set x and y position of joystick to 0
Else (not in deadzone)
Scale from 0-1023 -> -511-512
Endif
If joystick button is pressed (brake engaged)
Normalize the x and y velocity vectors
Set acceleration to all available navpower and point it in the opposite
direction of the velocity vector
Update lastVelocity variable
Else if in joystick deadzone
Calculate new position with last velocity vector
Else (acceleration is changing)
Normalize joystick position x and y
Take normalized vector, multiply by readval/maxval, multiplied by available
nav power in GW, then multiply by 100 because 0.001 GW/km/s^2
Calculate the new velocity vectors with the new acceleration
If above light speed -> squash to light speed
Calculate the new position and update LastVelocity vectors
If ship is out of bounds calculate the position rollover
Update LastPosition

You might also like