' ***********************************************************************
' *                       < Cloudrider - Hardware V1.0 >                *
' *                              <Motor Control>                        *
' *                  Hybrid computer section front-end                  *
' *             Source code for Basic Stamp microcontrollers            *
' *                      by Dr.Godfried-Willem Raes                     *
' *                       filename: CLOUD3.BS1                          *
' ***********************************************************************
' 07.08.1997: First version
'             T1000 used as downloader.
' 01.09.1997: Second version
'             now 1 pin, pin7 is used as serial input.
'             The BS1 becomes a slave processor for the master BS2
' 08.09.1997: Serin is completely useless...
'             now we use 'analog' data transport via pin 7, using pulsin
'             instead...
' 10.09.1997: BS2-code ready and debugged.
' 11.09.1997: BS1-1 code ready. To be tested!

' Hardware description:
' *********************
Start:
        dirs = %11111111  ' make all pins outputs
                          ' pin7 will be used bidirectional
                          ' analog pulse lenght data towards BS1 and
                          ' handshake to BS2
        pins = %10000000  ' set 7 low bits low and high bit high to indicate
                          ' BS1 not ready to receive pulse
        w1 = 0            ' reset w1 on init. = b2,b3
        w2 = 0            ' = b4,b5
        b6 = 11
        b7 = 0            ' byte used for external data reception and
                          ' command.
        w4 = 0           ' b8,b9 used for data reception via pulsin
                          ' b8 = LSB , b9 = MSB
        b10 = 1           ' robotflag: 1 if autonomous code is enabled
                          '            0 if running under external control
                          ' At startup de default is ON
                          ' The flag can be reset by external command
pause 500                ' 0.5 sec. pause before starting!
                         ' (give BS2 time to startup)

                         ' This is the Main program loop:
Loop:
        LOW 7                      :' discharge the line, in case...
        INPUT 7
        PULSIN 7, 1, w4
        HIGH 7                     :' disable reception of further pulses
                                   :' whilst processing the command...
        IF w4 = 0 THEN RobotKode   :' set a flag in b10
        IF w4 < 128 THEN SendCommand
        IF w4 < 256 THEN AllOFF    :' zero command, since 0 code cannot
                                   :' be used.
        b10 = 1                    :' if a larger code is received,
                                    ' the BS1 goes into robot mode.
GOTO Loop

                         ' Here are the two branches:
                         ' First we have the code processing the commands
                         ' as received:
SendCommand
     b10 = 0            ' disable robot mode...
     PINS b9 + 128      ' b9 contains the lower 7 bits of w4
                        ' We add 128 to disable reception of
                        ' another pulse, by forcing pin7 high
GOTO Loop
                        ' Second, we have the code for automatic run, if
                        ' link gets disconnected or if the robot command
                        ' was received:
RobotKode
     IF b10 = 0 THEN Loop
     w1 = w1 + 1        ' overflows automatic after $FFFF
                         'for w1 = 0 to 65535
                         'pause 1024: not needed. Pacing works by the
                         ' pulsin command
     w2 = w1 // 11
     if w2 = 0 THEN MOT0 
     w2 = w1 // 13
     if w2 = 0 THEN MOT1
     w2 = w1 // 17
     if w2 = 0 THEN MOT2 
     w2 = w1 // 19
     if w2 = 0 THEN MOT3
     w2 = w1 // 23
     if w2 = 0 THEN MOT4
     w2 = w1 // 29 
     if w2 = 0 THEN MOT5
     w2 = w1 // 31
     if w2 = 0 THEN MOT6 
     w2 = w1 // 41
     if w2 = 0 THEN LEFTMOT
     w2 = w1 // 43
     if w2 = 0 THEN RIGHTMOT
     w2 = w1 // 53
     if w2 = 0 THEN ARMMOT
     w2 = w1 // 57
     if w2 = 0 THEN WASMOT
     w2 = w1 // 61
     if w2 = 0 THEN ALLMOT
     w2 = w1 // 67
     if w2 = 0 THEN BLOWER
     w2 = w1 // 71
     if w2 = 0 THEN PUMPS
     w2 = w1 // 79
     if w2 = 0 THEN ALLOFF
MOT0:
     HIGH 0                           :' left motor, through 12A optorelais
     GOTO lus
MOT1:
     HIGH 1                           :' right motor, id.
     GOTO lus
MOT2:
     HIGH 2                           :' left motor on sweeping arm, 12Amp
     GOTO lus
MOT3:
     HIGH 3                           :' right motor on sweeping arm, 12Amp
     GOTO lus
MOT4:
     HIGH 4                           :' left waterpump, through 4Amp solid state opto relais
     GOTO lus
MOT5:
     HIGH 5                           :' right waterpump, id.
     GOTO lus
MOT6:
     HIGH 6                           :' air-turbine, through 4Amp solid state relais.
     GOTO lus
WASMOT:
     PINS = %10000011                 :' both stationary motors
     GOTO lus
ARMMOT:
     PINS = %10001100                 :' both motors on sweeping arms
     goto lus
ALLMOT:
     PINS = %10001111                 :' all cloud wheel motors
     GOTO lus
BLOWER:
     PINS = %11000000                 :' cluster turbine only
     GOTO lus
PUMPS:
     PINS = %10110000                 :' waterpumps only
     GOTO lus
LEFTMOT:
     PINS = %10010101                 :' all motors leftside of cloudrider
     GOTO lus
RIGHTMOT:
     PINS = %10101010                 :' all motors rightside of cloudrider
     GOTO lus
ALLOFF:
     PINS = %10000000                 :' all motors off
lus:
     'debug #w1
GOTO Loop


goto Start 
	bsave              ' use once to generate an object file
