Go Down

Topic: Calculating the difference in a set of variables in percent (Read 940 times) previous topic - next topic

PeterH

Using floats is one way to deal with small values, but it has its drawbacks and is not an approach I'd recommend.

I'd suggest that instead of calculating your slip ratio as an integer, you calculate the percentage slip:

Code: [Select]

int percentageSlip = (100 * diff) / nondrive;


Also suggest you need to consider what happens when any of your calculated values are zero - for example, when the vehicle is stationary. Dividing by zero is rarely useful.
I only provide help via the forum - please do not contact me for private consultancy.

computerjlt

Thanks peter,

for the sake of having more numbers to play with i stopped dividing the 2 sets of pulses for drive and non drive and just add them.  This is working better but I want to speed it up and implement some kind of smoothing.  i also thought about instead of counting pulses i should somehow time the pulses for better resolution;  the shorter the time between pulses the faster the wheel turns. so i could have the program react even faster than ever 100ms.  i could take 5-10 samples and use that for smoothing and still have it react faster than this at 200ms. 
or am i going about this all wrong and should i just calculate the slip on the fly and get rid of the delay function?   if i do that though then i'll really have a problem with resolution;  right now i have to wait 200ms to get enough pulses to do decent math with any shorter and the error goes through the roof

here is the code as it sits:
Code: [Select]

  const int TxPin = 6;
  const int output = 11;
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
  int pot = A0;
  volatile int pulse0 = 0;
  volatile int pulse1 = 0;
  volatile int pulse2 = 0;
  volatile int pulse3 = 0;
  volatile int drive = 0;
  volatile int nondrive = 0;
  volatile int diff = 0;
  volatile int slip = 0;
  volatile int threshold = 0;
  volatile int overslip = 0;
 
void setup() {
  mySerial.begin(9600);
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
  mySerial.write(22);
  mySerial.write(17);
  mySerial.write(212);                  // set Quarter note tone
  mySerial.write(220);                  // A note tone
  mySerial.write(12);                   // blank disp
  mySerial.print("CompJLT INC");        // First line
  mySerial.write(13);                   // Form feed
  mySerial.print("Traction Control");   // Second line
  delay(4000);
  mySerial.write(12);
  attachInterrupt(0, count0, RISING);
  attachInterrupt(1, count1, RISING);
  attachInterrupt(2, count2, RISING);
  attachInterrupt(3, count3, RISING);

}
void loop() {
  mySerial.write(12);
  delay(5);
  noInterrupts();
  drive = pulse0 + pulse1;
  nondrive = pulse3 + pulse3;
  diff = (drive - nondrive);
  slip = (100 * diff) / nondrive;
  interrupts(); 
  mySerial.print(drive);
  mySerial.print("/");
  mySerial.print(nondrive);
  mySerial.print(" ");
  mySerial.write(13); 
  mySerial.print("S:");
  mySerial.print(slip);
  threshold = analogRead(pot);
  threshold = map(threshold, 0, 1023, 0, 100);
  mySerial.print(" T:");
  mySerial.print(threshold);
 
  overslip = slip - threshold;
  if (overslip > 0)
  {
  if (overslip > 100)
  {
  analogWrite(output, 255);
  }
  else
  {   
  overslip = map(overslip, 0, 100, 0, 255);
  analogWrite(output, overslip);
  }
  }
  else
  {
  analogWrite(output, 0);
  }
 
  mySerial.print(" O:");
  mySerial.print(overslip);
   
  pulse0 = 0;
  pulse1 = 0;
  pulse2 = 0;
  pulse3 = 0;
  overslip = 0;
  delay(200);
}
void count0()
{
  pulse0++;
}
void count1()
{
  pulse1++;
}
void count2()
{
  pulse2++;
}
void count3()
{
  pulse3++;
}

Go Up