Vimes
Posts: 5
Joined: Tue Feb 25, 2014 8:00 pm

rev counter

Tue Feb 25, 2014 8:18 pm

Hi all,

I am currently running a School STEM club that is working on an electric race car for the Greenpower f24 race series.
Our Mk1 car is currently up and running and the kids are working on MK2. The new car will have a gear system to improve efficiency and battery life.

The question is...

using The raspberry Pi and Piface board could we measure the frequency of the rotation of a large cog.
The cog could have a magnet attached and some form of magnetic switch or hall sensor used to form an input.

Is the pi able to determine the frequency of the input and then use this to light a series of LEDs to show when a gear change up or down is required.

We know the optimum rotational speed of the cog is..283rpm

How hard would the code be to write for this.

Cheers

Vimes

ps will get help from the IT department to help kids write code.

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

Re: rev counter

Tue Feb 25, 2014 10:01 pm

The code will be trivial.

Here is some Python to read pulses on gpio 4 and report the RPM.

The code registers a default callback for gpio 4. The default callback just counts pulses and returns the count when tally() is called. 10 times a second the difference is multiplied by 600 to get the RPM.

The Hall sensor part is just as easy.

The hardest part is likely to be the mechanics.

Code: Select all

#!/usr/bin/env python

import time

import pigpio

REV_GPIO=4

pigpio.start()

# assuming rev counter pulses go to gpio 4

pigpio.set_mode(REV_GPIO, pigpio.INPUT)

rev_callback = pigpio.callback(REV_GPIO)

old_revs = rev_callback.tally()

while 1:
   time.sleep(0.1)
   new_revs  = rev_callback.tally()
   rpm = (new_revs - old_revs) * 10 * 60
   print("rpm={}".format(rpm))
   old_revs = new_revs
   
pigpio.stop()

User avatar
JRV
Posts: 270
Joined: Mon Apr 02, 2012 1:39 pm
Location: Minneapolis, MN

Re: rev counter

Tue Feb 25, 2014 10:30 pm

A RaspberryPi is overkill for this project, use an Arduino Uno.

This reed switch is more durable than the glass ones: https://www.sparkfun.com/products/10601

Your Arduino code will be something like this:

Code: Select all

/*****************************************************
 * The plus side of a red LED is connected to pin 8. 
 * The plus side of a green LED is connected to pin 9. 
 * The plus side of a red LED is connected to pin 10.
 * The negative side of each LED is connected to ground through 470 Ohm resistors.
 *
 * One side of a reed switch is connected to ground.
 * The other side is connected to pin 7, this side also connects to a 10 K resistor 
 * which connects to +5 Volts.
 *
 ******************************************************/

int stime; // Start Time
int etime; // End Time in milleseconds, 212 = 283 RPM.

void setup()
{
  for(int i = 8;i < 11;i++) pinMode(i, OUTPUT);
  pinMode(7, INPUT);
  stime = millis(); //Initialize time.
}


void loop()
{
  if(digitalRead(7)==LOW)
  {
    etime = millis()-stime;
    stime = millis();
    while(digitalRead(7)==LOW); // De-bounce
    for(int i = 8;i < 11;i++) digitalWrite(i, LOW); // Turn all LEDs off.
    if(etime < 207) digitalWrite(8, HIGH); // Fast, turn of red LED.
    else if(etime > 217) digitalWrite(10, HIGH); // Slow, turn on blue LED.
    else digitalWrite(9, HIGH); // In range, turn on green LED.
  }
}  



Vimes
Posts: 5
Joined: Tue Feb 25, 2014 8:00 pm

Re: rev counter

Wed Feb 26, 2014 4:19 pm

Thanks for the replies.

Very helpful. planning to get the kids started on the project soon.
Has to use Raspberry Pi as Greenpower run a competition alongside the racing for using the Pi in the Car.

Cheers

PiGraham
Posts: 3926
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: rev counter

Wed Feb 26, 2014 4:30 pm

Vimes wrote: Has to use Raspberry Pi as Greenpower run a competition alongside the racing for using the Pi in the Car.

Cheers
Make the most of the Pi's capabilities,log the data and download it over WiFi, maybe be once per lap, as it comes into WiFi range, or after the race.
Add accelerometers and pedal & stering position sensors.
Graph power consumption, estimated range, average speed in realtime on a mini TFT in the cockpit.
Show previous best lap 'ghost data'.

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: rev counter

Wed Feb 26, 2014 5:34 pm

Though I love the raspi I would agree with JRV's Aduino suggestion. Why? Because it's lower power consumption, which I guess is what Greenpower's all about :)

(Unless you can also use the raspi to do a lot more at the same time.)

EDIT: Correction, I read your second post PROPERLY this time and you have to use the raspi. That's fine, in that case do more stuff to make more use of it.

First a Model A will use less power, if you're able to use one.

Second you could plot speed and acceleration curves, perhaps estimate the battery life used in mAh, etc. This could get as fancy as you like and would involve a lot of research, to make the project look more impressive.

Vimes
Posts: 5
Joined: Tue Feb 25, 2014 8:00 pm

Re: rev counter

Thu Feb 27, 2014 7:28 pm

Fantastic ideas PiGraham.

In the long term I would love to use Wifi and also look at some of the other ideas. First I need to get it up and running as a rev indicator ( and then get a few kids addicted to it to do other things )

My biggest issue is the time I can commit to this side of the project - BUT the schools network manager seems very interested in running this side of things....

Vimes

User avatar
JRV
Posts: 270
Joined: Mon Apr 02, 2012 1:39 pm
Location: Minneapolis, MN

Re: rev counter

Fri Feb 28, 2014 11:49 am

The program I gave you will run on an RPi, with minor modification, if you install the C libraries from http://wiringpi.com.
This include goes at the beginning:

Code: Select all

#include <wiringPi.h>
And add a main().

Code: Select all

int main (void)
{
  wiringPiSetup() ;
  setup();
  for(;;)
  {
    loop();
  }
  return 0;
} 
Then add the other sensors and things to the end of the loop() function.

Return to “Beginners”