servo control

For issues with communication ports, protocols, etc.
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

servo control

Post by phishguy »

I wrote a little routine to control a parallax USB or serial servo motor controller. In the off chance that someone may some day want to experiment with controlling servos, here is the code:

Code: Select all


Dim bad As Integer
Dim good As Integer
Dim center As Integer
Dim stroke As Integer
Dim position As Integer
dim t as integer
Dim servocom As Integer

Declare Sub servo(position As Integer)

stroke = 50 'servo stroke value
center = 700 'center position value of servo
bad = center - stroke 'bad position value of servo
good = center + stroke 'good position value of servo
servocom = 6 'change to your serial port

' setup servo controller com port
Open com "com" & servocom & ":,2400,n,8,2,cs0,ds0,cd0,rs" As #3
t = Err
If t <> 0 Then
    Print "Check Servo Controller port selection"
    Sleep
    End 
End If
sleep 100

while inkey$ = ""
position = bad
servo(position)
sleep 50
position = center
servo(position)
sleep 450
position = good
servo(position)
sleep 50
position = center
servo(position)
sleep 450
wend
end

' servo movement routine
Sub servo(position As Integer)
  ' send command
    Print #3,"!SC" ; Chr$(0) ; Chr$(0) ; chr$(lobyte(position))_
  ; Chr$(hibyte(position)); Chr$(13);
End Sub


Last edited by phishguy on Mar 26, 2007 13:55, edited 1 time in total.
Barbaros
Posts: 1
Joined: Mar 24, 2007 13:26
Location: Paris
Contact:

Which environment?

Post by Barbaros »

Hello phishguy,

Could you please precise under which environment are you programming?
WIN98?, XP?, Vista? DOS? Linux?...

I am very interested in using LPT port under XP (and Vista in the futur).
This is in order to control some Electronic connected beyond LPT Port.
Until now I was working under DOS and Intel Assembly Language.
It is time to move to something new with FreeBASIC and use the PC Motherboard like a super calculator.
Some 3-4 year old PC Boards are still very usefull and free of charge.
Very little people think that a PC is wonderfull device at almost no cost.
Everything is already build-in : Display, Keyboard, Memory (disk) and all kind off communications capabilities ...
With little programming (FreeBASIC) and some Electronics you can almost control everything through LPT Port ...

Regards,
Eric.
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

I was running under Win2K at the time. However, it shouldn't really matter which OS. Anyway, as I mentioned, this uses a serial port or a USB port. Parallax has both versions. I picked the USB version because most newer PC's don't have serial ports anymore.
jtlodi
Posts: 3
Joined: Jan 02, 2008 22:50
Location: Rural Wisconsin, USA

Interacting with Serial Ports

Post by jtlodi »

Hi Phishguy ... came across your post about servos. I am brand new to FB but have been programming in QB, VB, VisualFoxPro for lots of years. I have a stepper motor control board from Eggert Electrics which interactively operates steppers using QB at the DOS level (Print #1, ... ). Of course I have to run QB in DOS and am looking for a way to run the code under Windows.

Is FB able to control serial ports while running under XP? or do I need some sort of serial port dll control? Windows took away direct access to ports and that caused a lot of hassles.

Hopefully you will tell me that it is a piece of cake in FB!

Regards,

Jim in Lodi
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Post by sir_mud »

open com should work across all platforms now, DOS may still be a little screwy since the driver is largely untested though.
maddogg6
Posts: 824
Joined: Dec 07, 2005 22:58
Contact:

Re: Interacting with Serial Ports

Post by maddogg6 »

jtlodi wrote:Is FB able to control serial ports while running under XP? or do I need some sort of serial port dll control? Windows took away direct access to ports and that caused a lot of hassles.
In short - Yes.
FB - when using a serial port in windows - will install a 'temporary driver' - in that, 1) you need to run it first with admin rights 2) need to install that driver each time you re-boot the PC. - or so thats my understanding.

http://www.freebasic.net/forum/viewtopi ... allel+port
http://www.freebasic.net/wiki/wikka.php ... yPgOpenLpt

jtlodi wrote:Hopefully you will tell me that it is a piece of cake in FB!
- Well - does 'Cow pies' count? - j/k - sorta...
I *think* (looking at the posted code above)

Code: Select all

Print #3,"!SC" ; Chr$(0) ; Chr$(0) ; Chr$(lobyte(position))_
  ; Chr$(hibyte(position)); Chr$(13);
is assuming the microcontroller receiving the serial port commands - is interpreting those commands - and causing the electrical signals needed to control the servo motor - Now for using the LPT port to directly control a stepper - is probably not advised anyway- too easy to darw too much current, thus some interfacing is needed - and IMHO - using a micro controller like the OP did, that interfaces/interprets commands is a *wiser* choice.

Not to mention:
http://www.freebasic.net/forum/viewtopi ... allel+port
Under Windows NT/2000/XP a DOS application can freely access only the I/O ports that the system is not using, and a normal ring3 Windows application cannot (directly) access any I/O ports. FB can install a driver that can provide access to the I/O ports, but you may be very limited in what you can do with an I/O port that Windows is using.
In my limited attempts to control individual pins in a parallel port was not too good. Thus, a MCU between the PC and the stepper motor, and use a serial port to communicate between the PC and MCU - is a solution I know can work for certain.

But things change quickly in FB - so maybe that has since improved??

I hope that all made sense. ??
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

From looking at the quickbasic example posted in the documentation, you should be able to compile that example using the -lang qb switch with little or no program changes. Give it a try and let us know if you need any help.
Eviltoaster
Posts: 26
Joined: Nov 08, 2007 16:59
Contact:

Post by Eviltoaster »

can i connect the parallax servo controller directly on the serial port with some transistors and resistors ?

or do i need a microcontroller?
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

Sure, it would be possible. However, it would probable be easier to use a max232 chip. This will provide the proper level translations of the signal.
Eviltoaster
Posts: 26
Joined: Nov 08, 2007 16:59
Contact:

Post by Eviltoaster »

http://macke.traeskmark.se/blandat/PC.bmp

is this possible to use directly to the controller?
or can you help me connect the max232 chip?

sry for all my questions, but im not good at this :P
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

That circuit should work just fine as long as you don't have a very long serial cable. (< 10ft)

BTW - the USB version of the servo controller may be a better option as it doesn't require a serial port on your PC. Many PC's, and most laptops no longer have serial ports available. This would give you better portability if you decide that you need to change PC's sometime down the road.
Eviltoaster
Posts: 26
Joined: Nov 08, 2007 16:59
Contact:

Post by Eviltoaster »

thanks a lot for the answers ..

why im using the serial version is becouse i got a old computer from the school with parallellport serialport and 1 usb port

+ i can buy more old stuff for nothing....
and i only spend 200 kr for everything .. its like nothing :P
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Post by mark bower »

happy to see this post for 2 reasons: 1) my interest in FB is primarily for engineering use, not gaming, 2) in the near future i plan to learn about stepping motors and employ their use in residential applications.

this post also reaffirms my belief that it would make sense to have a category called ENGINEERING DEV, similar to the existing GAME DEVeloper category. ENGINEERING DEV could scoop up all the com serial, parallel port, DAS card issues and address software for hardware of interest. i previously had posted register programming code talking to a DAS 1600 A/D card. and at least one other party is interested in handling base addressing under windows to talk to cards directly in windows (like we did under DOS), realizing that there is some complexity involved. anyhow, a separate category which co-locates software to hardware control issues could be a real advantage and would probably draw more engineering types into the FB camp.
mark bower
Last edited by mark bower on Feb 04, 2008 21:17, edited 2 times in total.
Eviltoaster
Posts: 26
Joined: Nov 08, 2007 16:59
Contact:

Post by Eviltoaster »

ENGINEERING DEV <3

i start to use fb just becouse it was so easy to play with LEDs on the parallelport .. and let my friends controll it over a webpage with webcam.. it was really fun and i start to love fb ^^
zoomkat
Posts: 15
Joined: Apr 08, 2007 22:31

Post by zoomkat »

Using the open command for the com port works quite well with fb. Below is some test code that I made for using with a serial servo conroller. Now that I have the controller, I see the code needs to be improved. This might be of interest if one wants to control the servos using a web interface. Bottom link shows a similar setup I have using batch files (but they can't query the controller for return info).

http://www.freebasic.net/forum/viewtopi ... highlight=

http://www.geocities.com/zoomkat/ezservo1.htm
Post Reply