User avatar
ralph
Posts: 29
Joined: Mon Aug 27, 2012 9:25 am

Cheaper alternative to brickpi? (Connecting NXT servomotors)

Thu May 02, 2019 9:17 pm

I have a couple of raspberry pi zero W powered robots. The robots are mostly made out of lego technic and use "power functions" motors.
I'm now trying to close the loop and get feedback from my motors for stall detection and driving in a straight line better. The best off the shelf solution for this is to swap my power functions motors for nxt motors and use the brickpi to read the encoders on the nxt motors.
The brickpi is nearly £100 though! That's roughly the total cost of every other component in the robot added together!
Anyone know of an cheaper alternative to a brickpi or will I have to spend months designing an nxt servomotor controller?
Last edited by ralph on Sun May 05, 2019 8:08 pm, edited 1 time in total.

mattyyey
Posts: 35
Joined: Sat Jan 11, 2014 11:44 am

Re: Cheaper alternative to brickpi?

Fri May 03, 2019 7:22 pm

I was recently looking into this, it is possible USB or Bluetooth or Wi-Fi control the ev3 brick and the NXT brick. Does mean the motors and sensors go into the NXT as normal, RPi is just a remote control...

www.ev3dev.org
http://www.raspberry-pi-geek.com/Archiv ... ego-and-Pi

I cannot find the link, but the full wiring diagrams are available from Lego and related sites... They run on higher than 5 volts, (24v iirc) so you cannot switch them directly with a rpi. But it would be possible to create a circuit that could connect to your raspberry pi.

I'm still researching this myself but there are many options other than the brickpi they just require more effort and more research...

User avatar
ralph
Posts: 29
Joined: Mon Aug 27, 2012 9:25 am

Re: Cheaper alternative to brickpi?

Sat May 04, 2019 4:22 pm

Thanks for those links, I'd been researching but I hadn't seen them before. The more I look into this, the more it looks like I'll be making my own motor driver :-(

Best brickpi alternative I've found so far is:
https://store.wayneandlayne.com/product ... river.html
Unfortunately it only works with 5V arduinos, there's no UK distributor, and it's currently out of stock!

I can find motor driver modules that can take the voltage/current requirements no problem, but I'm still looking for ways to handle the motor encoders.
Last edited by ralph on Sun May 05, 2019 4:07 pm, edited 1 time in total.

User avatar
ralph
Posts: 29
Joined: Mon Aug 27, 2012 9:25 am

Re: Cheaper alternative to brickpi?

Sun May 05, 2019 3:49 pm

So these are the motors I bought. They're standard Lego Technic NXT servomotors with integral optical encoders. They're about £7 each on bricklink.
Image

Motor specs:
Voltage: 9V
No-load speed: 170 RPM
Weight: 80g
No-load current: 60mA
Stall current: 2A
Stall torque: 50N.cm
Integral over-voltage protection: 15V
Integral over-current protection: PTC self-resettable fuse trips at 600mA
Integral gearbox: 1:48 reduction
Integral encoder: Quadrature optical encoder. I've just tested it and the encoder works on both 3.3V and 5V :-)


Here's the motor pinout:
Image
Pin 1 - white - motor terminal 1
Pin 2 - black - motor terminal 2
Pin 3 - red - encoder 0V (gnd)
Pin 4 - green - encoder Vcc (3.3V - 5V)
Pin 5 - yellow - encoder output A
Pin 6 - blue - encoder output B

I've crimped standard breadboard "dupont" connectors onto the wires so I can power it with anything. Here's my test setup with a spare pi zero W providing the 3.3V to the encoder:
Image

And here's the oscilloscope trace of the encoder output while the motor is running with no load:
Image

I'm looking at driving these with either a DRV8835 or a TB6612FNG module. Both of these are dual motor drivers with a full H bridge so they can reverse the motor as well as doing PWM speed control. They're also very cheap from eBay\China.

For feedback I'll probably connect the encoders to the Pi IO pins and use pigpio to count the pulses.

User avatar
ralph
Posts: 29
Joined: Mon Aug 27, 2012 9:25 am

Re: Cheaper alternative to brickpi?

Sun May 05, 2019 4:05 pm

Also, if anyone else wants to make the same motor driver as me, I've just ordered this motor driver:
https://www.amazon.co.uk/dp/B06ZZRKHCX/ ... 11_TE_item

And to power the motor from the same supply as the pi (a USB battery) I've ordered this DC-DC converter:
https://www.amazon.co.uk/dp/B07B4S3M3D/ ... 11_TE_item
(Simply plug a micro USB cable into it and it makes the 9V motor supply voltage)

So far I've spent £3.30, so less than the price of a pint, and much less than the nearly £100 cost of a brickpi!

User avatar
ralph
Posts: 29
Joined: Mon Aug 27, 2012 9:25 am

Re: Cheaper alternative to brickpi?

Sun May 05, 2019 8:07 pm

Okay so I had a rummage in my electronics box and found an unused DRV8833 module to test with. I now have the Pi powering the NXT servomotor with PWM in open loop mode.

Here's a pic of the dead simple setup:
Image

I've also connected the nxt quadrature encoder to the pi via a pair of 10kOhm series resistors and checked the waveforms with piscope:
Image

I can see the 90 degree phase difference perfectly :-) The third waveform on the bottom is the PWM output to the motor driver, currently set at about 25% duty cycle.

mattyyey
Posts: 35
Joined: Sat Jan 11, 2014 11:44 am

Re: Cheaper alternative to brickpi? (Connecting NXT servomotors)

Mon May 06, 2019 9:23 am

That's some great information thank you. When I get a free evening out give it a go

User avatar
ralph
Posts: 29
Joined: Mon Aug 27, 2012 9:25 am

Re: Cheaper alternative to brickpi? (Connecting NXT servomotors)

Tue May 07, 2019 10:45 pm

Okay, so I've got the PWM working from software on the pi, and I've got the same software reading the exact speed from the optical encoder. I've also added PID control but it needs tuning and I can't remember how to do that right now. Here's a graph of the motor response to a step input at t=0.5s
Image

Currently Kp=0.5, Ki=0.01, Kd=0.01 and I'm running my control loop every 30ms. Anybody got any good tips to get PID control working or should I start a new thread for that?

User avatar
Gavinmc42
Posts: 3180
Joined: Wed Aug 28, 2013 3:31 am

Re: Cheaper alternative to brickpi? (Connecting NXT servomotors)

Wed May 08, 2019 12:35 am

I saw these and thought why has Lego themselves not done this?
https://www.seeedstudio.com/Kittenbot-L ... -2950.html

Why do people step up the voltage to power motors?
If they use Li-ion power banks they are stepping up twice :o
Why not start with 9+v supply for the motors and step down to 5v for the cpu?
That way is more efficient and gives longer battery life.

One of the things I am looking at is a Zero in robot and a Microbit as the BT RC.
In theory a BT app could be made for Android too.

The encoders are a bit of a pain if you want to use a Pi to read them direct.
Might be better to add a Arduino type chip/pcb to handle the encoders and PWM.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

User avatar
ralph
Posts: 29
Joined: Mon Aug 27, 2012 9:25 am

Re: Cheaper alternative to brickpi? (Connecting NXT servomotors)

Thu May 09, 2019 1:50 pm

Lego have never been interested in making their motors, servos and sensors compatible with standard ones. And as for stepping up voltage for motors, I can't speak for others, but for me it's mainly due to time, simplicity and budget. With a cheap off the shelf USB battery I get a charging circuit, regulated output, lithium ion batteries, safeties to ensure no overcharging etc all in a convenient case. I'm also more interested in the 5V being supplied efficiently as the pi is on all the time whereas the 9V motors are only on occasionally.

Anyway, back on topic I've been trying to get my PID controller working for the NXT servomotor. I've noticed the speed control is quite unstable at lower speeds but works fine at higher speeds so I made a program to slowly ramp down the PWM duty cycle and record the average motor RPM at each point. This gave me the following graph:
Image
Note the massive non-linearity of the graph, probably explains why the controller struggles!

pootle
Posts: 320
Joined: Wed Sep 04, 2013 10:20 am
Location: Staffordshire
Contact: Website

Re: Cheaper alternative to brickpi? (Connecting NXT servomotors)

Fri May 10, 2019 8:29 am

I like this idea, as I have a couple of these motors. I developed techniques to drive (different) dc motors with optical encoders. A couple of things that might help:

I wrote code using pigpio to process the feedback from the optical encoder. Initially enitrly in python, but I recoded a critical part into C to provide greatly improved performance.The long story is here.

The short version is that the C optimised version (which is here on github) copes easily with up to 200,000 edges per second and enabled pid style feedback control of 2 motors on a pi Zero. There is some detail of the code handling the encoder output on this page.

I had problems with the pid feedback because the motor's response to pwm percentage is so non-linear (as you show in the recent post with the graph). I found it helped a lot to use a mapper that made the motor's speed appear to much much more linear.

Given that the encoders can return pi compatible signals these motors look like that they'll be nice to use direct from a pi.

User avatar
ralph
Posts: 29
Joined: Mon Aug 27, 2012 9:25 am

Re: Cheaper alternative to brickpi? (Connecting NXT servomotors)

Mon May 13, 2019 9:59 am

Thanks for all the information @pootle ! When I get a minute I'll have a long look at that code of yours to see how you've done it. Unfortunately my python and C skills are lacking, I usually code in Java but I should be able to get the gist from reading it. I see you've put your code on github so I've done the same, it's now available at https://github.com/ralphhughes/nxt_servomotor_driver Code is at a very early stage so far but I'll get there :-)

Return to “Automation, sensing and robotics”