November 25, 2024, 02:00:32 AM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


Servo control Questions (Prop-sx)

Started by threewheeler7, January 08, 2009, 03:11:10 PM

Previous topic - Next topic

threewheeler7

 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.


JonnyMac

January 08, 2009, 03:40:02 PM #1 Last Edit: January 08, 2009, 03:55:14 PM by JonnyMac
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.

Jon McPhalen
EFX-TEK Hollywood Office

threewheeler7

 Oh I see, that helps a little. What is the "STEP -1" for? Also how do do things like control speed?

JonnyMac

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?
Jon McPhalen
EFX-TEK Hollywood Office

threewheeler7

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?

JonnyMac

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.
Jon McPhalen
EFX-TEK Hollywood Office

threewheeler7

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?

JonnyMac

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).
Jon McPhalen
EFX-TEK Hollywood Office

threewheeler7

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?

JonnyMac

January 09, 2009, 09:56:00 AM #9 Last Edit: January 09, 2009, 09:57:34 AM by JonnyMac
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).
Jon McPhalen
EFX-TEK Hollywood Office

threewheeler7

Ok, that makes sense. How would your 4 servo program be modified to refresh 6 servos?

JonnyMac

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.
Jon McPhalen
EFX-TEK Hollywood Office