November 21, 2024, 09:09:00 PM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


Servo move then trigger AP-8

Started by Jadams, October 24, 2010, 11:46:59 AM

Previous topic - Next topic

Jadams

I'm trying to trigger segment 0 on the AP-8 after the servo snaps to a random location.  I tried putting GOSUB Start_AP-8 at different places in the Servo_Pause: subroutine, but can't make it work.  The sound is a 2 sec laser shot.

Can someone show me the correct place for the GOSUB Start_AP-8 line?  Do I need to use Let_AP-8_Finish on such a short segment?

Thanks

' =========================================================================
'
'   File...... Laser_Tag.BS1
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated... 10/24/2010
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------

'Servo snaps to a random location, AP-8 fires laser


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  Servo           = 0                     ' tail servo


' -----[ Constants ]-------------------------------------------------------

SYMBOL  Baud            = OT2400
SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0


' -----[ Variables ]-------------------------------------------------------

SYMBOL  status          = B0
SYMBOL   playing        =  BIT7
SYMBOL  pos             = B2
SYMBOL  timer           = B3
SYMBOL  sfx             = B4
SYMBOL  lottery         = W5                    ' random value


' -----[ Initialization ]--------------------------------------------------

  PAUSE 250                                     ' let AP-8 power-up
  SEROUT Sio, Baud, ("!!!!!!!AP8", %00, "X")    ' stop on reset


Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00000001                              ' make P0 an output

  pos = 150                                     ' start at center
  timer = 100
  GOSUB Servo_Pause


' -----[ Program Code ]----------------------------------------------------

Main:
  RANDOM lottery                                ' stir random #
  pos = lottery // 101 + 100                    ' randomize, 100 to 200
  RANDOM lottery
  timer = lottery // 2001 + 500                 ' random, 500 to 2500
  GOSUB Servo_Pause                             ' move servo and hold
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


Servo_Pause:

  PULSOUT Servo, pos                            ' refresh servo
   IF timer < 20 THEN SP_Exit
    PAUSE 19                                    ' delay (minus servo pulse)
     timer = timer - 4                          ' update timer
    GOTO Servo_Pause

SP_Exit:
  RETURN

' -------------------------------------------------------------------------

Start_AP8:
  SEROUT Sio, Baud, ("!AP8", %00, "P", 0)     ' play segment
  RETURN
' -------------------------------------------------------------------------


Let_AP8_Finish:
  SEROUT Sio, Baud, ("!AP8", %00, "G")          ' get status
  SERIN  Sio, Baud, status
  IF playing = Yes THEN Let_AP8_Finish
  RETURN
' -----[ User Data ]-------------------------------------------------------
Jim Adams

JonnyMac

Here's a variation to try.  What I added is the program checking the change (delta) from the last position to the new and making sure that's at least 20 units (so it can be seen).  A delay for Servo_Pause is calculated from that.  I checked a servo once and found it took about one second to make a full move -- so we could use 10ms per unit; I bumped this to 15ms per delta unit to give the servo time to move and get set.  Once it does it plays the sound, then calculates a random hold time as in your original.

' =========================================================================
'
'   File...... Laser_Tag.BS1
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated... 10/24/2010
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------

' Servo snaps to a random location, AP-8 fires laser


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Sio             = 7                     ' SETUP = out; no ULN
SYMBOL  Servo           = 0                     ' tail servo


' -----[ Constants ]-------------------------------------------------------

SYMBOL  Baud            = OT2400

SYMBOL  IsOn            = 1                     ' for active-high in/out
SYMBOL  IsOff           = 0

SYMBOL  Yes             = 1
SYMBOL  No              = 0


' -----[ Variables ]-------------------------------------------------------

SYMBOL  status          = B0
SYMBOL   playing        =  BIT7

SYMBOL  pos             = B2
SYMBOL  last            = B3
SYMBOL  delta           = B4

SYMBOL  delay           = W4
SYMBOL  lottery         = W5                    ' random value


' -----[ Initialization ]--------------------------------------------------

Power_Up:
  PAUSE 250                                     ' let AP-8 power-up
  SEROUT Sio, Baud, ("!!!!!!!AP8", %00, "X")    ' stop on reset

Reset:
  PINS = %00000000                              ' clear all
  DIRS = %00000001                              ' make P0 an output

  pos = 150                                     ' start at center
  delay = 1000
  GOSUB Servo_Pause                             ' hold for one second


' -----[ Program Code ]----------------------------------------------------

Main:
  RANDOM lottery                                ' stir random
  pos = lottery // 101 + 100                    ' randomize, 100 to 200
  IF pos >= last THEN CCW

  ' code keeps delta positive

CW:
  delta = last - pos
  GOTO Check_Delta

CCW:
  delta = pos - last

Check_Delta:
  IF delta < 20 THEN Main                       ' force bigger move
    last = pos                                  ' save for next cycle

  delay = 15 * delta                            ' time for move
  GOSUB Servo_Pause                             ' move servo

  GOSUB Start_AP8                               ' play sound

  RANDOM lottery                                ' stir again
  delay = lottery // 2001 + 500                 ' random, 500 to 2500
  GOSUB Servo_Pause                             ' hold

  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------

Servo_Pause:
  PULSOUT Servo, pos                            ' refresh servo
  IF delay < 20 THEN SP_Exit
    PAUSE 18                                    ' delay (minus servo pulse)
    delay = delay - 20                          ' update timer
    GOTO Servo_Pause

SP_Exit:
  PAUSE delay                                   ' finish delay
  RETURN

' -------------------------------------------------------------------------

Start_AP8:
  SEROUT Sio, Baud, ("!AP8", %00, "P", 0)       ' play segment
  RETURN

' -------------------------------------------------------------------------

Let_AP8_Finish:
  PAUSE 25
  SEROUT Sio, Baud, ("!AP8", %00, "G")          ' get status
  SERIN  Sio, Baud, status
  IF playing = Yes THEN Let_AP8_Finish
    RETURN

' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

Jadams

Jim Adams

Jadams

It does work very well, but in trying to better understand the logic, where does B3 get it's value for 'last'?
Jim Adams

JonnyMac

You're misunderstanding the use of SYMBOL.  Internally, the byte-sized variables are called Bx (B0, B1, etc.) and the word-sized variables are called Wx (W0, W1, etc.).  Using SYMBOL lets us rename variables to something friendlier.  After pos (B2) has been used with last (B3) to determine the change in position, pos is copied into last for the next cycle.
Jon McPhalen
EFX-TEK Hollywood Office