PDA

View Full Version : [Question(s)] Pic servo control question



Pi Guy
08-02-2009, 12:46 AM
I am interested in programming a Pic to continuously control 2 servos on my robot's engine when my Propeller Chip tells it to. I unfortunately have only been working with the Pic for less than a week making tasks that much harder.

I originally planned to use pin RA3 on my Pic16F684 to detect the binary signals 10 or 11 to choose the servo, then the regular pulse requested to have repeated by the Pic every 20ms. But during my earlier tests I found the pulse time the Pic repeated was longer than the input pulse time. Since I know little C, I do no know if there is a way to detect timing other than the way I tried, which was done by incrementing a variable during the time the pulse was presented.

Working with the Propeller Chip, I know it can detect the time it took to complete a task by looking up the number of clock cycles. Is there something similar on the Pic, or should I take a different approach?

jes1510
08-02-2009, 11:13 AM
For similar devices, look at the AVR family. They have a bit bigger hobbyist following I think. The Roboduino is a pretty good robot/AVR development platform and there is no programmer required. Plus it is supported by the Arduino IDE and libraries which makes it much more entry level friendly. It's also a pretty good way to get your feet wet in C.

Out of curiosity, why not use a serial servo controller?

Pi Guy
08-02-2009, 01:04 PM
I am currently learning C out of a book I already have. I am thinking about getting an Arduino later this year, though.

The reason I don't buy a servo controller is because I already have the parts necessary lying around, and it would be useful to learn how to do this in C anyway. Also, I was planning to connect it to my servo to have it as a more permanent feature and allow me to reduce the number of servo cables I need to have leading away from my engine. It is maybe two feet from my engine to the front area of my robot that I will hold all the electronics.

Adrenalynn
08-02-2009, 03:12 PM
An AVR can be programmed in C using _free_ tools.

But, depending upon the PIC, it does have interrupts [a concept the Propeller has renamed/changed a little], and with interrupts you get timers.

Pi Guy
08-02-2009, 03:49 PM
That could work, but I'll have to do a little research on it. I have an SX book here that talks about it a little, but it works with assembly. Would it be very difficult to then convert the code in ASM to C?

Adrenalynn
08-02-2009, 03:55 PM
Not if you're an expert in both PIC Assembler and whatever flavor of C you're coding to - then it's only "moderately difficult". ;)

If you're not going to learn interrupts and timers inside and out, you're probably better off with a different processor and a different language.

Pi Guy
08-02-2009, 04:44 PM
Hmm, that will have to wait then. I do plan to eventually get this kit though: http://www.robotshop.us/robotshop-kit.html
Think it's processor could do the job?

Adrenalynn
08-02-2009, 05:09 PM
Any reasonably sized microcontroller can drive a servo or two. It's not about what the processor can do, - it's entirely about what the programmer can create. PIC, Arduino, what-have-you - microcontrollers depend extensively upon interrupts and, supplementarily - timers. Without interrupts (or some message passing architecture that utilizes them), realtime behavior doesn't happen.

That said, some learning architectures are designed to insulate one from those concepts for as long as possible.

Is there a reason not to jump in and learn the concepts? They're not that tough really! Have a read through Fergs' tutorial on interrupts (written for the AVR architecture, but the concepts are similar across the board)

Pi Guy
08-02-2009, 05:32 PM
It's not that I don't wish to learn it, it's just I'm trying to figure out what works. I will read through the tutorial and see if it will help me somewhat, but right now I'm in the dark.

lnxfergy
08-02-2009, 05:44 PM
Ways to Read a Pulse Length (on an Arduino):

Option 1 - Use the pulsein command to read in the pulse. Pulsein is pretty accurate. It waits for the pulse to start, then loops around. You have to know the exact length of your loop, best timing is going to be plus/minus the length of your loop.

Option 2 - Use an interrupt, configured for rising and falling edges, when the interrupt triggers on rising edge we mark the time, when the interrupt triggers going down, we subtract the original time from the current time, we have pulse width. Have to time out how long the transition into the ISR is. Best time will be plus/minus the true width of the system clock (several uS on the Arduino I believe).

Option C - Use a timer/counter, setup to count only when the pin is high. Best time will be plus/minus width of the timer clock... likely not a good option as it will eat up the 16-bit timer/counter, which we need for:

Controlling a Servo with an Arduino:
The servo library uses timer/counter1 (the only 16-bit t/c) to control up to 2 servos. Really easy to use.

-Fergs

Pi Guy
08-02-2009, 06:11 PM
Helpful, but I don't know what the differences are between the Arduino and the Pic to know what I can do. Should I just get an Arduino to do this task since there is much more info out there on them?

Adrenalynn
08-02-2009, 06:32 PM
You know, I just have had a lot more luck with the Arduino writing my own servo drivers. I've just not been happy with the servo library for Arduino - but I haven't looked at it in six months, so it may have been rewritten (if for no other reason than I think you'll find on the Arduino forum a lot of grousing about it)

[edit: Didn't see the second page. My response was fyi and re: Fergs]

lnxfergy
08-02-2009, 08:15 PM
You know, I just have had a lot more luck with the Arduino writing my own servo drivers. I've just not been happy with the servo library for Arduino - but I haven't looked at it in six months

A little while ago, the servo library became integrated into the default Arduino libraries. Since then, it's been fairly decent. EDIT: I'm talking about the HArDWARE servo library here (the one that uses Timer1 to control 2 servos), the software servo library was still a steaming pile last time I saw it.....

-Fergs

lnxfergy
08-02-2009, 08:17 PM
Helpful, but I don't know what the differences are between the Arduino and the Pic to know what I can do. Should I just get an Arduino to do this task since there is much more info out there on them?

In my opinion, yes. The reason is that the Arduino is a very specific AVR, with a huge user base, primarily hobbyist. The "PIC" can be any of several families, of which many are incompatible with each other. Further, open-source tools for PIC are few and far between, meaning there isn't a very large hobbyists user base (and thus, it will be harder to find people who are willing to help out).

-Fergs

Pi Guy
08-02-2009, 09:05 PM
"it will be harder to find people who are willing to help out"
Noticed. Well, thanks a lot and I'll check into it. I just wish I could use existing parts.

lnxfergy
08-02-2009, 10:20 PM
"it will be harder to find people who are willing to help out"
Noticed. Well, thanks a lot and I'll check into it. I just wish I could use existing parts.

That's also just a by-product of who is on this particular board. Most of our most-active posters are AVR/arduino users. I can only think of one or two PIC users, who may be along shortly, but don't visit on a daily (or hourly) basis....

-Fergs

Adrenalynn
08-02-2009, 11:18 PM
It's funny - I've used the PIC a fair bit commercially, but only once in a hobby project and that years ago. Since "discovering" the ATTiny AVR parts, I don't get near the PIC at any level.

I might have been playing with the software servo library - and it certainly wasn't integrated into the standard libs, so I should probably go back and look at the Arduino servo libs again.

Pi Guy
08-03-2009, 12:11 AM
"Since "discovering" the ATTiny AVR parts, I don't get near the PIC at any level."

I still have time to "discover" my own microcontroller preferences as I'm still young.

jes1510
08-03-2009, 12:34 AM
Adrenalynn, The new Servo library that is now integrated into the standard distribution is much better than the old software library. The latest one uses a hardware timer though so be aware of all the usual byproducts.

Adrenalynn
08-04-2009, 07:05 AM
MODERATOR NOTE: The discussion of Polling vs Interrupts was moved here (http://forums.trossenrobotics.com/showthread.php?t=3464) ... It was just getting a little too far afield to be on-topic.

No big deal - just cleaning the thread up to bring it back around to the OP's query.

MikeG
08-04-2009, 08:42 AM
You can download a servo driver for the Propeller on the object exchange.
http://obex.parallax.com/

It takes up one cog. That way you don't need any extra hardware.

Pi Guy
08-04-2009, 10:36 AM
Yeah, but it was my intent to save on propeller memory. Riding at 30mph it will already have a load of work to do to keep itself from wrecking.

MikeG
08-04-2009, 08:02 PM
Each of the 8 COGs are deterministic at 20MIPS each or 160MIPS in total. The servo driver executes in a single COG and can control 32 servos. Very little HUB memory is needed. At 30mph that's 37,878 instructions per inch per COG.

Pi Guy
08-05-2009, 12:30 AM
Yeah, well, it's a battle you're not going to win. When I get all I want I will be working with:
1 or 2 ir heat sensors
gps
3 sonar sensors
many front bumper ir sensors
break motor
steering motor
servos for each sonar sensor
2 servos for the engine
rpm measurement from the engine
actual rpm of shaft after leaving clutch to detect slippage
accelerometer
compass
sd card writing/reading
emergency warning leds
cmu cam
rf transmitter/receiver
-and a lot more once I have all this
Some people have already tried telling me I won't have a space problem, but trust me, I will.

lnxfergy
08-05-2009, 06:26 AM
Ok, now I have to ask: What exactly do you intend to do with a CMUCam on a bot moving 30mph?

-Fergs

MikeG
08-05-2009, 08:41 AM
Wow so adding 2 servos pushed the Propeller memory over the edge. Still very impressive that you're controlling all those devices with a Prop.

Pi Guy
08-05-2009, 11:06 AM
I was hoping to spread out the use, give me more time for calculations and such. Also, I'm currently not doing all that with the prop. I only have the hardware for 1/3 of it and I'll buy the rest in the next couple weeks. My robot isn't quite done either, but after doing some welding on the steering system yesturday I'm ready to test it out. If all works, it will be autonomous without breaks, which I will finish up in a week or two. Then it's the harder part of getting my 1/3 hp 12v motor hooked up so I can go backwards with the bot, then the hardest pard will be the alternator because I have to modify the engine shaft as well as mount the alternator in a way that it can tighten the belt easily. I'm also not sure how to run it, I'll probably end up wandering around some junkyard looking for a battery charger I can connect to it.

Pi Guy
08-06-2009, 04:29 PM
Here is a pic of what I'm doing this for. I'll post a thread specifically on this later once I get bit more done on it.

Adrenalynn
08-06-2009, 04:30 PM
Cool chassis! Looks like it'd be interesting for a Magellan-type application.

Now, if you could control one of those Llamas with a PIC, that'd ROCK! :D

Pi Guy
08-06-2009, 05:11 PM
Idk about controlling them, but I did think about chasing them :p
As for the chassis it really isn't all that great. After many weeks of working on it it still came out with a lot of problems. I think I've done a good job covering them up and fixing them though. I've definitely learned a great deal about mechanics and welding while building it, and now its time to do the same with it's software.