/*
* THUN
* Reads the X-Cell RT and drives a controller based on sensed torque.
*
* Created 28 January 2013
* By Alexander Brandt
*
*/
// INPUT PULSE
int pulsePin = 21; // PulseOne from crank connected to pin 31
volatile int pulse = 0; // Hall sensor state
volatile int tempPulse = 2;
// INPUT TORQUE
int torquePin = 7; // Analog IN pin that reads torque
volatile int torque = 0; // Gemeten torque 0-1023
volatile int torqueMax = 0; // Maximum torque measured last 16 pulse changes, which equals one complete circle
//volatile int torqueMaxAge = 0; // Number if pulses that have passed since maxTorque
int torqueLowerLimit = 512; // Inclusive 512: 2,5 volt
int torqueUpperLimit = 600; // 919 to use full scale; 4,5 volt. Lower to increase sensitivity. Too low value will result in power falling away at peak.
volatile int torqueArray[16];
volatile int torqueArrayIndex = 0;
// OUTPUT LED
int ledPin = 13; // LED connected to digital pin 52
int led = 1; // LED STATE
// OUTPUT DRIVE
int drivePin = 8; // PWM pin that drives the controller
volatile int drive = 0; // Waarde die aan de controller wordt doorgegeven 0-255
int driveLowerLimit = 50;
int driveUpperLimit = 216;
// TIME & COUNTER VARIABLES
volatile int triggerCount = 0;
volatile int changeCounter = 0;
volatile long timeExpired = 0;
int loopDelay = 200;
volatile long lastUpdateTime = 0; // in miliseconds from starting the program. note: will need to deal with overflow.
int timeOut = 410;
volatile int pulseCounter = 0;
volatile int timeOutCounter = 0;
volatile int updateReason = 0;
void setup() {
// PULSE
pinMode(pulsePin, INPUT);
// digitalWrite( pulsePin, HIGH);
attachInterrupt(2, callUpdate, CHANGE);
pinMode(torquePin, INPUT); // TORQUE
pinMode(ledPin, OUTPUT); // LED
pinMode(drivePin, OUTPUT); // DRIVE
// Open serial communications and wait for port to open:
Serial.begin(57600);
Serial.println("Hello Dave..");
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()
{
//Serial.println("LOOP");
timeExpired = millis() - lastUpdateTime;
if(timeExpired > timeOut)
{
updateReason = 0;
timeOutCounter++;
update();
//printDebug();
}
// printDebug();
delay(loopDelay);
}
void callUpdate()
{
updateReason = 1;
pulseCounter++;
update();
//printDebug();
}
void update() {
pulse = digitalRead(pulsePin);
digitalWrite(ledPin, pulse);
updateTorqueMax();
setDrive();
lastUpdateTime = millis();
printDebug();
}
void updateTorqueMax(){
if(timeExpired > timeOut)
{
for (int i=0; i<16;i++)
{
torqueArray[i]=0;
}
torqueMax=(1023-analogRead(torquePin));
}
else
{
torqueArray[torqueArrayIndex]=(1023-analogRead(torquePin));
torqueMax=getTorqueMaxFromTorqueArray();
if(torqueArrayIndex==15)
{
torqueArrayIndex=0;
}
else
{
torqueArrayIndex++;
}
}
}
int getTorqueMaxFromTorqueArray(){
int max=0;
for (int i=0; i<16;i++){
if(max<torqueArray[i]){
max = torqueArray[i];
}
}
return max;
}
void setDrive(){
if(torqueMax<torqueLowerLimit)
{
drive = driveLowerLimit-1;
// Serial.println("WARNING: Recorded torque is lower than expected!!");
// Serial
}
else if(torqueMax>torqueUpperLimit)
{
drive = driveLowerLimit-1;
// Serial.println("WARNING: Recorded torque is higher than expected!!");
}
else
{
drive = map(torqueMax, torqueLowerLimit, torqueUpperLimit, driveLowerLimit, driveUpperLimit);
}
analogWrite(drivePin, drive);
}
void printDebug(){
/*
Serial.print("\r \r");
Serial.print("PULSE:");
Serial.print(pulse);
Serial.print(" R:");
Serial.print(updateReason);
*/
// Serial.print(timeExpired);
Serial.print(" T:");
Serial.print(timeOutCounter);
Serial.print(" P:");
Serial.print(pulseCounter);
/*
// Serial.print(" TRIGG:");
// Serial.print(triggerCount);
// Serial.print(" DELTA:");
// Serial.print(changeCounter);
// Serial.print(" TRT:");
// Serial.print(1023-analogRead(torquePin));
Serial.print(" I:");
Serial.print(torqueArrayIndex);
Serial.print(" TIND:");
Serial.print(torqueArray[torqueArrayIndex]);
Serial.print(" TMAX:");
Serial.print(torqueMax);
//Serial.print(" TIME:");
//Serial.print(timeExpired);
*/
Serial.print(" DRV:");
Serial.println(drive);
// Serial.println(".");
}