Shop OBEX P1 Docs P2 Docs Learn Events
building computer controlled car and need help with some code — Parallax Forums

building computer controlled car and need help with some code

eagletalontimeagletalontim Posts: 1,399
edited 2008-10-19 23:50 in General Discussion
Hey everyone again [noparse]:)[/noparse] I have been working with a VB program that communicates with the SX chip through a serial connection and I have run into a stump. I have been able to get the drive motor to go forward and backwards with the arrow keys, but now I am working on the servo to steer it. I am using a digital Futaba servo FP-S148 indirect drive.

The problem I am having is that now that I have added the code to center the servo when no activity is being sent, the drive motor will sometimes continue to move even though I do not have a button pressed. I was thinking about using an interrupt, but I am not sure about the timing on the chip and using the pulsout command.

Here is the code so far :
' =========================================================================
'
'   Original Author.... Jon Williams, EFX-TEK
'
' =========================================================================
ID              "srobot"

DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX, BOR42
FREQ            4_000_000


' -------------------------------------------------------------------------
' I/O Pins
' -------------------------------------------------------------------------

Drive1        PIN    RC.7 OUTPUT
Drive2        PIN    RC.6 OUTPUT
Turn        PIN    RC.5 OUTPUT

RX        PIN    RA.0 INPUT        ' via MAX232
Sout        PIN    RA.1 OUTPUT


' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------

IsOn        CON    1
IsOff        CON    0

Baud        CON    "N9600"

SYNC        CON    $80            ' start of packet marker

MTR_X        CON    $01
MTR_Y        CON    $00
MTR_Z        CON    $7F

' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------

tmpW1        VAR    Word
cmd        VAR    Byte
idx        VAR    Byte
in        VAR    Byte (3)        ' move per motor
DriveMotor    VAR    Byte
SteerMotor    VAR    Byte
ExtraMotor1    VAR    Byte
f        var    Byte

DELAY_MS    SUB    1, 2            ' shell for PAUSE
RX_BYTE        FUNC    1, 0            ' shell for SERIN


' =========================================================================
  PROGRAM Start
' =========================================================================

Start:
  PLP_A = %0000_0001                ' set pull-ups 
  PLP_B = %0000_0000
  PLP_C = %1111_0000

Main:
  for f = 1 to 136
  PULSOUT Turn, 150
  'DELAY_MS 20
  cmd = RX_BYTE
  Next
  
  IF cmd <> SYNC THEN Main

Get_Moves:
  FOR idx = 0 TO 2
    cmd = RX_BYTE
    IF cmd = SYNC THEN Get_Moves        ' fix busted packet
    in(idx) = cmd
  NEXT

MoveMotor:
  DriveMotor = in(0)
  SteerMotor = in(1)
  ExtraMotor1 = in(2) 
  IF DriveMotor = "1" THEN
    Drive2 = IsOff
    Drive1 = IsOn
  ELSEIF DriveMotor = "3" THEN
    Drive1 = IsOff
    Drive2 = IsOn
  ELSE
    Drive2 = IsOff
    Drive1 = IsOff
  ENDIF
  'SEROUT Sout, Baud, DriveMotor
 GOTO Main


SUB DELAY_MS
  IF __PARAMCNT = 1 THEN
    tmpW1 = __PARAM1
  ELSE
    tmpW1 = __WPARAM12
  ENDIF    
  pause tmpW1
ENDSUB

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

FUNC RX_BYTE
  SERIN RX, Baud, __PARAM1
  ENDFUNC



Thanks for any help in advance!

Comments

  • eagletalontimeagletalontim Posts: 1,399
    edited 2008-10-19 14:57
    I have made a few changes to the code hoping to get something to work, but still no luck [noparse]:([/noparse] I have noticed that when I send data to the chip, it has a long delay till anything else can be sent. I think it is getting stuck in the for loop for a few seconds while it tries to move the servo. once it is completed, I can send another command and it will do what it is supposed to. Any ideas on what i can change or do?
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-10-19 23:34
    try an interrupt on the receive pin. You can then run the receive code in the interrupt. there are a lot of examples of how to receive serial data in the interrupt, mostly using inline assembly to streamline things.

    Nick
  • eagletalontimeagletalontim Posts: 1,399
    edited 2008-10-19 23:50
    thanks! I did not think about doing it that way.
Sign In or Register to comment.