Serial communication, efficiency in programming

sailgene

Member
I'm sorry if this has been asked before but I couldn't find any threads. I'm working on an ROV using rs485 communication. One of the programs will be for the pan and tilt of a camera. I've written the code so that the sending picaxe (on the surface) includes the "loop" information to the receiving picaxe on the ROV. I've tested the code and the camera seems to respond to the commands with no particular problems.

Here is a sample of part of the code from the topside:

Init: 'sending chip master from topside - digital camera tilt control
setfreq m4
pause 2000
low c.1
let b3 = 150 'good servo position for vertical up/dn
let b4 = 155 'good servo position for horizontal rt/lt

start:
If pinc.3 = 1 then up
if pinc.4 = 1 then dn
if pinc.2 = 1 then rt
if pinc.5 = 1 then lt
goto start

up:
Let b0 = 1
for b1 = b3 to 175 step 2 'so b1 runs from 155 to 200
serout c.1, n2400_4, (b0, b1) 'send out data - 1,155
pause 40
let b3 = b1 'b3 now = 155 for starters
if pinc.3 = 0 then start
if b1 > 175 then start
next b1 'b1 now = 157
pause 40
goto start

A portion of the receiving picaxe code (rov) is something like this:

Main:
Setfreq m4 'set frequency at 4 mghtz to match other chip
pause 5000
servo c.1, 150 'vertical - center servo c.1,
servo c.2, 155 'horizontal - ditto
setint %000010000,%00010000 'set it up that if pin 4 (c.4) goes high, an interrupt occurs
pause 2050 'timing is everything relative to serin/serout

interrupt:
Serin c.4, n2400_4, b0, b1 'read data at pin c.4, b0 (1 or 2) & b1 (initial servo position)
if b0 = 1 then panupdn 'go to approp subroutine depending on b0 number
if b0 = 2 then panrtlt
goto interrupt 'if b0 not a 1 or 2, start over and read data again

panupdn:
Serin c.4, n2400_4, b0, b1 'reread data
if b0 = 2 then panrtlt
if b0 <> 1 then interrupt 'recheck if b0 has changed (no longer a 1)
let b2 = b1 'place b1 number (like 143) into b2
servopos c.2,b2 'move servo to position b2 - like 143
pause 20
if b0 = 1 then panupdn 'start over & read data again
setint %000010000,%00010000 'reset interrupt in case command changes
goto interrupt 'check again for new command info
return 'no idea but it is a needed code dealy

Here is my question: I'm wondering if it would be better or more efficient to have the "loop" programming at the receiving (ROV) end. In other words, would it be better to minimize the communication necessary to move the camera - to simply tell the camera to start or stop moving (from the topside) and to have the receiving picaxe (ROV) handle the code (loop information) to actually move the camera? Or is the communication fast enough that it really doesn't matter all that much? By the way, the communication tether is only 150 feet.

Ps: I'm sure many of you will ask, "where are the symbols, etc."? Sorry, but i'm still learning the basics here.
 

MPep

Senior Member
No problems re: Symbols. You will start using them as they make the whole program so much more readable :D.

As for the code location, top vs bottom side, that depends on how many more functions you require the top side to do. If things get rather intensive, or you are running out of code space then shift it down stairs.
It also depends on how many functions you are going to have.
If the PICAXE you're using for the Pan/Tilt is only for that function, then I would completely dedicate the whole PICAXE for all of it, and then only send info to PAN Left 1 degree, or something like that.


There is no correct way, only the correct way for your instance.
 

lbenson

Senior Member
First, when you post code, it is best to enclose it between the tags, [ code ] and [ /code ] (without the spaces). This will preserve the indentation in your code (and I hope there is indentation to make it clearer where the code blocks are). You can edit your original post to add the tags.

Second, a brief description of the values you expect to send between the two programs would be helpful. (For instance, a series of 2-byte codes are sent; for the first, a 1 indicates "tilt" and a 2 indicates "pan"; the second is the position to move to.)

Since the pan/tilt program (as shown) doesn't do anything except wait for instructions, there is no need for an interrupt (and anyway, your form of interrupt isn't correct--the "interrupt" routine must end with "return", and the interrupt should be reinitialized before you return). The program can simply sit "in serin" awaiting pan/tilt commands. Just make sure that the sending program pauses long enough between commands so that nothing is lost.

So the program could be like this:

Code:
main:
  servo c.1, 150 'vertical (tilt) - center servo c.1, 
  servo c.2, 155 'horizontal (pan) - ditto
  do
    Serin c.4, n2400_4, b0, b1
    select b0  ' only 1 & 2 are valid
      case 1: servopos c.2,b1 ' tilt
      case 2: servopos c.1,b1 ' pan
    endselect
    pause 20
  loop
It probably doesn't matter whether you send step-by-step servopos positions, or send a "to" position which you then step towards (you'd need to save the current pan and tilt positions). Try the step-by-step method, and if you have problems, look for other solutions.

My pan and tilt camera solution from 2009 (before servopos was available): http://www.picaxeforum.co.uk/showthread.php?13705-Rivercam-with-PICAXE-pan-tilt-control
 

sailgene

Member
Thanks for your help, guys. Sorry for the improper code posting. Still learning about coding AND the use of this forum. Thanks again.

Gene
 
Top