VaguelyAmused Posted February 19, 2015 Share Posted February 19, 2015 All, First post, so may I say hi and introduce myself I've long been into electronics and good old DIY dabbling, took a course in "Applied Computing and Electronics" at Bournemouth Uni in the UK which was a bit of everything from VHDL, circuit layout/design and 8051 programming to some C++, Java and C#. Since then I've gone from creative fun programmer to boring paper pusher manager in the hunt for something to label a career. Along came the little Uno32 and the Motor Shield for some of that home hobby messing about I've so missed - boy is it good to be doing it again! :-D So some more background - many years ago my dad and I effectively retrofitted a highly accurate drive system to a telescope mount for astro-photography, this had a DC servo motor drive unit at the heart and back then (12 years now) that little magic box cost well into the triple figures. Before the days of my training, the insides of that box were simply black magic. Fast forward to now and just for fun I wanted to see what a sub £50 bit of kit, a la the Uno32 and some rusty knowledge could do to imitate that once mysterious and extortionately priced system. Well, its not exactly stellar, but I'm quite chuffed with what I've done so far. Lots of googling, lots of libraries downloaded and fiddled with and some slightly oddball approaches to the motor control and I have something that, despite the rambling, may be of interest to others. Basic Description: Uses the ChipKit Uno32, the Motor Shield, a 9V 3A supply for the shield and a Maxon 12V 3.8:1 gearmotor with 512CPT encoder (£25 off eBay). Just the one motor so far, though 2 should be simply a few extra lines in the sketch. Libraries that have been used include the SoftPWMServo, modified to run at 30KHz (20KHz still made some audible wine) and accept a range of 0-65535 (finer control, in theory). Checked the output on a scope at work and it looks pretty darn accurate for a software 30KHz PWM - I'm impressed :-) Not clear if I am allowed to post links here, but a google search will take you straight to the library. Another library was the Arduino PID Library, modified to match changes to SoftPWMServo accepting a range from 0-65535. A bit of code was stolen from one of the MPIDE examples to assist with serial data parsing so a few commands can be sent over the serial port to set either position or velocity. That came from the BasicSerialParser example in Communications. Motor Control: Control is somewhat odd, my own thinking but I completely expect by no means original! I drive the H Bridge completely backwards. By this I mean I keep the enable pin high, and send the PWM through the direction pin. This is probably considered completely mental, but it has a few interesting benefits (IMHO). Before PID, I was seeing how slow the motor would turn before stalling using just open loop PWM. This is of interest (to me) because having a large dynamic range is useful for telescopes - sloooow tracking but speedy slewing to an object. Driving the H Bridge in this alternate way definitely allowed me to drive it slower by about 3 fold compared to driving the H Bridge normally. Similarly, holding torque is improved since in 0 position the motor is not actually off, but oscillating constantly. The motor does obviously get hot though since it is constantly being driven at max duty cycle just in different directions all the time! No magic smoke released, so it seems the H Bridge copes with it OK - I'll let you know if it changes! Having said all that, the motor wasn't exactly great on torque (its only 22mm as it is and under driven at 9V for a 12V motor). The "steady state" PWM value for the motor is 32767 (effectively 50/50 duty cycle in forward/reverse resulting in 0 angular velocity), 0 sets max reverse speed and 65535 sets max forward speed. Now with PID thrown in the mix that forward/reverse control of the oddball H Bridge driving makes things really easy. Oh, PID is just brilliant by the way. Hopefully the sketch attached shows how simple it is to throw a PID algorithm in there, and it ends up allowing for some really cool control. Position (not worked out how to allow for going negative yet) is between 0 and _alot_. It is just a case of setting a "setpoint" of encoder pulses as the desired position, throwing in the current encoder pulses when you sample and letting it the PID do its thing. Its crazy, holding torque at any position is stupid - the thing feels alive and fights with you. The more you try and turn it, the more you are deviating it from the setpoint and so the more the PID injects as an offset to fight with you, really is quite cool. I've seen loads on how to do velocity online and people seemed to find it difficult, so I've probably done this completely wrong as all I do is keep count of the last encoder pulse sample value and sample time and then when you next sample you can work out how many pulses you are doing in a specified time frame. You use this and the desired value for the PID and it then ensures the speed is kept. The encoder counter is using an interrupt timer and set within the sketch. Pretty straight forward logic but I think it might be slightly floored, or I am missing counts or something as I can't quite yet get the thing to 0 at the same point repeatably. At fully chat its doing about 170KHz encoder pulses though so not bad going. The default PID values I'm using (20,400,0.2) are interesting but work well. The velocity goes down to about 20RPM smoothly, 10RPM it does but not happily - 4000RPM is the max it will do at 9V and it appears the encoder counting is keeping up (I had a poor(er) implementation before and when it started missing counts behavior went haywire). Pretty good dynamic range I think. Dialing in a position is great - snaps to it with no hesitation although need to work out why the lack of really precise repeatability. Next things to do are to try and work out how to do a position+velocity move which is proving a head scratcher. Then I think its time to tidy it up and put it into something a bit more manageable and start thinking about wrapping it with some stuff to make it useful for actual telescope mount control. Anyway, I must say apologies for such a rambling first post. It kind of befits my enthusiasm for this little thing, so nice to be back in the thick of a bit of hardware with wires hanging out of it and led's flashing away and the like. If anyone is genuinely interested in progress with this let me know and I'll keep this updated. PS: The sketch probably won't build! It requires the libraries mentioned above, but I did modify them a bit so some of the variables will have type changes. I could package the whole lot up as a zip but I honestly don't know where I'd stand with GPL licenses and the like so any problems shout and I can explain what I did. Hopefully there are some useful scraps in there for others to use if nothing else :-) ... UPDATE: I am not permitted to upload pde files, which is fine (not grumbling - just noticed the error when about to hit submit). I'll still post this so if anyone is interested for the sketch or the encoder counting code or whatnot let me know I guess I am allowed to post code snippets of my own in posts? Thanks all! Chris Link to comment Share on other sites More sharing options...
This topic is now archived and is closed to further replies.