Help with Code for Hall-Effect Rotation Sensor
I'm working on a controller (SX28) for an electric bike application. The controller will eventually have three inputs (pot value; hall-effect pulse from motor; hall-effect pulse from crank-mounted geartooth sensor (digital output)) and one output (0-5v PWM signal to brushless motor controller.)
My questions are these:
Will the SX28 be fast enough to monitor and control these I/Os in real-time (more or less?) And how should I be thinking about coding this (I'm a total microcontroller newbie and would be ecstatic if I can do it in Basic)
This is what I'd like the controller to do:
* Monitor pot value (this will be the maximum throttle setting)
* Monitor crank rotation
* Monitor rear-wheel speed
* Output 0-5v via PWM based on the monitored pot value ONLY IF
crank rotation is > ValueCR (VCR) (this will be a Constant that defines pulses/unit time)
* When Crank Rotation (CR) falls below VCR, Output goes to 0v.
* When CR goes above VCR output resumes.
The rear-wheel speed data will be used at a later date for options that won't be part of this first project.
My biggest concern is over the ability of the MCU to sense when the pedals have stopped rotating and shut off the PWM output. So how do I monitor the hall-effect pulses in order to know when they've stopped while not totally interrupting the PWM output?
As I've said, I'm really new at this. So far I've gotten the MCU to read a pot value and and convert that value to a 0-255 duty PWM output. Meaning I can twist a pot and control the motor speed of the bike... now I just need to be able to interrupt that output with a rotation sensor.
Obviously I don't expect anyone to give me all the answers but some idea if this is even possible and, if so, what kinds of code I'm looking at, would be a great help and much appreciated.
My questions are these:
Will the SX28 be fast enough to monitor and control these I/Os in real-time (more or less?) And how should I be thinking about coding this (I'm a total microcontroller newbie and would be ecstatic if I can do it in Basic)
This is what I'd like the controller to do:
* Monitor pot value (this will be the maximum throttle setting)
* Monitor crank rotation
* Monitor rear-wheel speed
* Output 0-5v via PWM based on the monitored pot value ONLY IF
crank rotation is > ValueCR (VCR) (this will be a Constant that defines pulses/unit time)
* When Crank Rotation (CR) falls below VCR, Output goes to 0v.
* When CR goes above VCR output resumes.
The rear-wheel speed data will be used at a later date for options that won't be part of this first project.
My biggest concern is over the ability of the MCU to sense when the pedals have stopped rotating and shut off the PWM output. So how do I monitor the hall-effect pulses in order to know when they've stopped while not totally interrupting the PWM output?
As I've said, I'm really new at this. So far I've gotten the MCU to read a pot value and and convert that value to a 0-255 duty PWM output. Meaning I can twist a pot and control the motor speed of the bike... now I just need to be able to interrupt that output with a rotation sensor.
Obviously I don't expect anyone to give me all the answers but some idea if this is even possible and, if so, what kinds of code I'm looking at, would be a great help and much appreciated.
Comments
The SX48 would probably be easier by using the 2 hardware timers in the SX48. One could output the PWM while the other could count one of the hall sensor pulses, then the main program could count the other hall sensor and do the processing.
I would recommend the SX48 protoboard http://www.parallax.com/detail.asp?product_id=45300 ·for $9.95
Of course we're always here to help, as long as you show that you're willing to do your part.
Sounds like a cool project, keep us up to date.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I know what I know, don't confuse me with the facts...
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
Post Edited (Bean (Hitt Consulting)) : 9/22/2007 12:23:28 PM GMT
Thanks for the reply.
I figured I might have to go with the 48. Alas, I only received the 28 and techboard yesterday.
So far I've got the controller to read the pot, output the PWM and cut off the PWM if a switch is released.
The trouble comes in when I use the COUNT command. It stops the PWM until it's done counting. So the SX48 would be able to count and output PWM at the same time?
Is there a way (with the 28) to determine if pulses from the crank hall-effect sensor are coming in without using COUNT?
--Wiley
· You can set the timer for PWM output and it will keep outputing no matter what the main code is doing.
· Actually you might get away with just using one of the counters for the PWM, then have the main program read the hall sensors one at a time.
· I'd work on reading the hall sensors, you already can read the pot, and the SX48 timer can do the PWM. If you can read the hall sensors with the SX28 you have now, it will be pretty easy to put it all together on the SX48.
· You will probaby use either COUNT or PULSIN to·read the hall sensors.
· How often will the hall sensor pulse ? Do you know how long the pulses are ? Do you have an oscillocope ?
Bean.
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I know what I know, don't confuse me with the facts...
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
I don't have a scope. However, I'll be cutting the encoder gear myself (I have access to a waterjet cutter...fun) so I can determine the pulses/revolution and pulse width. I wonder.
I'm waiting to receive the hall sensors (these are the sensors: http://www.allegromicro.com/en/Products/Part_Numbers/0625/ ). My plan was to use two and do quadrature encoding to determine crank rotation and direction.
Right now I've been working on reading the halls from the motor to determine rear wheel rpm.
I think reading the sensors one at a time would be fine. I was planning on having a fairly fine gear encoder so I should be able to get enough pulses with a short count time. It's not the delay in sensor reading that's problematic, but the noticeable effect on the pwm output everytime I use the COUNT command that's troublesome as it causes the motor to pulse.
If I could get the COUNT time to be really short it probably wouldn't cause problems as the RCTIME doesn't effect PWM in a bad way.
Wiley
What about just timing two pulses to determine crank rpm? If I know the pulsewidth and how long a pulse lasts, I ought to be able to determine RPM from that, no? Rather than using COUNT and counting for a set amount of time, could I somehow look for pulses and, if pulses exist, time two pulses, then return to the main program?
Wiley
==============================
This subject is a good one. I have been working on a project for 6 months so far and it does pretty much what you are trying to achieve using only the SX28.
Here are my functional requirements:
Counting 2 Pulse Sources (Equivilent to your PulseIn Requirement - But for 2 Inputs)
PWM 2 Servos: (Equivelent to your PWM Channel 1 Requirement)
LCD Output: Display Counts (RPM For Example as a I get the 2 readings)
USB Output: Control Things from a program running on Windows Via the USB Port
Lessons Learned:
1. I found out that, I must time my events in a way that there is no jitter or objective collisions
2. I CAN USE SX28 to do all this
3. I MUST explore multi-threading to achieve all this
For example: You do not need to always read count on each (Loop Incident) You may time your pulse reads and use flags to indicate data validity
PWM Takes Precidence, since it will be controlling motion
LCD/USB Are time sensitive; therefore, they will need to be synchronized
4. I had to explore an external "Counter" - Binary Counter Part Number 74LS193. This will require (From You - 4 Ports RA/RB/RC you choose). The advantage, you will alwasy get an accurate count. Although this causes more headaches and pains of having to ammend your design. (Note, I am still struggling with my results)
5. You Definitely Need To Be Able to Measure: Frequency and Duty Cycle. If you do not have an oscilloscope, you may use CRAFTSMAN82139 From Sears for under 30Bucks. It has a (FREQ % duty) measurement option. So, this will help determine if your code is doing what it is supposed to do.
6. Note: ISR. Depending if you like Assembly Language or not, you may be able to find samples easier for ISR(To my knowledge)
Using Interrupts will give you the ability to multi-task. But is very cumbersome and prone to pain. You will need good measurement tools, an Osilloscope is one (Note: I do not currently have one, but I get results by using my Digital Multi Meter-For now). You may need to use a scratch pad: I use Excel to compare results and make adjustments in my program.
So, the good news: Is Yes You Can Use SX28 to Perform this task. Alittle bit of creativity and learning from observations. The stakes are high and you may find yourself needing to alternate your options.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bashar Sadig
Senior Solutions Architect - Enterprise Applications
Thanks for the help so far. I've got everything together on an SX48 protoboard and it seems to work well for outputting the 0-5v via pwm and reading a pot value and detecting pulses (from a button right now, soon to be a hall sensor)
I've pasted my code in below. Is there anything I can do to make this code better (there's probably a lot)? The purpose is to detect a spinning crank and, if the crank is spinning, to output a voltage via pwm based on the settings of either a pot value or buttons (see next question.)
Also, how would I go about using two buttons on the RB port to set the throttle instead of a pot? I'd prefer to ahve two buttons that increment the output of the PWM in 1/10 increments but I've been having trouble setting the buttons to trigger an interrupt.
Cheers,
Wiley
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Wiley
Trying not to let out the "magic smoke."