Hi Everyone,
I should start out by explaining the project i'm working on. I am a masters student working on my thesis and for my project I am trying to implement a cycle ergometer with the ability to measure every 6 degrees that the pedal moves. Along with this the bike already has a special crank arm on it that can measure force (company is called SRM). The way this bike works is that the force is measured within the rotating crank, to get the data out of the crank (since you can't use a wire since rotating) the signal converted to a PWM (hz and cadence) signal that is then sent to a display unit on the bike. I am able to connect to this unit and read cadence and hz separately. I am running into issues reading this hz signal. I thought I could just measure the time between rising edges, however I am getting a lot of crazy signals (see attached image, around 1.2seconds worth of data). My area of study is actually exercise physiology and like to play with electronics, however this is beyond me and was hoping I could get a bit of guidance. Attached is an image of the output I see, you can see how it should look, like a sine wave due to nature of how power is produced during each down stroke of the leg. Also is an image from the SRM manual that shows how the data flows. Also is my code, which is working perfect with the two optical slotted switches that measure when a tooth goes by (60 teeth total) or when an index hole goes by. Right now all the data is being read via serial into labview where I do all the calculations. I was wondering if I should use pulseIn? I am sure more information will be needed from my end, just let me know and I will try to get it for you. Hope someone can at least lead me in a better direction. Thanks.
I should start out by explaining the project i'm working on. I am a masters student working on my thesis and for my project I am trying to implement a cycle ergometer with the ability to measure every 6 degrees that the pedal moves. Along with this the bike already has a special crank arm on it that can measure force (company is called SRM). The way this bike works is that the force is measured within the rotating crank, to get the data out of the crank (since you can't use a wire since rotating) the signal converted to a PWM (hz and cadence) signal that is then sent to a display unit on the bike. I am able to connect to this unit and read cadence and hz separately. I am running into issues reading this hz signal. I thought I could just measure the time between rising edges, however I am getting a lot of crazy signals (see attached image, around 1.2seconds worth of data). My area of study is actually exercise physiology and like to play with electronics, however this is beyond me and was hoping I could get a bit of guidance. Attached is an image of the output I see, you can see how it should look, like a sine wave due to nature of how power is produced during each down stroke of the leg. Also is an image from the SRM manual that shows how the data flows. Also is my code, which is working perfect with the two optical slotted switches that measure when a tooth goes by (60 teeth total) or when an index hole goes by. Right now all the data is being read via serial into labview where I do all the calculations. I was wondering if I should use pulseIn? I am sure more information will be needed from my end, just let me know and I will try to get it for you. Hope someone can at least lead me in a better direction. Thanks.
Code:
const byte hzPin = 2; // HZ
const byte hzInterrupt = 2; // = pin 2
const byte degPin = 3; // Chain ring interrupt
const byte degInterrupt = 3; // = pin 3
const byte cPin = 4; // Cadence interrupt
const byte cInterrupt = 4; // = pin 4
const int BUFFERSIZE=20;
int HZ[BUFFERSIZE];
int degnumber = 0;
int cadence = 0;
int start;
int startDT;
int DT;
int startHT;
int HT;
int elapsed;
int lastlevel=0;
long HZ2;
volatile byte i=0;
volatile byte bufind=0;
volatile int encoder0Pos = 0; //Degree Time sensor position
volatile int index1=-1; //Count total pedal revolutions
volatile unsigned int count1=0;
volatile int value=0;
boolean DTready = false;
void setup(void)
{
Serial.begin(115200);
pinMode(hzPin, INPUT);
pinMode(degPin, INPUT);
pinMode(cPin, INPUT);
attachInterrupt(hzInterrupt, counterhz, RISING);
attachInterrupt(degInterrupt, counterdeg, RISING);
attachInterrupt(cInterrupt, doIndex, RISING);
}
void loop(void)
{
if (Serial.available()>0){ //reset index to -1
Serial.read();
index1 = -1;
Serial.flush();
}
if(DTready == true ){
Serial.print ("I:");
Serial.print (index1);
Serial.print ("DT:");
Serial.print (DT);
Serial.print ("CT:");
Serial.print (elapsed);
Serial.print ("P:");
Serial.print (encoder0Pos);
Serial.print ("HZ:");
for (i=0; i<bufind; i++) { //print HZ array
Serial.print(HZ[i]);
if (i<bufind-1){
Serial.print(",");
}
}
Serial.println();
i=0;
memset(HZ,0,sizeof(HZ));
bufind=0;
DTready=false;
}
}
void doIndex() { //Cadence
encoder0Pos=0; //the encoder Home position will equal the current encoder position.
elapsed=millis()-start;
start=millis();
index1++;
}
void counterhz() //Frequency
{
HT=micros()-startHT;
startHT=micros();
HZ2=1000000 / HT;
if ( bufind < BUFFERSIZE ) { //fill array with all HZ per chaing ring tooth
HZ[bufind++] = HZ2;
}
}
void counterdeg()// Degree Time
{
DT=micros()-startDT;
startDT=micros();
DTready = true;
encoder0Pos+=6;
}