mattjlewis / diozero

Java Device I/O library that is portable across Single Board Computers and microcontrollers. Tested with Raspberry Pi, Odroid C2, BeagleBone Black, Next Thing CHIP, Asus Tinker Board and Arduinos / Pico. Supports GPIO, I2C, SPI as well as Serial communication. Also known to work with Udoo Quad.
https://www.diozero.com
MIT License
261 stars 59 forks source link

ServoTrim assumes 180 degree rotation #197

Closed EAGrahamJr closed 4 months ago

EAGrahamJr commented 4 months ago

It took a while to figure out why my gear ratios were not matching real-world mappings until I realized that the servos I'm using (MG90s) move more than 180 degrees with an appropriate pulse-width setting, but the ServoTrim class assumes that a servo cannot move more than 180 degrees.

Thus, ServoTrim should have additional constructors to cover these additional parameters (and adjust all the things accordingly).

mattjlewis commented 4 months ago

I don't believe this constructor assumes 180 degree range of movement as per the new constructor comment:

    public ServoTrim(int midPulseWidthUs, int ninetyDegPulseWidthUs, int minPulseWidthUs, int maxPulseWidthUs) {
        this.midPulseWidthUs = midPulseWidthUs;
        this.ninetyDegPulseWidthUs = ninetyDegPulseWidthUs;
        this.minPulseWidthUs = minPulseWidthUs;
        this.maxPulseWidthUs = maxPulseWidthUs;
        minAngle = RangeUtil.map(minPulseWidthUs, midPulseWidthUs - ninetyDegPulseWidthUs,
                midPulseWidthUs + ninetyDegPulseWidthUs, 0, 180, false);
        maxAngle = RangeUtil.map(maxPulseWidthUs, midPulseWidthUs - ninetyDegPulseWidthUs,
                midPulseWidthUs + ninetyDegPulseWidthUs, 0, 180, false);
    }
EAGrahamJr commented 4 months ago

Well, I'm an idiot -- I was putting the same value for the 90 degree as the mid-point, so of course it was coming out odd.

At least my new constructor is a little easier to use? :blush: