Hey y'all. I'm a little stuck here. Let me make this simple to understand.
I have encoder feedback on a BLDC motor.
I'm using a ball screw assembly.
I have two interrupts connected to the A channel and B channel of the encoder. Pin 2 is interrupted Channel A is checked then Channel B, this is forward. The same goes for channel B, this is backwards.
The BLDC is being driven by a TI drive, who is fed a PWM signal from the Arduino MEGA pin 9. Ive changed the frequency of the PWM to 30,000HZ. This was a requirement from the drive.
My problem lies here. I have 4 integers.
int desired = 600; ( position the motor at 600 pulses )
int current = encoderCount ( received from the interrupt routines )
int error = 0;
int ref = 0;
error = desired - current;
ref = error;
I need clamps for this value ref. The maximum value I can write out to the PWM is a 255. The smallest value is a 0. I'm rolling over and under because ref is an integer! I can see it with the osciliscope the winds down as the error shrinks. I can clamp the high end with an IF statement. Once my error goes minus I'm having issues. I need 4 conditions.
error is more than 255, clamp error at 255
error is less than 255 but more than 0 leave it alone
error is negative, between -1 and -254. Multiply by -1 and leave the clamp off.
error is negative larger than -255, multiple by -1 and then clamp it at 255
if statements seems clumsy.
I haven't even added any I or D 
int clamp(int const value, int const lhs, int const rhs)
{
return ((value < lhs) ? lhs : ((value > rhs) ? rhs : value));
}
int value = 256;
value = clamp(value, 0, 255);
I'm leaving the analog world and trying my hand in the digital life. Let me break this down, so I can try to understand the idea here.
int clamp(int const value, int const lhs, int const rhs) a subroutine that passes in error, low clamp and high clamp
{
return ((value < lhs) ? lhs : ((value > rhs) ? rhs : value)); return with ?
}
int value = 256; // max limit?
value = clamp(value, 0, 255); // ?
I hope I didn't misunderstand your request!
No, 'value' to be clamped, 'lhs' is min value and 'rhs' is max value.
int value = 256;
value = clamp(value, 0, 255);
'value' is greater than 0, so next check, value is greater than 255 so return value is clamped to 255 and returned.
Thanks for the reminder - been programming to long, I keep forgetting that one and use what I've long used.
Undert9281:
Hey y'all. I'm a little stuck here. Let me make this simple to understand.
I have encoder feedback on a BLDC motor.
I'm using a ball screw assembly.
I have two interrupts connected to the A channel and B channel of the encoder. Pin 2 is interrupted Channel A is checked then Channel B, this is forward. The same goes for channel B, this is backwards.
The BLDC is being driven by a TI drive, who is fed a PWM signal from the Arduino MEGA pin 9. Ive changed the frequency of the PWM to 30,000HZ. This was a requirement from the drive.
My problem lies here. I have 4 integers.
int desired = 600; ( position the motor at 600 pulses )
int current = encoderCount ( received from the interrupt routines )
int error = 0;
int ref = 0;
error = desired - current;
ref = error;
I need clamps for this value ref. The maximum value I can write out to the PWM is a 255. The smallest value is a 0. I'm rolling over and under because ref is an integer! I can see it with the osciliscope the winds down as the error shrinks. I can clamp the high end with an IF statement. Once my error goes minus I'm having issues. I need 4 conditions.
error is more than 255, clamp error at 255
error is less than 255 but more than 0 leave it alone
error is negative, between -1 and -254. Multiply by -1 and leave the clamp off.
error is negative larger than -255, multiple by -1 and then clamp it at 255
if statements seems clumsy.
I haven't even added any I or D 
You will have to use an INT (i.e. a 16 bit signed variable).
Then, do this with it:
[b]var = (var < 0) ? 0 : var;[/b]
[b]var = (var > 255) ? 255 : var;[/b]
This clamps "var" between 0 and 255. Now, notice that the end of each line is "var", and since everything to the right of the equal sign IS "var", you can string these two lines together into one:
[b]var = (var < 0) ? 0 : (var > 255) ? 255 : var;[/b]
hope this helps......
Thanks guys. I'm gonna give it a shot now.
Ha, Still no good. I'm seeing two problems with the oscilloscope.
-
On the very start of the program the encoderCount reads 16, This diminished the error off the bat. I have verified it. The square wave is slightly under 100 percent duty cycle.
-
As the error becomes negative, -1,-2,-3,-4 the PWM is rolling backwards and showing a duty cycle of 100 and shrinking as the error gets bigger and bigger.
/* Encoder Counter Section */
int encoderCount = 16; // Variable to store ongoing encoder count
int channela = 2; // Pin connected to channel A of the encoder
int channelaState = 0; // Variable to store channel A's state
int channelb = 3; // Pin connected to channel B of the encoder
int channelbState = 0; // Variable to store channel B's state
void countUp() // Subroutine that reads the encoders position and counts up.
{
channelaState = digitalRead(2); // Read pin 2 and store it into channelaState
channelbState = digitalRead(3); // Read pin 3 and store it into channelbState
if (channelbState > channelaState) // Is A leading B?
{
encoderCount++; // Increment the memory location
Serial.println(encoderCount);
if (encoderCount == 4000) // Is memory location 500? since we are working with a 240 PPR encoder, our largest recordable number is 500 (roughly 2 revolutions of the motor).
{
encoderCount = 0; // Reset the memory location
}
}
}
void countDown() // A subroutine that reads the encoders position and counts down.
{
channelaState = digitalRead(2); // Read pin 2 and store it into channelaState
channelbState = digitalRead(3); // Read pin 3 and store it into channelbState
if ( channelaState > channelbState ) // Is B leading A?
{
encoderCount--; // Decrement the memory location
Serial.println(encoderCount);
if ( encoderCount < -4000 ) // Is the memoroy location -500? Our largest recorable number is -500.
{
encoderCount = 0; // Reset the location to 0;
}
}
}
void positionTable()
{
delay(1000); // Delay for reading the screen.
int desired = 700; // Desired position for the ball screw assembly
int actual = 0; // Where the ball screw is presently located
int error = 0; // The differece from desired and actual
int ref = 0; // Refence value that feeds the PWM board input
int reverse = 7; // Bit attached to the reverse bit of the drive
ref = constrain(error,0,255); // Clamp the drive ref PWM ref
actual = encoderCount; // actual becomes the current encoder count
error = desired - actual; // Error is calculated
ref = error; // Ref is created by turning the error into a PWM value
Serial.println("desired"); // Debug nonsense
Serial.println(desired);
Serial.println("actual");
Serial.println(actual);
Serial.println("error");
Serial.println(error);
Serial.println("PWM REF");
Serial.println(ref);
digitalWrite(reverse,HIGH); // Error is plus go foward
analogWrite(9,ref); // write ref out to the PWM input
if (error > 0) // Is error is negative, meaning we have gone past our desired location. This should be expected because im only using P terms
{
digitalWrite(reverse, LOW); // Bang the bit LOW, back up!
analogWrite(9, ref); // Write out the PWM ref
}
}
void setup()
{
pinMode(2, INPUT); // Pin 2 is set as an input.
pinMode(3, INPUT); // Pin 3 is set as an input.
pinMode(7, OUTPUT); // Pins 4 is the reverse bit
Serial.begin(9600); // Begin serial communication with Arduino interface.
Serial.println("TEST"); // Print TEST to the screen
attachInterrupt(digitalPinToInterrupt(2), countUp, CHANGE); // Enable the intterupt on pin 2. When triggered by a change of state, it will call countUp subroutine
attachInterrupt(digitalPinToInterrupt(3), countDown, CHANGE); // Enable the intterupt on pin 3. When triggered by a change of state, it will call countDown subroutine
int myEraser = 7; // This is 111 in binary and is used as an eraser for the timer register.
TCCR2B &= ~myEraser; // This operation (AND plus NOT), set the three bits in TCCR2B to 0
int myPrescaler = 1; // This could be a number in [1 , 6]. In this case, 1 corresponds in binary to 001. These are prescalers
TCCR2B |= myPrescaler; // This operation (OR), replaces the last three bits in TCCR2B with our new value 001, 31KHZ clock for the PWM.
encoderCount = 0;
}
void loop()
{
positionTable();
}