Loading...
Pages: [1]   Go Down
Author Topic: Crankshaft sensor code  (Read 155 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Folks,
I'm currently involved in an ev conversion project and wrote some code (well mostly copied!) to simulate a 60-2 crankshaft position sensor. Basically the sketch should output 58 pulses , skip two , another 58 and so on. A pot on A0 provides a reference for pulse width and hence rpm. Could someone please have a look at the sketch and let me know if I have the pulse counts correct as i don't have a dso to verify the output?
Code:
/**
crank signal simulator
*/
 
#define PULSE_PIN 10

 
void setup() {
pinMode(PULSE_PIN, OUTPUT);
}
 
/**
Simulate the high of a tooth on a
reluctor wheel
*/
void triggerHigh(int duration) {
digitalWrite(PULSE_PIN, HIGH);
delayMicroseconds(duration);
digitalWrite(PULSE_PIN, LOW);
}
 
/**
Simulate the reference marker on a
reluctor wheel
*/
void triggerReference(int duration) {
// pin should be low already
delayMicroseconds(duration);
delayMicroseconds(duration); // two delays for two missing pulses.
}
 
 
/**
Simulates a 58 tooth reluctor wheel
with a 2 tooth reference
*/
void loop(){
int val = analogRead(0);
val = map(val, 0, 1023, 100, 3500);

for(int i = 0; i <= 58; i++) {
triggerHigh(val);
delayMicroseconds(val);
}
triggerReference(val);
delayMicroseconds(val);
}


many thanks. For anyone interested in the project please see http://www.e39ev.com/
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 97
Posts: 6376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Counting 0 through 58, inclusive, gives you 59 counts:
Code:
for(int i = 0; i <= 58; i++) {

Typically you would count to 58 by counting 0 through 57, inclusive:
Code:
for(int i = 0; i < 58; i++) {


If the two missing pulses are supposed to be the same length as the 58 other pulses you need to do FOUR delays instead of THREE (two in the triggerReference() function and one after).

I would put all the delays in the functions:
Code:
/**
crank signal simulator
*/
 
#define PULSE_PIN 10
 
void setup() {
pinMode(PULSE_PIN, OUTPUT);
}
 
/**
Simulate the high of a tooth on a
reluctor wheel
*/
void triggerHigh(int duration) {
digitalWrite(PULSE_PIN, HIGH);
delayMicroseconds(duration);
digitalWrite(PULSE_PIN, LOW);
delayMicroseconds(val);
}
 
/**
Simulate the reference marker on a
reluctor wheel
*/
void triggerReference(int duration) {
// pin should be low already
delayMicroseconds(duration);
delayMicroseconds(duration); // two delays for two missing pulses.
delayMicroseconds(duration);
delayMicroseconds(duration); // two delays for two missing pulses.
}
 
 
/**
Simulates a 58 tooth reluctor wheel
with a 2 tooth reference
*/
void loop(){
int val = analogRead(0);
val = map(val, 0, 1023, 100, 3500);

for(int i = 0; i <= 58; i++) {
    triggerHigh(val);
    }

triggerReference(val);
}

Logged

Pages: [1]   Go Up
Print
 
Jump to: