danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Add small cpu for R/C servo controlled

Sat Apr 20, 2013 4:02 pm

The Rpi is using linux and sometimes the real time process need more accuracy. This is a problem using servos in software mode. You can't be sure that the RPi will be always in time. Adding real time driver into the kernel is also time consuming.

Then why not using an other cpu to do the stuff! I know you could use the Pollolu serial servo but it is for one part maybe to rigid. So I decide to use a very small cpu , the Microchip pic12F1840.

This is a 8 pin dip and it will be able to drive 4 servos and it cost around $1.20 CAD per unit.

This was bought first to add analog capabilities for my Rpi but since a lot of people struggle with servos, I decide to start with a PWM module.

So first! look at the video https://dl.dropboxusercontent.com/u/488 ... iServo.MP4

And if you like it, continue the reading. This is only 1 part add-on to get 4 servos, up to 60, connected to the rpi on the GPIO14 port (uart TXM).

Image

I design the system in a way that you could connect up to 15 modules in the same Rpi Txm pin.
But if you use 15 modules you should buffer the pin using 74hc08 for example.

15 modules will put 60 servos or 60 leds with intensity controlled or 20 colors leds.
N.B. You need to put a resistor in serie with led to limit the current.

This is the schematic of the system.
Image

Quite simple is in it!

This is the source code in C https://dl.dropboxusercontent.com/u/48891705/RpiServo.c
This is the Hex file to program the cpu https://dl.dropboxusercontent.com/u/488 ... iServo.hex

Ok now to make it work on the Rpi, you need to disable the console and the system debug on the uart .

1 - in /boot/cmdline.txt removes "console=ttyAMA0,115200 kgdboc=ttyAMA0,115200"
2- in /etc/inittab comment out the last line
"#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100"
3- Install python serial module
sudo apt-get install python-serial

And this is my python routine to check it. (the same as the video)

Code: Select all

#!/usr/bin/env python
from time import sleep
import RPi.GPIO as GPIO
import sys, termios, atexit
from select import select
import serial

out = serial.Serial('/dev/ttyAMA0', 9600)

def SetPwm(Module , Channel , Value):
 out.write( chr(0x80 + (Module << 3) + (Channel)) + chr(Value >> 7) + chr(Value & 0x7f))

#set duration 20ms (default anyway)
SetPwm(0,7,2000)

#unlock Module Id
SetPwm(0,5,0)
#set Module Id to 2
SetPwm(0,5,2)

#do a little dance
for l1 in range(50, 250,50):
  for s1 in range(1,5):
    SetPwm(2,s1,l1)
    sleep(0.5)

#go to the middle
SetPwm(2,1,150)
SetPwm(2,2,150)
SetPwm(2,3,150)
SetPwm(2,4,150)
sleep(1)

# do it faster
for l1 in range(50, 250,50):
  for s1 in range(1,5):
    SetPwm(2,s1,l1)
    sleep(0.2)

sleep(2);

#set all servos to middle position
SetPwm(0,0,150)
sleep(1)

#turn them off
SetPwm(0,0,0)
If you want to use pwm for led instead , change the duration for 99. This way 0 will be OFF, 100 will be ON.


I know this is not for everyone but this is the spirit. Start little and build upon it.


P.S. I know my breadboard and scope are old , >35 years, but they still work. Why change them.

Daniel
Last edited by danjperron on Sat Apr 20, 2013 5:31 pm, edited 4 times in total.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: Add small cpu for R/C servo controlled

Sat Apr 20, 2013 4:38 pm

Great stuff :)

I like the fact that its only 8 pins :)

And its cheap :)
1 - in /boot/cmdline.txt removes "console=ttyAMA0,115200 kgdboc=ttyAMA0,115200"
2- in /etc/inittab comment out the last line
"#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100"
I'd need to script this (which I can do) to comment out those lines in my Scratch-GPIO installer but do you think there is a way to simply disable it each session by simply killing a process as I'd be much happier doing it that way?

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Sat Apr 20, 2013 4:55 pm

I don't think that normal user knows about it or uses the serial port anyway.

So I think a permanent fix is the way to go.

I'm not sure about the the debug and console in the kernel. Maybe is not an issue. I will need to check it.

And for the "/etc/initab" you could modify it and run "init q". This will force the reload of the inittab file.

Daniel

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Sat Apr 20, 2013 6:27 pm

I correct a small error in my code and some text typo.

If you have already download the hex and C code, please reload it again.

thanks

Daniel

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Fri May 31, 2013 1:55 am

It is now possible to program the PIC12F1840 using the RapsberryPi without any transistor or resistor.

Image

I just add the application , in python, into my Pic A/D converter Git repository.

https://github.com/danjperron/A2D_PIC_RPI search for 'burnLVP.py'.

Daniel

User avatar
mikerr
Posts: 2500
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Add small cpu for R/C servo controlled

Fri May 31, 2013 8:28 am

danjperron wrote:
I design the system in a way that you could connect up to 15 modules in the same Rpi Txm pin.
How would you connect extra modules ? A different code for each one?
Android app - Raspi Card Imager - download and image SD cards - No PC required !

User avatar
xranby
Posts: 538
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Add small cpu for R/C servo controlled

Fri May 31, 2013 9:04 am

danjperron wrote:The Rpi is using linux and sometimes the real time process need more accuracy. This is a problem using servos in software mode. You can't be sure that the RPi will be always in time. Adding real time driver into the kernel is also time consuming.
FYI: it is possible to run servos using the pi-blaster project to drive up to 8 PWM servos stable without using any extra cpu controller. pi-blaster uses the Raspberry Pi DMA controller in combination with timing from the built in PWM or PCM circruit to run the servos stable using the GPIO pins without any CPU usage.
http://hackaday.com/2013/02/10/better-p ... pberry-pi/
https://github.com/sarfata/pi-blaster/
danjperron wrote: So first! look at the video https://dl.dropboxusercontent.com/u/488 ... iServo.MP4
Awesome video, nice to see the pulse length live using your oscilloscope!
Well done
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Fri May 31, 2013 11:10 am

Yes I know about the DMA software. Very nice software btw.

I did this before that software was available. Also you could create a pattern of colour leds using the PWM mode. Up to 60 servos or 20 RGB leds without changing the current software.

Also this method use only one GPIO. All the other GPIOs are available for other things. (Robotics arm with sensors…)
My old small robots rover use the saber tooth motor driver, (4 X 5Amps),which could be connected to the same serial pin without problem.

It is just an alternative and yes if you just use servos and a small amount of GPIO, the software alternative is the choice. But if you want to have the minimum impact on your hardware use the cpu!

Also the biggest advantage about the cpu method is the electric isolation on the Rpi. A small resistor limit voltage and current to the port and you could use 5V on the servos and not worry about frying the Rpi. If the cpu failed , it is just $2CAD.

Thanks anyway to pointing that out.

I'm just curious , What is the % of ram access lost using the DMA. Does it do memory access or it just trick the system with timing. Just wandered what is the cpu degradation using the DMA? I assume that DMA steal memory access times.

Daniel

User avatar
joan
Posts: 12777
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Add small cpu for R/C servo controlled

Fri May 31, 2013 12:25 pm

Using DMA to sample gpios 0-31 200,000 times a second plus send PWM/Servo pulses to gpios 0-31 and all the related processing uses about 10% of the Pi's CPU.

I'd guess it uses about 0% of the memory bandwidth.

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Fri May 31, 2013 1:15 pm

To be frank,

I will use the Pi-Blaster to drive the h bridge in pwm mode when I will switch it to the cmos version.
( 0.2V drop instead of 3V).

But I can't use it for the servos. I need all the available pins and I want to add gyro, accelerometer, compass and some analog A/D (My pic A/D) to read voltages.

this is the post of my Rpi robot car. Look at the schematic.
http://www.raspberrypi.org/phpBB3/viewt ... 25#p347925
and the web interface http://www.raspberrypi.org/phpBB3/viewt ... 89#p353089

You know, alternative is always good.

Daniel

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Sun Jun 02, 2013 3:44 pm

Hi Mikerr,


They have all the same program!

If you look at the C code , you will see that I use the data eerom to hold a cpu id. Then you could turn off the power and the cpu will remember which Id it has.

In my demo example, In python, I show you how to change the cpu id.

Code: Select all

#unlock Module Id
SetPwm(0,5,0)
#set Module Id to 2
SetPwm(0,5,2)
Then it is just a question of changing the cpu id by connecting first, one cpu at the time and write different Id on each. And after that connect them all together.

The pwm command in my code is
SetPwm( Module, Channel , value).

Module is cpu Id (1.. to 15 ) 0 is for all
Channel is the servo (0.. to 3) 4 servos or 4 pwm output.

P.S. Module value 0 is a special command to send to all modules. Don't set module Id to 0.

Daniel

pawellot
Posts: 9
Joined: Fri Jun 07, 2013 6:34 pm
Location: London

Re: Add small cpu for R/C servo controlled

Fri Jun 21, 2013 4:26 pm

Hi Guys!
I know... I am n00b. but what is the "ICSP" from from the schematic?

all best
Pawel

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9599
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Add small cpu for R/C servo controlled

Fri Jun 21, 2013 5:21 pm

most often ICSP means "In Circuit Serial Programming", http://en.wikipedia.org/wiki/In-circuit ... rogramming but as I didn't see ICSP in the schematic I'm not sure which Schematic you meant. Most AVR's (arduino microcontrollers) are programmed using ICSP.

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Fri Jun 21, 2013 5:27 pm

Hi Mahjongg,

Pawellot is talking about my cpu for 4 servos. I did put the ICSP connector there.

And you are correct about the ICSP definition.
Handy if you have a pickit device ;-)


Daniel

6677
Posts: 382
Joined: Wed Mar 14, 2012 9:23 pm

Re: Add small cpu for R/C servo controlled

Fri Jun 21, 2013 7:19 pm

I personally just use the RPIO library for C and python. Excellent lib, uses DMA for far more accurate PWM on just about any pin. Works fine for me panning a servo and simultaneously changing the speeds of 2 motors without any jitter.


Your method is also great though, at the very least it frees up a few IO pins.

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Fri Jun 21, 2013 8:45 pm

Well the problem using the Rpi for pwm is the number of pins available. I do have a robot car with a webcam and I need the Gpio pins to drive the motors, i2c for the compass and the accelerometers and some extra pins for spraying water or turn light on.

This is a very good option to free gpio pins. Also according to some previous post there is a lost of cpu process to control the pwm, up to 10% I believe.


Later I want to add a robotic arm on it. Since I will just add an other cpu on the same pin , It won't be a problem.

Maybe I'm the only one thinking that way. I didn't have any feedback.

B.T.W. I bought some pic16F1827 and PIC16F1823 and I was able to burn them with the raspberry software that I post.

I could program the pic16f1827 for 12 servos or leds.

I'm building a led bar like a cylon scanner (Battlestar Gallactica). It will be 16 leds in PWM mode . I will use pwm for smooth transistion between leds.
This without using additional gpio to the current system.


This is just to say that it is an alternative to free gpio pins or have more pwm than the Raspberry could offer.

Daniel

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9599
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Add small cpu for R/C servo controlled

Fri Jun 21, 2013 10:11 pm

danjperron wrote:Hi Mahjongg,

Pawellot is talking about my cpu for 4 servos. I did put the ICSP connector there.

And you are correct about the ICSP definition.
Handy if you have a pickit device ;-)


Daniel
maybe I'm blind or something ;) but I don't see it in the schematic, assuming you are talking about this one:
https://dl.dropboxusercontent.com/u/488 ... ot_sch.png

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Fri Jun 21, 2013 11:44 pm

Yes this my in house one.

It is using a pololu servo that I purchase half a decade ago. It does the same thing but way more expensive. This is my test jigg and i will try the led bar on it first. I just got the compass and gyro working yesterday night and I will work on the webiopi web page to incorporate the readings.

My plant for this winter will be to continue working on my robot garden. Replacing the pc-104 computer by the Rpi and implement all the above plus some kind of positioning and recharging plateform.

This old robot used drill motors with sabertooth driver. Very powerfull. A water pump,( windshield washer) , is already there.

If you found my schematic , I'm sure you saw my old robot video.

Daniel

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Fri Jun 21, 2013 11:54 pm

The pololu servo driver does the same thing. But the one I got had a small problem. It is auto baud! Every times you power up the Rpi, it will lock, because it can't figure out the baud rate.

This is why I added an extra gpio to reset the pololu after the rpi fully power. Because The Rpi send the 0xff code on power up.


My 4 servos cpu use fix baud! No problem.

Daniel
Last edited by danjperron on Sat Jun 22, 2013 1:20 am, edited 2 times in total.

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Sat Jun 22, 2013 12:44 am

Hi Mahjongg,

for the correct schema check at the top of this forum. The first post.

http://www.raspberrypi.org/phpBB3/viewt ... 95#p335295

Daniel

Todd Alan
Posts: 6
Joined: Sat Jun 22, 2013 12:59 am

Re: Add small cpu for R/C servo controlled

Sat Jun 22, 2013 5:50 am

Daniel,
Can you give some more detail on the pic programer. I've tried to run it on my pi and end up with a syntax error on line 97 "invalid character in identifier" I'm kind of a newby to the pi but learning.

Todd

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Sat Jun 22, 2013 5:06 pm

Hi Todd,

looks like that python gpio modules is not installed. Are you running Raspian?
code.google.com/p/raspberry-gpio-python/wiki/Examples

python stop at the declaration gpio.

Daniel

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9599
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Add small cpu for R/C servo controlled

Sun Jun 23, 2013 1:26 pm

danjperron wrote:The pololu servo driver does the same thing. But the one I got had a small problem. It is auto baud! Every times you power up the Rpi, it will lock, because it can't figure out the baud rate.

This is why I added an extra gpio to reset the pololu after the rpi fully power. Because The Rpi send the 0xff code on power up.


My 4 servos cpu use fix baud! No problem.

Daniel
ah, thanks, as usual i was looking too far (for a link to a schematic) and overlooking what was right in front of me. :oops:

danjperron
Posts: 2667
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Add small cpu for R/C servo controlled

Sun Jun 23, 2013 1:40 pm

Hi Todd,

To Find if you have the missing module in python , you could check the list of all the modules

Start Python

Code: Select all

python
help('modules')
This will give you the list of all the modules installed. Check for RPi . It is in the beginning since it start with an upper case letter. If it is not there you will need to install it.

Code: Select all

$sudo apt-get update
$sudo apt-get install python-dev
$sudo apt-get install python-rpi.gpio
P.S. use "quit()" to exit python.

good luck,

Daniel

Todd Alan
Posts: 6
Joined: Sat Jun 22, 2013 12:59 am

Re: Add small cpu for R/C servo controlled

Mon Jun 24, 2013 5:02 am

RPi is listed as an available module.
This is the error:
pi@raspberrypi ~ $ python burnlvp.py
File "burnlvp.py", line 97
SyntaxError: Non-ASCII character '\xc2' in file burnlvp.py on line 97,
but no encoding declared; see http://www.python.org/peps/pep-0263.html for details.

Thanks for your help my friend

T

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 20 guests