I have now given the master an 8 command buffer. And finnally life is good ^^.
I think I am going to up the buffer to 16, but that will require that I use 2 buffer arrays. For now, heres the master code, feel free to make any suggestions, and just let me know if you use it in any projects.
:MSmaster:
Code:
'****************************************************************
'* Name : MSERVMASTER.BAS *
'* Author : Joseph Dattilo *
'* Notice : Copyright (c) 2007 Joseph Dattilo *
'* : All Rights Reserved *
'* Date : 5/25/2007 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
define OSC 8
OSCCON = %1111110 ' sets internal osc to run at 8MHZ
'----------turn analog on porta off, and use as digital input/output
'----------this is used to allow serial input on the A bus.
CMCON = 7 'Comparators OFF
ANSEL = 0 'A/D OFF -- Port pins all digital
'----------
'---**Pins
rsoutpin var porta.0
rsinpin var porta.1
picinpin var portb.4
picoutpin var portb.5
porta=0
portb=0
'---vars
temp var byte
qued var byte
outqued var byte
message var byte[24]
'-up to 8 commands can be buffered in que array.
que var byte[96]
'---------------
qued = 0 'set number in que to 0
goto pcin
'############################## pc recieve ################################
'que up messages from pc
'##########################################################################
pcin:
'---- let pc know we are ready to recieve message.
serout2 rsoutpin, 16468, ["waiting!"] 'call for data
'---- ready to recieve message
serin2 rsinpin, 16468, [str message\24]
'------------------------------------------------------------------------
'compare both arrays checking for bad data if data bad request it again.
for temp = 0 to 11 : if message[temp] <> message[temp+12] then goto baddata
next temp
'------------------------------------------------------------------------
serout2 rsoutpin, 16468, [str message\12] 'send confirmation of reciept.
'------------------inque-----------------
for temp = 0 to 11:que[(12*qued)+temp]=message[temp]:next temp
serout2 rsoutpin, 16468, ["Qued:",str que\(12*qued+12)] 'display all in que
'----------------------------------------
if message[11] = "n" then
serout2 rsoutpin, 16468, ["still more commands!?"] 'debug statement
qued=qued+1 'go to next position in que
goto pcin 'request the data for the next item.
else
outqued = 0 'set picout que start position to 0 (first item in que first)
goto picout 'we are ready to command the servo controller.
endif
'-------------------------
'where bad data takes you.
baddata:
serout2 rsoutpin, 16468, ["bad data!"]
goto pcin
'-------------------------
'############################## pic transmit #############################
'---- picout procedure waits for high on picinpin and then transmits data
'#########################################################################
picout:
'---------------------unque next item into message array
for temp = 0 to 11:message[temp]=que[(12*outqued)+temp]
message[temp+12]=que[(12*outqued)+temp]:next temp
'-------------------------------------------------------
input picinpin
while picinpin = 0
pause 1
wend
serout2 picoutpin, 16468, [str message\24]
pause 7
if picinpin = 1 then 'checks if data was recieved
goto picout 're-send until recieved
endif
serout2 rsoutpin, 16468, ["SUCCESS|",str message\12,"|"] 'tell computer
if outqued < qued then
outqued = outqued +1 'move to next qued item
goto picout 'proceed to transmit it.
else
serout2 rsoutpin, 16468, ["|ALL",DEC (qued+1),"items transmitted succesfully|"]
qued=0
goto pcin
endif
end
It is important that you attribute for the pause 7 in the master in the slaves procedure to prevent the master from constantly retransmitting the first message in the que.
This is the modified recieve part of the function which allows the mserv slave to recieve any number of messages in a row.
:MSslave:
Code:
'############################SLAVE RECIEVE################################
'######################################################################
recieve:
'----send serial request, and get user input for motor drive timeout 70 ms
high picoutpin
'----
serin2 picinpin, 16468,20,drivemotor, [dec2 servonum[0],skip 1,_
dec3 rate[0],skip 1,dec3 position[0],skip 1,donecommanding[0],_
dec2 servonum[1],skip 1,dec3 rate[1],skip 1,dec3 position[1],skip 1,_
donecommanding[1]]
if servonum[0]!=servonum[1] or rate[0]!=rate[1] or _
position[0]!=position[1] or donecommanding[0]!=donecommanding[1] then
'data is bad recieve again
goto recieve 'try again
endif
low picoutpin
'-----
'setup the servo chosen
goalarray[servonum-1]=position[0] 'set servo goal
ratearray[servonum-1]=rate[0] 'set servo rate
'
'allow for multiple commands.
if donecommanding[0] = "n" then
pause 7 'pause long enough to make sure master does not attempt retrans.
goto recieve
endif
goto drivemotor
Bookmarks