UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


projects:mihab:ultracode
' ************************ IMPORTANT!!!!!   **********************************
' * Use these values in BASCOM at "Options, Compiler, Chip" at the beginning *
' *       HWSTACK-64                                                         *
' *       SWSTACK-128                                                        *
' *       FRAMESIZE-16                                                       *
' ****************************************************************************
'
'
$regfile = "m2561def.dat"                                   'ATMega 2561, change if using different processors
$crystal = 16000000                                         '16 MHz
$hwstack = 256
$swstack = 256
$framesize = 256
$baud1 = 9600
 
CONST Roottwomgovercda = 2.9
 
DECLARE SUB I2cread()
DECLARE SUB I2cwrite()
DECLARE SUB Onewiretemperature()
DECLARE SUB Sendsms()
DECLARE FUNCTION Checkposition(BYVAL Xpos AS SINGLE , BYVAL Ypos AS SINGLE)AS Byte
DECLARE SUB Video(BYVAL Timeinterval AS Byte)
DECLARE SUB Processgps()
DECLARE SUB Updatepredict()
DECLARE SUB Cutdown()
DECLARE SUB Landingvideo()
 
DIM Temp1 AS Byte , Temp2 AS Byte ,
DIM Bitstring(140) AS Byte , Numb AS Byte
DIM Jordancurvexpoints(18) AS SINGLE
DIM Jordancurveypoints(18) AS SINGLE
DIM Numberofpoints AS Byte
DIM Videookay AS Byte
 
 DIM G AS Byte , De AS Byte , Cutdown AS Byte
 DIM North AS SINGLE
 DIM East AS SINGLE
 DIM Altitude AS SINGLE
 DIM Storage AS SINGLE
 DIM Gpstring AS STRING * 15
 DIM S AS STRING * 90
 DIM B AS STRING * 6
 DIM Degrees AS STRING * 3
 DIM Minutes AS STRING * 7
 DIM Contents(17) AS STRING * 11
 DIM V AS STRING * 1
 DIM K AS SINGLE
 DIM Storebyte AS Byte
 DIM Up AS Byte
 DIM Across AS Byte
 DIM Bd(9) AS Byte
 DIM I AS INTEGER
 DIM Iterations AS Word
 DIM Writepermission AS Byte
 DIM LOCK AS Bit
 DIM Videocount AS Byte
 DIM Videos AS Byte
 DIM Btemp1 AS Byte
 DIM Seconds AS LONG
 DIM Timestring AS STRING * 10
 DIM Two AS STRING * 2
 DIM Oldeast AS SINGLE
 DIM Oldnorth AS SINGLE
 DIM Oldaltitude AS SINGLE
 DIM Oldseconds AS SINGLE
 DIM Predictedn AS SINGLE , Predictede AS SINGLE
 DIM Deltae AS SINGLE , Deltan AS SINGLE , Deltaa AS SINGLE , Deltat AS SINGLE
 DIM Averagealt AS SINGLE
 DIM Cuttingdown AS Eram Byte
 
 OPEN "COM2:" FOR BINARY AS #1
 OPEN "coma.0:9600,8,n,1,inverted" FOR OUTPUT AS #3
 OPEN "comd.0:4800,8,n,1" FOR INPUT AS #2
 OPEN "comd.1:4800,8,n,1" FOR OUTPUT AS #4
 
 Numberofpoints = 17
 Jordancurvexpoints(1) = -2.166
 Jordancurvexpoints(2) = 0
 Jordancurvexpoints(3) = 0
 Jordancurvexpoints(4) = 0.333
 Jordancurvexpoints(5) = 1.000
 Jordancurvexpoints(6) = 1.250
 Jordancurvexpoints(7) = 1.500
 Jordancurvexpoints(8) = 1.500
 Jordancurvexpoints(9) = 1.166
 Jordancurvexpoints(10) = 0.166
 Jordancurvexpoints(11) = -0.666
 Jordancurvexpoints(12) = -0.666
 Jordancurvexpoints(13) = -2.166
 Jordancurvexpoints(14) = -2.166
 Jordancurvexpoints(15) = -1.666
 Jordancurvexpoints(16) = -1.666
 Jordancurvexpoints(17) = -2.166
 Jordancurvexpoints(18) = -2.166
 Jordancurveypoints(1) = 53.166
 Jordancurveypoints(2) = 53.166
 Jordancurveypoints(3) = 52.833
 Jordancurveypoints(4) = 52.75
 Jordancurveypoints(5) = 52.833
 Jordancurveypoints(6) = 52.833
 Jordancurveypoints(7) = 52.666
 Jordancurveypoints(8) = 52.416
 Jordancurveypoints(9) = 52.083
 Jordancurveypoints(10) = 51.666
 Jordancurveypoints(11) = 51.666
 Jordancurveypoints(12) = 51.166
 Jordancurveypoints(13) = 51.166
 Jordancurveypoints(14) = 52.333
 Jordancurveypoints(15) = 52.333
 Jordancurveypoints(16) = 52.666
 Jordancurveypoints(17) = 52.666
 Jordancurveypoints(18) = 53.166
 
 Iterations = 0
 Cutdown = 0
 Videookay = 0
 
'----------------------------------------------------------------------
 
Config 1wire = Portc.1                                      'connects to our temperature sensor
Config Scl = Portc.3                                        ' we need to provide the SCL pin name
Config Sda = Portc.5
Config Portc.4 = OUTPUT
Config Portc.6 = OUTPUT
Config I2cdelay = 10                                        ' we need to provide the SDA pin name
Config Portc.0 = OUTPUT
Config Portc.2 = OUTPUT
Config Porta.1 = OUTPUT
Config Clock = Soft
Config Portd.0 = INPUT
Config Watchdog = 2048
 
I2cinit                                                     ' we need to set the pins in the proper state
 
Cutdownpin ALIAS Porta.1
Cam_shutter ALIAS Portc.2
Cam_power ALIAS Portc.0
Cam_right ALIAS Portc.4
Cam_ok ALIAS Portc.6
Cam_right = 1
Cam_ok = 1                                                  'Port b.1
 
Config Porta = OUTPUT                                       'need to work out WTF connects here
Porta = 0
Config Portc.7 = OUTPUT
Portc.7 = 0                                                 'this is dead man button
Portd.4 = 1
Config Portd.4 = INPUT
 
'------------------------------------------------------------------start of main program
 STOP Watchdog
 Cutdownpin = 0
 PRINT #1 , "Hello from ULTRAHAB"
 WAIT 1
 
$include "Config_MMC.BAS"                                   'code off the avr-dos site
$include "CONFIG_AVR-DOS.BAS"
 
IF Gbdriveerror <> 0 THEN
 PRINT #1 , "card error " ; Gbdriveerror
END IF
 
Btemp1 = Initfilesystem(1)
IF Btemp1 <> 0 THEN
 PRINT #1 , "File system error" ; Btemp1
ELSE
 PRINT #1 , "Card setup okay"
END IF
 PRINT #1 , "sms time"
 S = "hello test"
 CALL Sendsms()
 PRINT #1 , "temperture sensor test"
 CALL Onewiretemperature()
 PRINT #1 , "GPS test"
 DO
  INPUT #2 , S
  B = Mid(s , 2 , 6)
  IF B = "$GPGGA" THEN
   PRINT #1 , S                                             'shows us the GGA string
   Temp1 = Split(s , Contents(1) , ",")
   K = 0
   K = VAL(contents(10))
   IF K <> 0 THEN                                           'if nonzero altitude then we've got something
    GOTO Gotlock
   END IF
  END IF
 LOOP
Gotlock:
PRINT #1 , "Okay we have GPS"
CALL Processgps()
CALL Updatepredict()
Temp1 = Checkposition(predictede , Predictedn)
PRINT #1 , "check says:" ; Temp1
PRINT #1 , "press the button you fool!!!!"
FOR Temp1 = 1 TO 10                                         ' dead man button
 WAIT 1
 IF Pind.4 = 0 THEN
  GOTO Userpresent
 END IF
NEXT Temp1
Iterations = 241
PRINT #4 , "ULTRAHAB bad news: forced reeboot, cutting down"
PRINT #1 , "bad news: forced reeboot, cutting down"
WAIT 4
IF Cuttingdown = 15 THEN
 PRINT #4 , "ULTRAHAB insufficient power to cutdown"
 PRINT #1 , "not enought power!!!"
 Cutdown = 1
END IF
GOTO Theflightloop:
Userpresent:
IF Cuttingdown <> 0 THEN
 Cuttingdown = 0
END IF
PRINT #1 , "Filesystem: " ; Gbfilesystem
PRINT #1 , "FAT Start Sector: " ; Glfatfirstsector
PRINT #1 , "Root Start Sector: " ; Glrootfirstsector
PRINT #1 , "Data First Sector: " ; Gldatafirstsector
PRINT #1 , "Max. Cluster Nummber: " ; Glmaxclusternumber
PRINT #1 , "Sectors per Cluster: " ; Gbsectorspercluster
PRINT #1 , "Root Entries: " ; Gwrootentries
PRINT #1 , "Sectors per FAT: " ; Glsectorsperfat
PRINT #1 , "Number of FATs: " ; Gbnumberoffats
S = Dir( "*.*")
  WHILE LEN(s) > 0                                          ' if there was a file found?
   PRINT #1 , S ; "  ";                                     'file name
   PRINT #1 , Filedate() ; "  ";
   PRINT #1 , Filetime() ; "  ";
   PRINT #1 , Filelen()
   S = Dir()                                                ' print file , the date the fime was created/changed , the time and the size of the file
  WEND
 
OPEN "VERB.TXT" FOR INPUT AS #14
DO
 DO
   LINE INPUT #14 , S                                       ' read a line
   PRINT #1 , S                                             ' print on terminal
 LOOP UNTIL EOF(#14) <> 0
 PRINT #1 , "deleting, okay? (Y/N)"
 INPUT #1 , B
LOOP UNTIL B = "Y"
CLOSE #14
KILL "VERB.TXT"
 
 
OPEN "LOG.TXT" FOR INPUT AS #14
DO
 DO
   LINE INPUT #14 , S                                       ' read a line
   PRINT #1 , S                                             ' print on terminal
 LOOP UNTIL EOF(#14) <> 0
PRINT #1 , "deleting, okay? (Y/N)"
INPUT #1 , B
LOOP UNTIL B = "Y"
CLOSE #14
KILL "LOG.TXT"
PRINT #1 , "camera on"
 Cam_power = 1
 Waitms 500
 Cam_power = 0
 
 
 
Theflightloop:
DO
 OPEN "VERB.TXT" FOR APPEND AS #11                          'these are all our files
 OPEN "LOG.TXT" FOR APPEND AS #12                           'main flight loop
 Incr Iterations
 PRINT #1 , "Main loop iteration number:" ; Iterations
 Cam_shutter = 1
 Waitms 200                                                 'photo
 Cam_shutter = 0
 CALL Onewiretemperature()
 PRINT #12 , Seconds ; "," ; Storage ; ",";
 PRINT #11 , "temperature is:" ; Storage
 WAIT 30
 DO
  INPUT #2 , S
  B = Mid(s , 2 , 6)                                        'check stuff and update
 LOOP UNTIL B = "$GPGGA"
 S = Mid(s , 2 , 90)
 PRINT #11 , S
 PRINT #12 , S
 CALL Processgps()
 CALL Updatepredict()
 IF Deltaa < 0 AND Videookay = 1 AND Altitude < 300 THEN
  CALL Landingvideo()
 END IF
 PRINT #11 , "Predicted at " ; Seconds ; "was" ; Predictede ; " " ; Predictedn
 CALL Sendsms()
 PRINT #4 , "ULTRAHAB" ; Contents(3) ; Contents(4) ; Contents(5) ; Contents(6);
 IF Cutdown = 0 THEN
  PRINT #4 , "PRED:" ; Predictede ; Predictedn
 ELSE
  PRINT #4 , "cut"
 END IF
 Temp1 = Checkposition(predictede , Predictedn)
 IF Cutdown = 0 THEN
  IF Temp1 = 1 THEN
   PRINT #11 , "Geofence cutdown"
   CALL Cutdown()
   PRINT #4 , "geofence cutdown"
  END IF
  IF Iterations > 180 THEN                                  ' 3 hour cutdown approx
   PRINT #1 , "Time limit cutdown"
   PRINT #11 , "Time limit cutdown"
   CALL Cutdown()
   PRINT #4 , "time limit cutdown"
  END IF
 END IF
 Cam_shutter = 1                                            'photo
 Waitms 200
 Cam_shutter = 0
 CALL Onewiretemperature()
 PRINT #12 , Seconds ; "," ; Storage ; ",";
 PRINT #11 , "temperature is:" ; Storage
 WAIT 10                                                    'video
 CALL Video(8)
 WAIT 10
 DO
  INPUT #2 , S
  B = Mid(s , 2 , 6)
 LOOP UNTIL B = "$GPGGA"
 S = Mid(s , 2 , 90)
 PRINT #11 , S
 PRINT #12 , S
 CALL Processgps()
 CALL Updatepredict()
 IF Deltaa < 0 AND Videookay = 1 AND Altitude < 300 THEN
  CALL Landingvideo()
 END IF
 PRINT #11 , "Predicted at " ; Seconds ; "was" ; Predictede ; " " ; Predictedn
 PRINT #4 , "ULTRAHAB" ; Contents(3) ; Contents(4) ; Contents(5) ; Contents(6);
 IF Cutdown = 0 THEN
  PRINT #4 , "PRED:" ; Predictede ; Predictedn
 ELSE
  PRINT #4 , "cut"
 END IF
 Temp1 = Checkposition(predictede , Predictedn)
 IF Cutdown = 0 THEN
  IF Temp1 = 1 THEN
   PRINT #11 , "Geofence cutdown"
   CALL Cutdown()
   PRINT #4 , "geofence cutdown"
  END IF
 END IF
 CLOSE #11
 CLOSE #12
 
LOOP
 
 
 
'---------------------------------------------------------- prediction
 
SUB Updatepredict()
 IF Iterations = 0 THEN
  Predictedn = North
  Predictede = East
  Oldeast = East                                            'start
  Oldnorth = North
  Oldaltitude = Altitude
  Oldseconds = Seconds
 END IF
 Deltae = East - Oldeast
 Deltan = North = Oldnorth
 Deltaa = Altitude - Oldaltitude
 Deltat = Seconds - Oldseconds
 Averagealt = Oldaltitude + Altitude
 Averagealt = Averagealt / 2
 Oldeast = East
 Oldnorth = North
 Oldaltitude = Altitude
 Oldseconds = Seconds
 IF Deltaa > 0 THEN
  K = Averagealt / 16000
  K = EXP(k)
  K = 0.90909 * K
  K = Roottwomgovercda * K                                  'we now have the velocity of decent
  PRINT #1 , "Descent velocity=" ; K
  K = Deltaa / K                                            'time of decent for this layer
  PRINT #1 , "descent time=" ; K
  K = K / Deltat                                            'weighting for this layer
  PRINT #1 , "Layer weighting=" ; K
  K = K + 1                                                 'need to account for ascent drift
  Deltae = K * Deltae
  Deltan = K * Deltan
  Predictedn = Predictedn + Deltan
  Predictede = Predictede + Deltae
 END IF
 PRINT #1 , "predicted north=" ; Predictedn
 PRINT #1 , "predicted east=" ; Predictede
END SUB
 
'---------------------------------------------------------- GPS parsing
 
SUB Processgps()
   Temp1 = Split(s , Contents(1) , ",")
   Altitude = VAL(contents(10))
   Degrees = Left(contents(3) , 2)
   Minutes = Right(contents(3) , 7)
   North = 0
   North = VAL(minutes)
   North = North / 60
   K = 0
   K = VAL(degrees)
   North = K + North
   Degrees = Left(contents(5) , 3)
   Minutes = Right(contents(5) , 7)
   East = 0
   East = VAL(minutes)
   East = East / 60
   K = 0
   K = VAL(degrees)
   East = K + East
   IF Contents(6) = "W" THEN
    East = -east
   END IF
   Timestring = Contents(2)
   Two = Mid(timestring , 1 , 2)
   I = VAL(two)
   Seconds = I * 3600
   Two = Mid(timestring , 3 , 2)
   I = VAL(two)
   I = 60 * I
   Seconds = Seconds + I
   Two = Mid(timestring , 5 , 2)
   I = VAL(two)
   Seconds = Seconds + I
   PRINT #1 , "Lat=" ; North
   PRINT #1 , "Long=" ; East
   PRINT #1 , "Altitude=" ; Altitude
   PRINT #1 , "Time=" ; Seconds
   IF Altitude > 500 THEN
    Videookay = 1
   END IF
END SUB
 
'---------------------------------------------------------- temperature
 
SUB Onewiretemperature()
 1wreset
 1wwrite &HCC
 1wwrite &H44
 WAIT 1
 1wreset
 1wwrite &HCC
 1wwrite &HBE
 Bd(1) = 1wread(9)
 1wreset
 Storage = Bd(1)
 IF Bd(2) = 255 THEN                                        'bd(2) is 255 for -ive temp
  Storage = -storage
 END IF
 Storage = Storage / 2
 Gpstring = Str(storage)
 PRINT #1 , "T=" ; Gpstring
END SUB
 
 
'----------------------------------------------------------- phone
 
 
SUB Sendsms()
LOCAL E AS Byte , D AS Byte , M AS Byte                     ' this is a for one sms
E = 1
D = 0
  FOR G = 1 TO 80
   V = Mid(s , G , 1)
   Numb = ASC(v)
   FOR M = 0 TO 6
    Bitstring(e).d = Numb.m                                 'd,ti,m ,g and e Are Bytes,
    Incr D
    IF D = 8 THEN
     D = 0
    Incr E
    END IF
   NEXT M
  NEXT G
PRINT #1 , "SMS"
PRINT #3 , "AT+CMGS=154"
WAIT 1
PRINT #3 , "079144775810065011000C914497313345270000AA";    'PDU string to Mobile
PRINT #3 , "A0";
FOR G = 1 TO 140
 PRINT #3 , Hex(bitstring(g));
NEXT G
PRINT #3 , Chr(026);
END SUB
 
'----------------------------------------------------------- poscheck
 
FUNCTION Checkposition(BYVAL Xpos AS SINGLE , BYVAL Ypos AS SINGLE) AS Byte
 LOCAL Count AS Byte , Skip AS Byte , Gplus AS Byte
 LOCAL Grad AS SINGLE , Gradtwo AS SINGLE , Delta AS SINGLE , Deltatwo AS SINGLE , Xone AS SINGLE , Xtwo AS SINGLE , Yone AS SINGLE , Ytwo AS SINGLE
 Count = 0
 FOR G = 1 TO Numberofpoints
   Gplus = G + 1
   Xone = Jordancurvexpoints(g)
   Xtwo = Jordancurvexpoints(gplus)
   Yone = Jordancurveypoints(g)
   Ytwo = Jordancurveypoints(gplus)
  'first we check to see if both points north or both south
   Skip = 0
   IF Yone > Ypos THEN
    IF Ytwo > Ypos THEN
     Skip = 1
    END IF
   END IF
   IF Yone < Ypos THEN
    IF Ytwo < Ypos THEN
     Skip = 1
    END IF
   END IF
   IF Skip = 0 THEN
    'then we work out if the x axis intercept is at positive or negative x
    Delta = Xone - Xpos
    'now the gradient
    Grad = Xtwo - Xone
    Gradtwo = Ytwo - Yone
    Grad = Grad / Gradtwo
    'we've got the gradient
    Deltatwo = Ypos - Yone
    Deltatwo = Deltatwo * Grad
    Delta = Delta + Deltatwo
    'is the crossing to the left or right?
    IF Delta > 0 THEN
     IF Count = 0 THEN
      Count = 1
     ELSE
      Count = 0
     END IF
    END IF
   END IF
  NEXT G
IF Count = 0 THEN
 PRINT #1 , "outside"
 Checkposition = 1
ELSE
 PRINT #1 , "inside"
 Checkposition = 0
END IF
 
END FUNCTION
 
'----------------------------------------------------------- eeprom
 
 
SUB I2cread()
I2cstart
I2cwbyte &B1010_0000
I2cwbyte Up
I2cwbyte Across
I2cstart
I2cwbyte &B1010_0001
I2crbyte De , Nack
I2cstop
END SUB
 
SUB I2cwrite()
IF Writepermission = 1 THEN
 I2cstart                                                   'start condition
 I2cwbyte &B1010_0000                                       'slave address
 I2cwbyte Up                                                'high address
 I2cwbyte Across                                            'low address
 I2cwbyte De                                                'value to write
 I2cstop                                                    'stop condition
 Waitms 10
END IF
END SUB
 
'--------------------------------------------------------------------------  camera
 
SUB Video(BYVAL Timeinterval AS Byte)
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 300
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 200
Toggle Cam_ok
Waitms 200
Toggle Cam_ok
Waitms 500
Toggle Cam_shutter
Waitms 200
Toggle Cam_shutter
WAIT Timeinterval
Toggle Cam_shutter
Waitms 200
Toggle Cam_shutter
WAIT 1
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 300
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 200
Toggle Cam_ok
Waitms 200
Toggle Cam_ok
END SUB
 
SUB Landingvideo()
PRINT #1 , "Landing video"
Videookay = 0
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 300
Toggle Cam_right
Waitms 200
Toggle Cam_right
Waitms 200
Toggle Cam_ok
Waitms 200
Toggle Cam_ok
Waitms 500
Toggle Cam_shutter
Waitms 200
Toggle Cam_shutter
END SUB
 
'------------------------------------------------------------------------------cutdown
 
SUB Cutdown()
 PRINT #1 , "Cutting down !!!!"
 Cuttingdown = 15
 Start Watchdog
 Cutdownpin = 1
 FOR Temp1 = 1 TO 12
  WAIT 1
  RESET Watchdog
 NEXT Temp1
 Cutdownpin = 0
 Cutdown = 1                                                'so we know we've cutdown
 STOP Watchdog
 Cuttingdown = 0
END SUB
 
 
'---------------------------------------------------------------------------
 
CLOSE #1
CLOSE #2
CLOSE #3
 
END
projects/mihab/ultracode.txt ยท Last modified: 2008/07/19 23:33 (external edit)