rjhazeld
Posts: 38
Joined: Mon Feb 16, 2015 9:19 am

Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 10:42 am

I have a tracked cambot with two motors each of which has a hall effect sensor with two signal outputs. The signal outputs are connected to a ModMyPi MCP23017 HAT with i2c address 0x20. Initially I want to get a rev count from each motor and its direction and later be able to set accurate movement patterns.
The example rotary encoder code on the PIGPIO site looks a good start point if the connections were direct to the PI (v3). I can see that PIGPIO has i2c capabilities but I can't work out how I can 'define' the pins in order fro the rotary encoder code to work with the connections to the MCP23017 pins.
It seems that the MCP23017 pins should operate in the same way as the Pi GPIOs once set up. Has anyone found some 'interface' code that might help me or an alternative approach that might be easier for a novice?

User avatar
DougieLawson
Posts: 36123
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 10:47 am

Reading MCP23017s can be done with a dtoverlay (which does map the MCP23017 like a GPIO pin), but it's horribly slow.

Doing it with python smbus is trivial.

http://www.raspberrypi-spy.co.uk/2013/0 ... pi-part-1/
http://www.raspberrypi-spy.co.uk/2013/0 ... pi-part-2/
http://www.raspberrypi-spy.co.uk/2013/0 ... pi-part-3/
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

rjhazeld
Posts: 38
Joined: Mon Feb 16, 2015 9:19 am

Re: Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 12:06 pm

Thanks for the quick reply. my reading did highlight the speed issue and was what took me in the direction of PIGPIO which seems to have i2c capability and is fast. The motors are said to run at up to 11,500 rpm
The smbus route looks straightforward in the context of switching LEDs but I do not know how I would set up interrupt driven callbacks, which seem to be necessary to read direction and rpm.

User avatar
DougieLawson
Posts: 36123
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 12:20 pm

You may need to switch to using C/C++ (or your favourite compiled/assembled language probably with Gordon Henderson's WiringPi). But prototyping your code in python makes lots of sense to get the basics right even if python later fails the speed test.

I've never tried reading an MCP23017 at high speed, most of the stuff I've done with them has been using them to switch things like relays and LED matrices.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

rjhazeld
Posts: 38
Joined: Mon Feb 16, 2015 9:19 am

Re: Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 3:26 pm

I think PIGPIO is in C and is reputedly fast hence looking to it for a solution. As far as I can tell wiringpi's Python wrapper doesn't work with interrupts..

User avatar
DougieLawson
Posts: 36123
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 3:58 pm

rjhazeld wrote:I think PIGPIO is in C and is reputedly fast hence looking to it for a solution. As far as I can tell wiringpi's Python wrapper doesn't work with interrupts..
WiringPi's python wrapper is an unsupported user addition.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
mikronauts
Posts: 2722
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 5:06 pm

I really doubt you can do that over I2C, especially if you get multiple pulses per revolution.

As I recall, Pi's I2C defaults to 100khz. Each I2C packet for reading a register takes ~30 i2c bit periods at a minimum.

For this speed, use the Pi's native pins, in C/C++ with interrupts, or an external microcontroller.
rjhazeld wrote:Thanks for the quick reply. my reading did highlight the speed issue and was what took me in the direction of PIGPIO which seems to have i2c capability and is fast. The motors are said to run at up to 11,500 rpm
The smbus route looks straightforward in the context of switching LEDs but I do not know how I would set up interrupt driven callbacks, which seem to be necessary to read direction and rpm.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

ghp
Posts: 1409
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 5:20 pm

Hello,
alternative solutions are
- connect the hall sensors direct to gpio pins, but with python this will be difficult to achieve (2 motors, each prox 200Hz).
- use an external atmel328 coprocessor, e.g. a nano board. For a single encoder I measured up to 50.000Hz.
See a performance analysis in http://heppg.de/ikg/wordpress/?p=900
Hope this helps,
Gerhard

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

Re: Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 5:38 pm

You would have more options if you connect direct to the Pi GPIO rather than via the MCP23017.

All the Python GPIO libraries should be able to cope with circa 400 interrupts per second.

rjhazeld
Posts: 38
Joined: Mon Feb 16, 2015 9:19 am

Re: Hall motor Sensors MCP23017 and PIGPIO

Fri Apr 07, 2017 9:56 pm

Thanks for the helpful comments.
The mcp23017 provides pins that are much more accessible than those on the PI itself and a degree of protection for the PI - having managed to fry one of the i2c pins on a previous pi. I'll look to redesign the packaging to allow access to the pi pins themselves and consign the mcp23017 to controlling leds.

Return to “Python”