Servo

Our library lets you control your servos in HITIPanel with sub-degree resolution, while the standard library only permits degree resolution. This feature allows for finer positioning (the position resolution of a servo is around 0.1°).

The first thing you have to do is to attach a Servo to a pin during the setup() using HC_attachServo(). Once attached, you can control your servo in millidegrees or in microseconds.

 

1) Attaching a servo to a pin

When attaching a servo to a pin, several optional parameters can be set, namely the initial position where the servo must immediately go, and the servo min pulse and max pulse widths.

The servo variable can be created internally by the library or it can be supplied by you. You may need to supply your own servo variable if it must be shared and used by multiple libraries at the same time. In this case, add your servo variable as the last parameter.

Syntax :

  • HC_attachServo(pin)
  • HC_attachServo(pin, initial position)
  • HC_attachServo(pin, min pulse, max pulse)
  • HC_attachServo(pin, position, min pulse, max pulse)
  • HC_attachServo(…, &servo)

Parameters :

  • pin (byte): pin number
  • initial position (unsigned long): initial position. Default : 90000 m°
  • min pulse (unsigned int): min pulse width. Default : 544 us
  • max pulse (unsigned int): max pulse width. Default : 2400 us
  • servo (&Servo): Servo variable supplied by you

 

2) Detaching a servo from a pin

  • HC_detachServo(pin)

Example 1 : Servo variable internally created

Refer to this example : Servo.

#include <HITIComm.h>


// pins assignment
const int pin_Servo = 8;


// run once at power on
void setup()
{
    // initialize library
    HC_begin();

    // attach Servo to the pin. Initial position is 53.7°.
    HC_attachServo(pin_Servo, 53700);
}


// run repeatedly after setup()
void loop()
{
    // communicate with HITIPanel
    HC_communicate();
}

Example 2 : Servo variable created and supplied by you

#include <HITIComm.h>


// pins assignment
const int pin_Servo = 8;

// Servo variable created by you
Servo servo;


// run once at power on
void setup()
{
    // initialize library
    HC_begin();

    // attach Servo to the pin. Initial position is 90°.
    HC_attachServo(pin_Servo, &servo); // don’t forget the &
}


// run repeatedly after setup()
void loop()
{
    // communicate with HITIPanel
    HC_communicate();
}

 

3) Position control (in millidegrees)

  • HC_servoWrite(pin, setpoint)   => write a setpoint (unsigned long) to the servo
  • HC_servoRead(pin)                   => return the setpoint (unsigned long)

The meaning of the setpoint value depends on your servo type:

  • standard                      => setpoint = position:   0 (0°),       90000 (90°),           180000 (180°)
  • continuous rotation   => setpoint = speed:      0 (CCW),   90000 (stopped),   180000 (CW)

Example

Refer to this example : Digital Data (Virtual Switch).

 

4) Position control (in microseconds)

  • HC_servoWriteMicroseconds(pin, setpoint)   => write a setpoint (unsigned int) to the servo
  • HC_servoReadMicroseconds(pin)                   => return the setpoint (unsigned int)

The meaning of the setpoint value depends on your servo type:

  • standard                      => setpoint = position:   700-1000 (0°),       1500 (90°),           2000-2300 (180°)
  • continuous rotation   => setpoint = speed:      700-1000 (CCW),   1500 (stopped),   2000-2300 (CW)

 

Was this article helpful?
0 out of 0 found this helpful