Servo and Serial at the same time.

Marcwolf

Senior Member
Hi Technical, Hippy,etc

I have several small projects where it would be very useful to use serial communications as well as the servo commands at the same time, or atleast suspend the servo operations until the serial op's have finished.

For example - retrieving servo movement commands from a VDrive or other system and then sending them out to the servo(s)

If I could turn off the servo's for a moment and then re-initialise them witoug changing the position data that would be great as well.

Do you have any suggestions or an idea if the PicAxe code will ever support this.

Many thanks
Dave
 

westaust55

Moderator
Have your tried (From PICAXE manual 2 page 213):

SERVOPOS pin,OFF

There were problems in the past with the OFF parameter - unsure if now fixed.

If not, then try:

LOW pin
 

BobMcNobby

Senior Member
I am doing something similar using HSerin and servos, I am more concerned about receiving the data so I have opted to simulate the servo command

... read data via serial
High Servo_Pin
PauseUs (calculated on time in the range 1ms-2ms)
Low Servo_Pin
... loopback for more data

I think the update rate of servos should be about 10ms, but I am sure this is flexable

Just an idea
 

g6ejd

Senior Member
As the servo command and serrx commands cannot be operated together, unless the PIC has a hardware UART, then @bobmcnobby2 has the answer, that is to simulate the servo command under programme control, i.e. to generate your own servo bit stream. The update rate is not usually critical, perhaps once a second would be adequate but depends on your application, what level of response do you need, if you want your servo to be reacting quickly, then this solution may not be suitable. In model aircraft a slow framerate (much less than 50Hz for 6-8 channels) would be unacceptable and lead to a crash, but not so for a slow lumbering robot, etc.,
 
Last edited:

hippy

Technical Support
Staff member
To extend on g6ejd's idea; you can generate the servo pulse, set a signal then use SERIN or SERRXD with a timeout. The sender can then wait for the 'allowed to send' signal and has a window in which to send its data.
 

nick12ab

Senior Member
Hardware PWM?
That wouldn't need constant attention of the program but will require the servo to be on a correct pin.
 

g6ejd

Senior Member
Not necessarily, as the output pin can be driven high for the required time within 1-2mS to set the servo position, then set it low again and move on for the rest of the framerate, lets' say of 100mS, giving the PIC time to check a serial port (in the 98mS gap) and then get back to set the PW again. The key is (unless performance is an issue) that the framerate can be slow.
 

joely87

Member
Hi All,

I have read this post and it is a fairly similar issue to the one I am having so thought I would avoid starting a new post. I have not attempted to manually generate the servo pulses as I would prefer to try this when I have access to a scope. I have posted a video on youtube to help show you how the servos are preforming and will paste my code here. Any feedback would be greatly appreciated. It is currently working (in the video) but i would like to optimize it to reduce the servo jitter and maybe also reduce the slight delay.

I get a little confused when it comes to hserin but am prepared to give it a try on a 20X2 if need be.

Here is the video http://www.youtube.com/watch?v=7cVSCLmLGeI

and the code for Tx:

Code:
' Pinout for PicaxeProtoPlatform using PICAXE 14M2 Microcontroller. 
' "P#" can be replaced with a descriptive word for the input/output pin.
' For example P19 Could be replaced with "GLED" to represent the connected Green LED

SETFREQ m8

'SYMBOL P1	= +V		SYMBOL P20 = 0V
 SYMBOL P2	= C.5		SYMBOL Tx 	= B.0
 SYMBOL Pot1= C.4		SYMBOL P18 	= B.1
 SYMBOL PUSH= pinC.3	SYMBOL TxPwr= B.2
 SYMBOL P5= C.2		SYMBOL VPOT	= B.3
 SYMBOL P6	= C.1		SYMBOL P15 	= B.4
 SYMBOL P7	= C.0		SYMBOL HPOT	= B.5


SYMBOL BAUD		= N2400_8
SYMBOL Ail		= B0
SYMBOL Elv		= B1
SYMBOL Thro		= B2

#PICAXE 14m2


main:
	pause 500				'allow time for Push Button to be low
	low TxPwr				'Save power by turning Tx Off
	if PUSH = on then init		'Arm Tx and begin sending
	goto main

init:
	High TxPwr				'Turn Tx Power on
	pause 500				'allow time for push button to be low

send:
	ReadADC HPOT, Ail			'Read Joystick Horizonal postion
	ReadADC VPOT, Elv			'Read Joystick Vertical postion
	ReadADC Pot1, Thro		'Read Pot value
	
	serout Tx, BAUD,(85,85,85,85,85,85,85,85,85,85,85,85,"433", Thro, Ail, Elv)	'85 is 101 which allow Rx to sync before transmitting data

 '	sertxd (#Thro,13,10)		'used to diagnose values
 '	sertxd (#Ail,13,10)
 '	sertxd (#Elv,13,10)

 ' 	pause 200				'used when diagnosing
	
	if push = on then main		'disarm Tx if button is pressed
	goto send

and the code for Rx:
Code:
' Pinout for PicaxeProtoPlatform using PICAXE 14M2 Microcontroller. 
' "P#" can be replaced with a descriptive word for the input/output pin.
' For example P19 Could be replaced with "GLED" to represent the connected Green LED

Setfreq m16


'SYMBOL P1	= +V		SYMBOL P20 = 0V
 SYMBOL P2	= C.5		SYMBOL Motor= B.0
 SYMBOL P3	= C.4		SYMBOL P18	= B.1
 SYMBOL P4	= C.3		SYMBOL P17	= B.2
 SYMBOL Rx	= C.2		SYMBOL Serv2= B.3
 SYMBOL P6	= C.1		SYMBOL P15	= B.4
 SYMBOL P7	= C.0		SYMBOL Serv1= B.5
 
 SYMBOL Ail = B0 
 SYMBOL Elv = B1
 SYMBOL Thro= B2
 
 SYMBOL BAUD = N2400_16

 
init: 
	servo Serv1 ,150 ; initialise servo
	servo Serv2 ,150 ; initialise servo
	servo Motor ,75
			
main:
 	serin Rx, BAUD,("433"),Thro,Ail,Elv	'Recieve values from Tx
 
 ' 	sertxd (#Thro,13,10)	'Send Values for diagnostics
 '	sertxd (#Ail,13,10)
 '	sertxd (#Elv,13,10)

 '	sertxd (13,10)		'Sen Values formated for SerialChart
 '	sertxd (#Thro,",",#Ail,",",#Elv)

 	let Thro = 255 - Thro	'Reverse Throtle
 	let Thro = Thro /2 +75
 	
 '	let Ail = 255 - Ail	'Reverse Aileron
 	let Ail = Ail /2 +75
 	
 	let Elv = 255 - Elv	'Reverse Elevator
 	let Elv = Elv /2 +75

 	servopos Serv2,Ail ; move servo to one end
 	servopos Serv1,Elv ; move servo to one end
 	servopos Motor,Thro ; move servo to one end
 
 	pause 30		'Alow sevos time to reach desired position before serial in
 	
	goto main ; loop back to start
 
Top