Page 1 of 1

Pulse counting problem

Posted: Wed Jan 01, 2014 5:54 pm
by ossir
Hello

I'm makin device for my dirtbike which measure time, speed and trip. Speed and time works fine, but i do not know how to measure trip correctly. Now the trip increases too fast compared to speed, about 1km in 10-20s if speed is about 60 km/h. Here is my program attached, so can somebody help me please.

I know that the speed resolution is not very good, but i know how to fix it.

My dirtbike wheel circumference is 2.1m


Second thing what in do not understand is, why i have to use negative value to get positive results?

example:
freq_float = fsub(freq_float,-1)

BR
Ossi

Re: Pulse counting problem

Posted: Fri Jan 03, 2014 2:09 am
by AndrewC
I'll try and help but I'm not really sure what the set up is and how your program works. And I'm the one usually scratching my head :)

I guess the input is coming from some sort of switch on the final drive?
The way I would do it is something like this:

Count=Count+1
Trip=Count*(0.0021/Counts per wheel revolution)

If I have got that right, it should give you the distance in Km's.
Second thing what in do not understand is, why i have to use negative value to get positive results?

example:
freq_float = fsub(freq_float,-1)
Could it be because you are subtracting a negative. Do you get the same result with freq_float = fadd(freq_float,1)

Re: Pulse counting problem

Posted: Sat Jan 04, 2014 1:12 pm
by ossir
I try to explain beter how the program works (in my opininion):

- Program is based : Flowcode V4 examples --> 5. Digital frequency counter
- It uses TMR0 interrupt (16 interrupts = 1s)

- Macro "tulo" = input pulses from wheel
- Macro "kello_laskutoimitus" = Timer calculations seconds, minutes and hours.
- Macro "LCD_tulostus" = LCD printing
- tmr_service = interrupt
- main = main program


Trip calculation is bellow the trip comment in main program:

float_matka = fmul(freq_float2,0.0021) // 0.0021km = 2.1m = which is dirtbike wheel circumference
float_matka_total = fsub(float_matka_total,float_matka)


freq_float2 = freq_float2 +1 // rising variable freq_float2 +1 after the 1 second is passed the freq_float2 get value 0 and it starts all over again.


In Flowcode simulation it seems to work, but in my circuit it kind of multiplies the trip value depending wheel speed/frequency.
If you press play button in simulation and use (pulsing) that input switch(0), you can see that the trip value increasing. I dont understand why it is not work correctly in my circuit :?:


-----------------------------------
Second problem

If i use negative value:
freq_float = fsub(freq_float,-1)

i get positive results 1, 2, 3...

If i use postive value
freq_float = fsub(freq_float,1)

i get negative results -1, -2, -3...


I hope you understand my explanations, beacause my english skils are not very good.

Here is attachment that "Digital frequency counter"

Re: Pulse counting problem

Posted: Sat Jan 04, 2014 6:23 pm
by medelec35
Hi Ossi,
On your hardware What value crystal are you using for you clock (have you got a part number for it)?
Have you considered INT0 rather than a tight loop for detecting wheel speed?

Martin

Re: Pulse counting problem

Posted: Sat Jan 04, 2014 8:00 pm
by ossir
medelec35 wrote:Hi Ossi,
On your hardware What value crystal are you using for you clock (have you got a part number for it)?
Have you considered INT0 rather than a tight loop for detecting wheel speed?

Martin
Hello

I use 4194304 Hz crystal and PIC16f887. These interrupts in program are new thing for me, so i dont know how to use INT0 in my case.

Re: Pulse counting problem

Posted: Sat Jan 04, 2014 11:29 pm
by medelec35
Hi Ossi, problem with counting over 1 second periods and 2.1m wheel circumference is if there are 10 det per sec then Kmh = 2.1*10*3.6 = 75.6
Yet if only one less detection per second then speed = 68.04 Kmh so the resolution is not that great.

So as an experiment, I have tried to compensate by using 5 seconds of detection time instead of 1 second.
The disadvantage id speed is updated every 5 seconds of course.

I have also used INT0 interrupr, so that does require a change of hardware.
i.e LCD = PortC and freq detection i on pin B0

This may or may not work?

If detection device is normally high, and when wheel is triggering detection sensor it goes low, then you will need to change RB0INT interrupt edge detection (within int properties) from rising edge to falling edge

Due ot low resolution and to make more efficient, I have removed all float variables.

Martin

Re: Pulse counting problem

Posted: Sun Jan 05, 2014 12:48 pm
by medelec35
Just had a thought on two things and wondering it's worth proceeding?

1) For better accuracy of updating every second, it it possible to have more than one triggering magnet (assuming hall effect device)?
The more magnets the accurate it would be.

Or

Rather than count revs per second or even revs per 5 seconds, how about using the Speed = distance over time?

We know the distance = 2.1 mtrs, so we can using an interrupt timer to time between pulses.
Once we have the time, then speed = 2.1/time taken between pulses.

Re: Pulse counting problem

Posted: Sun Jan 05, 2014 2:44 pm
by ossir
Hi

My main problem is tripmeter/odometer, not the speedometer. Like i sayd in my first post, I know that the speedometer resolution is not so good and like you said Martin in previously post, i propably add more magnets to get beter resolution.

Specs/functions for my meter:

- Speedometer, resolution 1km/h, max value about 100km/h
- Odometer/tripmeter , resolution 0.1km, max value 9999.9km , can set to zero
- Hourmeter, shows seconds, minutes and hours, max value hours 3 digits, can set to zero
- Saves data (hours, minutes, seconds and odo) in eeprom when engine is off
- Meter starts when engine is on


So can i done this all with based my program version "Laskuri" or Martin version "laskuri_V2" if can not, can somebody tell me beter solution. I'm also very glad if somebody tell why my "odometer" in my first post not working correctly.

Hopefully you understand what i mean.

Ossi

Re: Pulse counting problem

Posted: Sun Jan 05, 2014 4:08 pm
by medelec35
Hi Ossi,
I understand what your saying but I was working on the principle of getting the speed more accurate before working on Odometer.

Bad resolution Speedometer = way out Odometer multiplied several times.

If you can test Speedometer and it's reasonably accurate, then I can progress with the Odometer/tripmeter.

No use working on the latter if the former is not that good.

Also if circumference is 2.1 meters and you have an accurate speedometer, then if I have worked it out correctly
After 47 lots of 2.1meters = 98.7 mtrs = 0.0987 Km
or after 48 lots of 2.1meters = 100.8 mtrs = 0.1008 Km
That's about as accurate as you can get.

I can implement that , then post update flowchart.
Would that be helpful?

Martin

Re: Pulse counting problem

Posted: Sun Jan 05, 2014 5:29 pm
by ossir
Hello Martin

Program works in Flowcode but not in my circuit. I change LCD port to B and button back to D0 , but LCD prints only 1x 16 beams/bars. For me good resoluotion is 1km/h(speedometer) and 0.1km in odometer. I try to figure out tomorrow, why program is not working in my circuit.

That would be nice. If you could add some explanation comments in program for me.

Thanks

Re: Pulse counting problem

Posted: Sun Jan 05, 2014 9:54 pm
by medelec35
Hi ossir,
This is using B0int (interrupt) for 1rev detection.

Will not work with sensor connected to D0

LCD must be on portC

Port b does have weak pull-ups so I have enabled them.
This means that a pull-up resistor is not required.
I have modified flowchart to work with pull-ups.
The sensor has to be connected between 0V and B0
the trip reset switch has to be connected between 0V and B1

The Flowchart is a bit complex as it was more difficult than I first thought to get a reading up to 9999.9 without using any floats as they are not recommend with 8bit pics.
Was beginning to think I have bitten off more than I can chew.

If it does work then IU can add more comments to flowchart.
Just did not have enough time tonight.

Note simulator is not in real time so you will notice seconds incrementing by 2
Should be ok on hardware?

If it does not work then can you post a circuit diagram of your full set up.

Martin

Re: Pulse counting problem

Posted: Mon Jan 06, 2014 1:52 pm
by ossir
Hi Martin

Now it seems almost working, there is couple of bugs what i notice:

- Trip reset button not working
- when input frequency is about 13.23Hz = 100km/h, speedometer shows 101.3km/h or 997.9km/h, correct value maybe 97.9 km/h

Other questions:

- Is there some way to measure how long 1 second in circuit actually is with oscilloscope, or shorter time which is proportional to 1second? Now i have to compare it to my pc clock, which needs time before i can see error with my eye. In this device it's not super important to have 1.00000000000s accuracy, but it's nice to now if it can measure.

- What about temperature, how it will affect timer operation for example -20°C compared to +20°C ?

- Now resolution of speedometer is about 1.5km/h and 5 second update time. I think i use 3 magnets and 2 second update time, so then resolution is about 1.26 km/h if i'm right?


Ossi

Re: Pulse counting problem

Posted: Mon Jan 06, 2014 2:23 pm
by medelec35
Hi Ossi
ossir wrote:Now it seems almost working,
Do you think it's better than you had before and is worth pursuing with the way flowchart is now done?

ossir wrote:there is couple of bugs
Was expecting a few but as I did not have correct hardware and was making it up as I
go along.
All untested.
ossir wrote:Trip reset button not working
It looks like I have selected to read all of port B instead of B1 on the input icon.
If you change that, trip reset should work.
Dont forget line is pulled to 0 to reset trip.
ossir wrote:when input frequency is about 13.23Hz = 100km/h, speedometer shows 101.3km/h or 997.9km/h, correct value maybe 97.9 km/h
I will look into that tonight.
Wonder if its better to round to a whole number and drop the d.p?
ossir wrote:- What about temperature, how it will affect timer operation for example -20°C compared to +20°C
I would guess the accuracy at the two temperatures would be dependant on the crystal accuracy. for that you would need to look at the crystal data sheet.
ossir wrote:- Is there some way to measure how long 1 second in circuit actually is with oscilloscope, or shorter time which is proportional to 1second? Now i have to compare it to my pc clock, which needs time before i can see error with my eye. In this device it's not super important to have 1.00000000000s accuracy, but it's nice to now if it can measure.
What about placing an output at the 1sec part with a toggle calculation.
e,g.
calculation box: Toggle = ! Toggle

Output: Toggle>portb2
Then place scope on portb2. pulse length should be 1 sec. freq= 0.5Hz

Martin

Not got any more time to work out accuracy as i'm currently at work. beside not an expert on that side of things.

Re: Pulse counting problem

Posted: Mon Jan 06, 2014 8:14 pm
by ossir
Hello

Now reset button works.

If you have time, can you add comments especially in "after one second (yes) " in main program and also in LCD printing section.

Thanks

Re: Pulse counting problem

Posted: Tue Jan 07, 2014 12:56 am
by medelec35
I have made some changes and hoping Its right.
Tested on a simulator only.
Set simulator to pulse low approx every 75ms and speed is showing as 99kmh which I believe is about Correct:
Simulation.png
(24.95 KiB) Downloaded 7103 times
I have calculated speed deriving from pulse from:
Speed = (1/pulse_Duration)*2.1*3.6
= Freq *2.1*3.6
13.3*7.56
= 100

Not sure how accurate the 70ms pulse is, but it does not seem that far out from expected results.
Greatly increased simulation speed so 1 hour takes a few minutes, and here are the results:
Simulation2.png
(22.48 KiB) Downloaded 7103 times
Results are as near as expected. I know it should read 99 but it's only a simulation.

Martin

Re: Pulse counting problem

Posted: Tue Jan 07, 2014 8:43 pm
by ossir
Hello Martin

It seems to work similarly like in your simulation, so i'm happy with that.
Now i need to make some final adjustments and finalize the program.

Thanks a lot for your help Martin

Re: Pulse counting problem

Posted: Wed Jan 08, 2014 12:11 am
by medelec35
That's good.
Thanks for letting us know.

Martin