arduino / ArduinoCore-nRF528x-mbedos

[Archived] Arduino core supporting mbed-enabled boards
86 stars 34 forks source link

Servo position read returns wrong value for Nano 33 BLE Sense #109

Closed arpita-agrawal closed 3 years ago

arpita-agrawal commented 3 years ago

with Mbed 1.1.6

The read method of Servo returns incorrect values. If position 0 is written the read value is -1 and for 180 the returned value is ~ 177- 178.

The issue is not seen with SAM, SAMD, or AVR.

Please use the below sketch for reproducing the issue:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
}

void loop() {
  myservo.write(0);
  int currPos = myservo.read();
  Serial.println(currPos);
  delay(150);
  myservo.write(180);
  currPos = myservo.read();
  Serial.println(currPos);
  delay(150);
}
facchinm commented 3 years ago

Hi @arpita-agrawal , the difference in mbed package is that the callback overhead is much larger (see https://github.com/arduino-libraries/Servo/blob/master/src/mbed/Servo.cpp#L46 against https://github.com/arduino-libraries/Servo/blob/master/src/samd/Servo.cpp#L27 ). So, when the map() calculation is performed https://github.com/arduino-libraries/Servo/blob/a98c543e7dabf8ad4114c748d4370997275ca200/src/mbed/Servo.cpp#L107-L117 the value written can "swing" more than in other architectures. TRIM_DURATION value was found empirically, so maybe reducing it doesn't affect performances. @dcuartielles can we retest it on real hardware with a nano33ble / portenta and check if we can fix it in some way? @arpita-agrawal I'm closing the issue here since it's not related with mbed core itself, please reopen in https://github.com/arduino-libraries/Servo