Part V: Coding

Before starting the program, first we must think what the task we are achieving is and how to achieve that task. The task here was to get speed of the wheel, but then again for my specific project, I wanted to average the speed of both wheels just because I felt like programming it that way. Also I was working to design a robot that is thoroughly incorporated into other parts of the project such as the speed controller or navigation. This meant I had to send data over a bus to other parts of the system. This meant when I will get speed sensing data and what will happen to old speed data vs. new speed data. Will I store it or will I dump it? Will I use the old data to influence the new data for speed smoothing to remove noise data? What type of readings do I want my data in, MPH, KMH, Hz? These are the questions I had to figure out.

This may not have an influence on the community I am writing to though. So for this program, here is what I incorporated.

  • EWMA

I added the element of speed smoothing through an algorithm that is known in statistics. It is called the exponentially-weighted moving average or EWMA for short. It takes a certain percent of the old reading and the remaining percent of the new reading. This helps create a steady reading along, but still with accurate readings. To get a better idea of this, think of your car speedometer, it moves but it doesn’t go from let’s say 5 to 100 in 0.5 ms. It’s a gradual process to read 100, it moves up smoothly to that number. This is what EWMA does.

The formula for this is:

(New Data/Reading) * f  + (Old Data/Reading)*(1-f), where f is frequency

For the program, f was 0.8 so it took 80% of new data and 20% of old data.

This also means I would have only two data, the current reading and the old reading. This means two variables in the program.

  • Timing

For how to measure data, we need the time element. So this means that we should measure pulse to pulse.

 

 

 

 

 

So for this program, you want to measure the time as marked in 1a or 1b, it doesn't matter which one as long as you choose one of these. This is since 1a or 1b will get you the time between each magnet pulse which would give you a wheel rotation and the time it took for that rotation. Measuring 2 will get you the time it is OFF the magnet which is not the intent of the program. Measuring 3 will get you the time it is ON the magnet which is not the intent of the program. 2 and 3 are the implementations of the pulseIn() syntax for Arduino and unfortunately will not do you any good for this program.

For this program you will have to get the timing element using your own code and not a previously written syntax such as pulseIn(). To achieve this you will have to get a program that will start the time when the signal is a rising edge and stops the time when the signal is a rising edge again or start the time when the signal is a falling edge and stops the time when the signal is a filling edge again.

After you achieve that goal of getting time, you need a formula to get the actual speed. I've used this formula to get speed:

After getting that magical number from applying that formula remember to save it to a variable old so that you can apply EWMA with the new speed reading.

  • LEDs

You may also add LEDs lighting up when the hall sensor is near a magnet so it makes it easier to troubleshoot your code. Also so it shows that your code is actually running.

*Also incorperate the display code if you want a more touched up project!

 

Add new comment

Full HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.