Here is the code for the first flights.
Code:
E02 con 12338 'Buffer overrun
E03 con 12339 'No free files
E04 con 12340 'Unrecognized command
E06 con 12342 'Command formatting error
E07 con 12343 'End of file
E08 con 12344 'Card not inserted
E09 con 12345 'MMC/SD reset
E0A con 12353 'Card write protected
EE6 con 17718 'Read only file
EE7 con 17719 'Not a file
EE8 con 17720 'Write falure
EEA con 17729 'No freespace
EEB con 17730 'File not open
EEC con 17731 'Improper mode
EED con 17732 'Invalid Open mode
EF1 con 17969 'Handle in use
EF2 con 17970 'File does not exist
EF4 con 17972 'File already exists
EF5 con 17973 'Path invalid
EF6 con 17974 'Invalid handle
EFB con 17986 'Bad FSINFO sector
EFC con 17987 'Unsupported FAT version
EFD con 17988 'Unsupported partition type
EFE con 17989 'Bad partition information
EFF con 17990 'Unknown error
combined var word 'Combine 2 byte error code to word
stuff var byte(2) '2 byte error code from SD
check var byte 'First charicter of error code
value1 var byte 'Error loop counter
index var byte 'Error lookdown table index
baro var word 'ADC baro reading
accel var word 'ADC accel reading
counter var word
counter1 var byte
Accel_at_rest var word 'Accelrometer at rest reading
Baro_at_rest var word 'Baro at rest reading
threashold var word 'Liftoff detietion threashold
delta var sword 'Differance of accel reading and at rest reading
accel_float var float 'Floating point value from delta
gees var float 'Calculated gee 1 g = 5.6counts
acceleration var float 'Floating point value for Gees * 32.2 for ft/sec 9.8 m/sec
velocity var float 'Floating point value (Accel + velocity) * timestep
int_velocity var Slong 'Interger value for velocity
int_acceleration var slong 'Interger value for acceleration
GPS_status var byte(15) 'Set up array to store GPS Status data
Long_Nav var byte(57) 'Set up array to store GPS Long Nav. message
index = 30 'Set SD lookdown value to an out of bounds value
value1 = 0 'Set variables to 0
counter1 = 0
Accel_at_rest = 0
Baro_at_rest = 0
gees = 0.0
acceleration = 0.0
velocity = 0.0
'-------------------------------------SD Start up ------------------------------------------
enablehserial 'Enable hardware uart
sethserial h9600 'Set baud to 9600bps
hserstat 0 'Clear uart buffers
serout S_out, i9600, ["Waiting for SD to initiliez", 13]
sdinit:
high 1 'Power on SD (Positive logic control)
pause 500 'Pause for initilization
value1 = value1 + 1
if value1 > 20 then gosub startuperror 'DIvide by 10 to get average If more than 10 tries reset SD
hserstat 4, sdinit 'If no data goto sdint
hserin [check] 'Read in byte from uart Sd sends > after valid command
If check <> ">" then sdinit 'If not > then try again
serout S_out, i9600, [ "Start SD sync ", 13] 'Recived > ready to sync
SD_sync:
serout S_out, i9600, [ "SD sync ", 13]
hserout [27] 'Send esc to SD(SD command to clear buffers)
value1 = 0
waitcommand:
value1 = value1 + 1
if value1 > 20 then goto SD_sync 'If more then 20 tries and no data resend command
hserstat 4, waitcommand 'If no data got to waitcommand
hserin [check] 'Read in byte from uartSd sends > after valid command
serout S_out, i9600, [check, " stsync", 13]
if check = ">" then 'If byte = > then no error
hserstat 0 'Clear uart buffers
goto Config_SD_Settings
elseif check = "E" 'If byte = E then error
hserin [str stuff2] 'Read in remaing 2 bytes of error code
serout S_out, i9600, ["Error", 13]
gosub errormessage
endif
goto SD_sync
Config_SD_Settings:
serout S_out, i9600, ["Config SD", 13]
hserout ["S 1 1", 13] 'Change SD timeout to 100ms
gosub Check_Return_Command 'Check return command for >
Check_Status:
serout S_out, i9600, ["Check Status", 13]
hserout ["Z", 13] 'Check status for missed error messages
waitcommand_Status:
hserstat 4, waitcommand_Status 'If no data present then loop
hserin [check] 'Read in byte Sd sends > after valid command
serout S_out, i9600, [check, "Status", 13]
if check = " " then 'If byte = > then no error
hserstat 0 'Clear uart buffers
goto Open_Files
elseif check = "E" 'If byte = E then error
hserin [str stuff2] 'Read in remaing 2 bytes of error code
serout S_out, i9600, ["Error", 13]
gosub errormessage
endif
goto SD_sync
'-------------------------------------SD Start up Subroutines------------------------------------------
startuperror:
serout S_out, i9600, ["Startup Error", 13]
low 1 'Power off SD
pause 500
high 1 'Power on SD
value1 = 0
pause 500
return
errormessage:
serout S_out, i9600, [str stuff2, 13]
combined.lowbyte = stuff(1) 'Combine 2 byte error code to word for lookdown comparison
combined.highbyte = stuff(0)
lookdown combined, =, [EEA, E08, E0A, E09, E03, EE6, EE7, EEB, EEC, EED, EF1, EF2, EF4, EF5, EF6, E02, E04, E06, E07, EE8, EFB, EFC, EFD, EFE, EFF ], index
serout S_out, i9600, [dec index, " index", 13]
if index <= 3 then 'Evaluate index and act accordingly
serout S_out, i9600, ["Main Error", 13]
goto SD_sync
elseif index <= 14
serout S_out, i9600, ["File Error", 13]
end
elseif index <= 17
serout S_out, i9600, ["Command Error", 13]
end
elseif index <= 19
serout S_out, i9600, ["Write Error", 13]
end
elseif index <= 24
serout S_out, i9600, ["Format Error", 13]
end
elseif index = 30
serout S_out, i9600, ["No match", 13]
goto SD_sync
endif
'----------------------------Sync Complet open files-----------------------------------
Open_Files:
serout S_out, i9600, [check, "open files", 13]
hserout ["O 2 W /Sensors.TXT", 13] 'Send command to SD to create Sensors in write mode
gosub Check_Return_Command 'Check for > from SD
serout S_out, i9600, ["Sensor File Made", 13]
'freqout 0,500,1000 'DurationNote
'freqout 0,1000,200 'DurationNote
goto GPS_Main
'---------------------Open files sub routines------------------
Check_Return_Command:
hserstat 4, Check_Return_Command 'Check uart for data if no data loop until data
hserin [check] 'Read in byte Sd sends > after valid command
if check = ">" then 'If byte = > then no error
hserstat 0 'Clear uart buffers
return
elseif check = "E" 'If byte = E then error
hserin [str stuff2] 'Read in remaing 2 bytes of error code
serout S_out, i9600, ["Error", 13]
gosub errormessage
endif
goto SD_sync
'---------------------------------------Initilize GPS----------------------------------------
GPS_Main:
serout p7, i9600, [">QST<"] 'Send command to gps to quiry Status
serin p6, i9600, [str GPS_status15] 'Read in and store responce
serout S_OUT, i9600, [str GPS_status15,13] 'Send Status out hardware serial port
Antenna_Fault: 'Check for antenna fault
if GPS_status(6) = "0" then
serout S_OUT, i9600, ["Antenna Good", 13]
elseif GPS_status(6) = "2"
serout S_OUT, i9600, ["Feedline Open Fault", 13]
goto GPS_Main
elseif GPS_status(6) = "6"
serout S_OUT, i9600, ["Feedline Short Fault", 13]
goto GPS_Main
endif
if GPS_status(7) = "1" then 'Check battery backup
serout S_OUT, i9600, ["Battery Backup Failed", 13]
else
serout S_OUT, i9600, ["No Problems", 13]
endif
if GPS_status(11) = "0" then 'Check RTC and Almanac
serout S_OUT, i9600, ["No Problems", 13]
elseif GPS_status(11) = "2"
serout S_OUT, i9600, ["RTC not available", 13]
elseif GPS_status(11) = "8"
serout S_OUT, i9600, ["Almanac not complete", 13]
elseif GPS_status(11) = "A"
serout S_OUT, i9600, ["No RTC or Almanac", 13]
endif
Tracking: 'Routine to monitor GPS until lock
if GPS_status(5) = "0" then
serout S_OUT, i9600, ["Doing Position Fixes", 13] 'If doing fixes jump to GPS_Lock
gosub GPS_LN 'Quiry for starting location
goto GPS_Lock
elseif GPS_status(5) = "1"
serout S_OUT, i9600, ["No GPS Time Yet", 13]
elseif GPS_status(5) = "3"
serout S_OUT, i9600, ["PDOP to High", 13]
elseif GPS_status(5) = "8"
serout S_OUT, i9600, ["No Usable Satalites", 13]
elseif GPS_status(5) = "9"
serout S_OUT, i9600, ["Only 1 Usable Satalite", 13]
elseif GPS_status(5) = "A"
serout S_OUT, i9600, ["Only 2 Usable Satalites", 13]
elseif GPS_status(5) = "B"
serout S_OUT, i9600, ["Only 3 Usable Satalites", 13]
elseif GPS_status(5) = "C"
serout S_OUT, i9600, ["Chosen Satalite is Unusable", 13]
endif
serout p7, i9600, [">QST<"] 'If no lock read in ST sentencen
serin p6, i9600, [str GPS_status15] 'Read in and store responce
pause 500
goto Tracking 'Repeat until lock
'------------------------------------------Hardware Ready ---------------------------------------
'-------------------------At Rest Readings--------------------------
GPS_Lock:
serout S_out, i9600, ["Get AT rest and wait for liftoff", 13]
At_Rest_Readings:
for counter = 1 to 10 'Sample sensors 10 times and average for atrest reading
adin ax0, baro 'Get baro ADC
adin ax1, accel 'Get accel ADC
Accel_at_rest = Accel_at_rest + accel 'Add current reading to previous readings
Baro_at_rest = Baro_at_rest + baro 'Add current reading to previous readings
serout S_out, i9600, [dec accel, " ", dec baro," ", dec Accel_at_rest, " ", dec Baro_at_rest, 13]
next
Accel_at_rest = Accel_at_rest/10 'Divide by 10 to get average
Baro_at_rest = Baro_at_rest/10 'Divide by 10 to get average
serout S_out, i9600, [dec Accel_at_rest, " ", dec Baro_at_rest, 13]
Calculate_Threashold: 'Calculate launch detect threashold
threashold = Accel_at_rest - 6 'Subtract 1g(5.6 counts) from atrest reading
serout S_out, i9600, ["Threashold = ", dec threashold, 13]
'--------------------------Launch Detect--------------------
Launch_detection:
repeat 'Reapeat until accel < threashold
adin ax1, accel 'Read in accel ADC
serout S_out, i9600, ["No Launch ", 13]
until accel < threashold 'Compare to if theashold < accel loop
serout S_out, i9600, ["Liftoff", 13]
for counter = 1 to 500
adin ax0, baro 'Get baro ADC
adin ax1, accel 'Get accel ADC
delta = Accel_at_rest - accel 'Find differance between atrest and adc accel reading
accel_float = tofloat delta 'Convert delta interger to floating point
gees = accel_float/5.6 'Gees = acel counts / 5.6 counts per gee
acceleration = gees * 32.2 'Accel = gees * 32.2 for ft/sec or 9.8 for m/sec
velocity = (velocity + acceleration) * 0.25 'velocity = (accel + velocity) * timestep
int_velocity = toint velocity 'Convert velocity float to interger
int_acceleration = toint acceleration 'Convert acceleration float to interger
hserout ["W 2", 13] 'Send Write file 2 to SD
hserout [dec baro, ",", dec accel, ",", sdec int_acceleration,",", sdec int_velocity, 13, 10] 'Send data to SD
gosub Check_Return_Command 'Check for > (SD return command)
if counter1 = 15 then 'Quiry GPS once every 15 samples
gosub GPS_LN
else
counter1 = counter1 + 1
endif
next
serout S_out, i9600, ["Close File", 13]
hserout ["C 2", 13] 'Send command to Close file 2 to SD
gosub Check_Return_Command 'Check for > (SD return command)
serout S_out, i9600, ["All Done", 13]
end
GPS_LN:
serout p7, i9600, [">QLN<"] 'Send command to gps to quiry Long Nav. message
serin p6, i9600, [str Long_Nav57] 'Read in and store responce
counter1 = 0
serout S_OUT, i9600, [str Long_Nav57,13] 'Send version out hardware serial port
Save_SD:
hserout ["W 2 58", 13] 'Set SD to "W"rite file 2 "57" bytes
hserout [str Long_Nav57, 13, 10] 'Send bytes to write
gosub Check_Return_Command 'Check for > (SD return command)
return