EFX-TEK

TEK Talk => Prop-SX => Topic started by: threewheeler7 on January 08, 2009, 03:11:10 PM

Title: Servo control Questions (Prop-sx)
Post by: threewheeler7 on January 08, 2009, 03:11:10 PM
 Hello, I have quite a bit of experience in microcontrollers, (mostly Microchip's PIC) electronics and some experience in servo and motion control. I am trying to get into this whole Prop-sx thing for some control stuff. Anyway what I would like to know is, how do I embed servo control on a Prop-sx? Not yet having a full understanding of the language yet, I am not sure how to use the template  "Prop + Servo Control (Advanced)". How do I put in what i wan  my servos to do, and when to do it? I really just want some help getting started.

Title: Re: Servo control Questions (Prop-sx)
Post by: JonnyMac on January 08, 2009, 03:40:02 PM
The SX28 is like a really fast PIC16C57 so you should have much trouble with it.  We use SX/B which Parallax provides at no charge and mix in assembly where it makes sense to do so.  The program you reference (http://www.efx-tek.com/php/smf/index.php?topic=11.msg13#msg13) is actually old, so I went in an updated it to current SX/B standards (1.51.03).

The servo control runs in the background; this makes P8 - P11 behave like a stand-alone servo controller.  You can move a servo by updating its position element in the array called "pos."

Lets say you wanted to sweep the servo connected to P8 back and forth; you could do this:

Main:
  FOR idx = 100 TO 200
    pos(0) = idx
    WAIT_SYNC
  NEXT
  DELAY_MS 500

  FOR idx = 200 TO 100 STEP -1
    pos(0) = idx
    WAIT_SYNC
  NEXT
  DELAY_MS 500

  GOTO Main


This is a very simple example.  The thing that you don't have to worry about is the servos getting refreshed every 20ms as that gets handled by the ISR.  Just tell the servo where you want it to be and the code will take care of the rest.

Title: Re: Servo control Questions (Prop-sx)
Post by: threewheeler7 on January 09, 2009, 07:28:11 AM
 Oh I see, that helps a little. What is the "STEP -1" for? Also how do do things like control speed?
Title: Re: Servo control Questions (Prop-sx)
Post by: JonnyMac on January 09, 2009, 07:47:23 AM
STEP -1 causes the FOR-NEXT loop to count backward.  STEP is an option with FOR-NEXT that overrides the normal behavior of adding one to the control variable until it reaches the destination value.  To speed up the motion, for example, you could use step values of 2 and -2; this would cause the servo to jump to the intermediate positions.

Servo speed depends on how long you spend at a given position. In the loops above the sweep takes about two seconds (100 total steps @ 20ms each).  If you change WAIT_SYNC to DELAY_MS 40 will slow the servo.  Once you get a basic loop running it's a good idea to spend time playing with it -- there are many ways to skin a cat, you've got to find the one that works best for you.

When you were programming PICs, what language did you use?
Title: Re: Servo control Questions (Prop-sx)
Post by: threewheeler7 on January 09, 2009, 07:55:18 AM
When I program PIC's I use pure assembly. So what if I want to be moving more than one servo at a time? What would a Loop look like then?
Title: Re: Servo control Questions (Prop-sx)
Post by: JonnyMac on January 09, 2009, 08:14:34 AM
That's what I'm getting to: you may want to use SX assembly as the instruction set is very close to the PIC.  I'm not a very big assembly guy so I use SX/B as a framework and mix in assembly segments where I want the best efficiency.

To move multiple servos you either have to pull moves from a table (this can eat up program space so it might be best to build the moves and store them in an EEPROM) or setup the program as a state machine.  Again, if you could do it before on a PIC, you can do it now on an SX -- it's just simple language translation.
Title: Re: Servo control Questions (Prop-sx)
Post by: threewheeler7 on January 09, 2009, 09:13:31 AM
Ok, I guess I can toy around with that a little. How easy is it to do EEPROM reads with sx? And also Is there some kind of eeprom editor i would use and how do I get it to the EEPROM on the Prop-sx? So I would use a format like:                 (_servo#_, _servo speed_, _servo position_ ) for each movement on the EEPROM, and just call EEPROM reads for the number of movements needed at that point of the program?
Title: Re: Servo control Questions (Prop-sx)
Post by: JonnyMac on January 09, 2009, 09:27:49 AM
It's pretty easy to read an EEPROM -- just takes a few routines (I've code them and they're in my SX/B template for the Prop-SX).  You might have to customize a program if you want to add movement speed as well as target position.  I typically setup a table that gets read every 20 to 30 ms and dumped into the servo controller.  This take more table space but simplifies program code.

One of my ongoing projects is a program that will translate servo moves from VSA to an EEPROM table that can be downloaded into the EEPROM on the Prop-SX.  Stay tuned (it's part of a big pile of projects for this year so I don't have a release date).

PS: You might want to join the Parallax SX forum as well -- there is a lot of advanced programming topics discussed there that will help you with the Prop-SX (forums.parallax.com).
Title: Re: Servo control Questions (Prop-sx)
Post by: threewheeler7 on January 09, 2009, 09:44:53 AM
Do you have an example of a servo program that uses table reads I can look at?  Also do you need any help working on your VSA to eeprom table program?
Title: Re: Servo control Questions (Prop-sx)
Post by: JonnyMac on January 09, 2009, 09:56:00 AM
There are lots of table demos for the Prop-2, I don't have one for the Prop-SX.  The process is the same, though: You set your values into a table with the DATA directive and then use READ or READINC to read the table.  Let's say you have four servos and you're going to store their raw position values; the table might look like this:

Servo_Moves:
  DATA  150, 150, 150, 150
  DATA  155, 150, 150, 150
  DATA  160, 160, 150, 150
  DATA  $FF, $FF, $FF, $FF   ' end of table marker


Then, you could read the table like this:

  addr = Servo_Moves
  DO
    READINC addr, tmpB1, tmpB2, tmpB3, tmpB4
    IF tmpB1 = $FF THEN EXIT
    PUT pos(0), tmpB1, tmpB2, tmpB3, tmpB4
    DELAY_MS 40
  LOOP


This is a coarse an off-the-top-of-my-head example to show you some SX/B features.  You should go through the examples in the help file to learn the language (I'm also writing a book on SX/B for Parallax that will be available in a couple months).
Title: Re: Servo control Questions (Prop-sx)
Post by: threewheeler7 on January 12, 2009, 12:07:30 PM
Ok, that makes sense. How would your 4 servo program be modified to refresh 6 servos?
Title: Re: Servo control Questions (Prop-sx)
Post by: JonnyMac on January 12, 2009, 12:38:07 PM
You would have to update the servo driver in the interrupt, and if using the same pin group give up two of the trigger pins -- pretty easy changes, really.