ArduinoのServoクラスについてメモ

ArduinoのServoクラスのwriteメソッドで角度[deg]をパラメータに指定するけど、パルス幅と角度の関係は、サーボの品種によってじゃっかん異なるはず。まあ、概ね 1500usがニュートラル(90°)で、500usが0°、2500usが180°くらいではあるけど。
そこらを正確に指定するには、attachメソッドをattach(pin, min, max) の形で用いる。すなわち、minが0°のときのパルス幅[us]、maxが180°のときのパルス幅[us]である。attachメソッドをattach(pin)の形で用いた場合には、min=544, max=2400となる。つまり、ニュートラル(90°)で1472usである。
参考URL: http://www.musashinodenpa.com/arduino/ref/


たとえば、HiTECのHSR-8498HBの場合、データシートによると、

The servo midpoint is at 1500uS. In standard operation pulses from 900uS to 2100uS move the servo through an angle of about 120 degrees. Using the full range of 550uS to 2450uS an overall operating angle of 190 degrees can be obtained.
Servo Angle (degrees) = (Pulse Width (uS) – 1500) / 10

とあり、つまり1500usがニュートラル(90°)で、600usが0°、2400usが180°である。(上記のデータシートではニュートラルを0°と見ている。) したがって、attachメソッドは次のように記述すべきだ。


Servoオブジェクト.attach(ピン番号, 600, 2400);