Bizzar sample code?

Sample code from "Parallax-FeedBack-360-Servo-Control-Library-4-Arduino-1.0.0"
Can you interpret this line? "value = (abs(errorAngle) < 4) ? 0 : output + offset;"

#include <Servo.h>
Servo Parallax;
// set signal pin to Pin no.2 WHAT IS THIS COMMENT TELLING US?
volatile float angle;
volatile float targetAngle;
int theta = 0;
int thetaP = 0;
volatile unsigned long tHigh = 0;
volatile unsigned long tLow = 0;
volatile unsigned long rise = 0;
volatile unsigned long fall = 0;
volatile float dc;
volatile int tCycle;
int turns = 0;
int Kp = 1;
int unitsFC = 360;
float dcMin = 0.029;
float dcMax = 0.971;
float dutyScale = 1.0;
float q2min = unitsFC / 4.0;
float q3max = q2min * 3.0;
void setup() {
  Serial.begin(9600);
  Serial.println("servo example");
  Parallax.attach(3);
  attachInterrupt(0, feedback360, CHANGE);
  targetAngle = 270;
}
void loop() {
  int errorAngle;
  int output;
  int offset
  int value;
  delay(10000);
  targetAngle = targetAngle + 5;
  Serial.print(" targetAngle: "); Serial.println(targetAngle);
  errorAngle = targetAngle - angle;
  output = errorAngle * Kp;
  if (output > 200)
    output = 200;
  if (output < -200)
    output = -200;
  if (errorAngle > 0)
    offset = 30;
  else if (errorAngle < 0)
    offset = -30;
  else
    offset = 0;
  value = (abs(errorAngle) < 4) ? 0 : output + offset;
  Parallax.writeMicroseconds(1490 - value);
}
void feedback360() {
  if (digitalRead(2)) {
    // rise
    rise = micros();
    tLow = rise - fall;
    // Calcuate duty
    tCycle = tHigh + tLow;
    if ((tCycle < 1000) && (tCycle > 1200))
      return;
    dc = (dutyScale * tHigh) / (float)tCycle;
    theta = ((dc - dcMin) * unitsFC) / (dcMax - dcMin);
    if (theta < 0)
      theta = 0;
    else if (theta > (unitsFC - 1))
      theta = unitsFC - 1;
    if ((theta < q2min) && (thetaP > q3max))
      turns++;
    else if ((thetaP < q2min) && (theta > q3max))
      turns--;
    if (turns >= 0)
      angle = (turns * unitsFC) + theta;
    else if (turns < 0)
      angle = ((turns + 1) * unitsFC) - (unitsFC - theta);
    thetaP = theta;
  } else {
    // fall
    fall = micros();
    tHigh = fall - rise;
  }
}
value = (abs(errorAngle) < 4) ? 0 : output + offset
if (abs(errorAngle) < 4)
{
  value = 0;
}
else
{
  value = output + offset;
}

Google "C ternary operator" for details

This is not an interpretation, this is a long and bizzar story:

Is it this library ? https://github.com/HyodaKazuaki/Parallax-FeedBack-360-Servo-Control-Library-4-Arduino

The 'errorAngle' is already a problem on it own. Because it is calculated from float values, and there is no explanation why it is converted to a integer.

int errorAngle;
volatile float angle;
volatile float targetAngle;
errorAngle = targetAngle - angle;

Then the abs() function is used: "abs(errorAngle)".
It should be a normal abs() function from the C and C++ standard: https://cplusplus.com/reference/cstdlib/abs/.
However, it has been redefined by Arduino in Arduino.h in this line: https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Arduino.h#L94.

#define abs(x) ((x)>0?(x):-(x))

In my opinion, that is a horrible dreadful thing to do. The standard is sacred.
It is no longer a function, but a macro. That is dangerous, because if the parameter is a function or a calculation, then the result is no longer the same.
I this case, it is luckily working as intended.
The condition "(abs(errorAngle) < 4)" tests if the error (the difference between the actual and the aimed angle) is less than 4.
The rest is a "conditional operator". I just read that UKHeliBob wrote that it is called a ternary operator, that is probably the official name.
The long version is:

if(abs(errorAngle) < 4)
{
  value = 0;
}
else
{
  value = output + offset.
}

I think that the code could be simpler and with more explanation.
If that code runs on a Arduino Uno, then it is not good enough.
The interrupt might occur while reading/writing the 4 bytes of the 'angle' variable in the loop().

I think conditional operator is fine. Ternary operator is a more general term.

In C/C++ there is only one ternary operator as far as I know, but several conditional operators

That could very well be. I do not know of any other examples either.

https://www.learncpp.com/cpp-tutorial/comma-and-conditional-operators/

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.