Control parameters

A HITI Servo is a variable which lets you control the position and speed of 1 servo with sub-degrees resolution (the position resolution of a servo is around 0.1°). It internally creates and manages a Servo variable from the standard Servo library.

This example introduces you with the main parameters involved in the control of Finite Motions, that is motions which have a defined target position. All these parameters are controlled and watched in HITIPanel using HITI Data. The servo is on pin 8.

 

1) Sketch: 5_MotionControl \ 1_ServoParameters

We start our code by including the HC_Servo.h library and by creating a HITI Servo variable (HC_Servo servo).

We also assign several HITI Data to different control parameters for controlling and monitoring the Servo from HITIPanel :

  • Position control (setpoint type, setpoint, target, current)
  • Speed control (setpoint type, setpoint, max, target, current)
  • Motion Time (target)
  • “START” and “STOP” virtual switches
  • Servo state (can be Ready or Moving)
  • Virtual travel limits 
#include <HITIComm.h>
#include <HC_Servo.h>


// pins assignment 
const int pin_Servo = 8;

// Analog Data assignment
const int ad_PosSetpoint       = 0; // position (setpoint, target, current)
const int ad_PosTarget         = 1;
const int ad_PosCurrent        = 2;
const int ad_SpeSetpoint       = 3; // speed (setpoint, max, target, current)
const int ad_SpeMax            = 4;
const int ad_SpeTarget         = 5;
const int ad_SpeCurrent        = 6;
const int ad_MotionTime        = 7; // motion time (target)

// Digital Data assignment
const int dd_PosType           = 0; // position (setpoint type)
const int dd_SpeType           = 1; // speed (setpoint type)
const int dd_Start             = 2; // virtual switches
const int dd_Stop              = 3; 
const int dd_ServoIsReady      = 4; // servo state
const int dd_ServoIsMoving     = 5;
const int dd_NegLimitIsReached = 6; // travel limits
const int dd_PosLimitIsReached = 7;

// position control
bool  posType     = LOW; // LOW: absolute    /  HIGH: relative
float posSetpoint = 30;  // 30° absolute

// speed control
bool  speType     = LOW; // LOW: speed(°/s)  /  HIGH: motion time(s)
float speSetpoint = 10;  // 10°/s
float speMax      = 100; // 100 (°/s)

// HITI Servo
HC_Servo servo;

During the setup(), we use init() to initialize the Servo and to get the following configuration:

  • We give the Servo an ID number (ID 0)
  • We attach the servo to pin 8
  • We invert the direction of rotation (useful if your servo is turning the wrong way)
  • We add a permanent position offset of -4.3° to adjust our software reference position. This position offset depends on the direction of rotation set above.
  • We ask the servo to move to an initial position of 70°. The servo does this move at max speed (no speed control)

The next thing we do is to limit the reachable position range by setting 2 virtual travel limits: a negative limit at 17° and a positive limit at 165°. It results that the servo will only be able to move between 17° and 165° and will stop when reaching this travel limits.

We end the setup() by sending the initial values of our control parameters to HITIPanel to update their display in the control panels.

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


    // initialize HITI Servo
    servo.init(
            0,           // ID = 0
            pin_Servo,   // attached to pin 8
            true,        // true => invert direction
            -4.3,        // position offset = -4.3°
            70.0);       // initial position = 70°
    
    // set travel limits -/+ (°)
    servo.travelLimits(17.0, 165.0);
    
    // send initial values of control parameters to HITIPanel
    HC_digitalDataWrite(dd_PosType,    posType);     // position
    HC_analogDataWrite(ad_PosSetpoint, posSetpoint);
    HC_digitalDataWrite(dd_SpeType,    speType);     // speed
    HC_analogDataWrite(ad_SpeSetpoint, speSetpoint);
    HC_analogDataWrite(ad_SpeMax,      speMax);
}

Inside the loop(), we start by retrieving the values of the control parameters set through HITIPanel and we set the position and speed setpoints accordingly.

The meaning of the setpoints values depend on the type of setpoints selected in HITIPanel (variables posType and speType). The position setpoint can be absolute or relative, and the speed setpoint can be a speed (°/s) or a motion time (s).

Next, we set the max speed. This is a safety parameter which limits the speed and can increase the motion time accordingly.

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


    // set new control parameters ---------------------------------------------
    
    // read from HITIPanel
    posType     = HC_digitalDataRead(dd_PosType);
    posSetpoint = HC_analogDataRead(ad_PosSetpoint);
    speType     = HC_digitalDataRead(dd_SpeType);
    speSetpoint = HC_analogDataRead(ad_SpeSetpoint);
    speMax      = HC_analogDataRead(ad_SpeMax);

    // 1) set position and speed setpoints
    if (posType == HIGH)
        servo.relativePosition(posSetpoint);
    else
        servo.absolutePosition(posSetpoint);

    if (speType == HIGH)
        servo.motionTime(speSetpoint);
    else
        servo.speed(speSetpoint);

    // 2) set max speed
    servo.maxSpeed(speMax);

After that, we use moveOnTrigger() to start the motion. The motion is started only if the “START” virtual switch is activated and if the Servo is Ready.

Similarly, we use stopOnTrigger() to stop any ongoing motion. The motion is stopped only if the “STOP” virtual switch is activated and if the Servo is still Moving.

Next, we systematically deactivate the virtual switches as we don’t want them to stay activated.

Finally, we send all the data to HITIPanel:

  • Speed (Target, Current)
  • Position (Target, Current)
  • Motion time (Target)
  • Servo state (Ready or Moving)
  • Virtual travel limits (has the servo reached one of them?)
    // start/stop motion -----------------------------------------------------
    
    // move/stop on trigger (START/STOP virtual switches)
    servo.moveOnTrigger(HC_digitalDataRead(dd_Start));
    servo.stopOnTrigger(HC_digitalDataRead(dd_Stop));

    // immediately deactivate the virtual switches
    HC_digitalDataWrite(dd_Start, false);
    HC_digitalDataWrite(dd_Stop,  false);


    // send data to HITIPanel ------------------------------------------------

    // control parameters
    HC_analogDataWrite(ad_PosTarget,  servo.getTargetPosition());  // position
    HC_analogDataWrite(ad_PosCurrent, servo.getCurrentPosition()); 
    HC_analogDataWrite(ad_SpeTarget,  servo.getTargetSpeed());     // speed
    HC_analogDataWrite(ad_SpeCurrent, servo.getCurrentSpeed());
    HC_analogDataWrite(ad_MotionTime, servo.getMotionTime());      // motion time

    // servo state
    HC_digitalDataWrite(dd_ServoIsReady,  servo.isReady());
    HC_digitalDataWrite(dd_ServoIsMoving, servo.isMoving());

    // has the servo reached a travel limit?
    HC_digitalDataWrite(dd_NegLimitIsReached, servo.isNegLimitReached());
    HC_digitalDataWrite(dd_PosLimitIsReached, servo.isPosLimitReached());
}

 

2) Control Panels

  1. Display the HITI DATA Control Panels.
  2. As you can see, the servo is at its initial position of 70° (“current position”).
    image311.png

  3. Display the DIGITAL I/O Control Panels.
  4. The “raw position” is 114.3°. Indeed, the “current position” is obtained after inverting the direction and applying a position offset of -4.3° (180° - (70° - 4.3°) = 114.3°).
    image312.png

  5. Go back to the HITI Data Control Panels. Your servo is “Ready” to start a move. Click on START to move it to the absolute position 30° at 100°/s. During the motion, the servo is “Moving”. And when the motion finishes, it becomes “Ready” again. An action on STOP during the motion will end the motion prematurely.
    image313.png

    image314.png

  6. Now change the position setpoint to “Relative” and the speed setpoint to “Time”. Then enter a relative position setpoint of -5° and a motion time setpoint of 3s.
    image316.png

  7. As you can see, the (absolute) target position as well as the target motion time are updated according to your new setpoints.
    image318.png

  8. Click on START to move your servo 5° in negative direction in 3s.
    image320.png

  9. Repeat this operation several times. You will notice that your servo cannot go lower than 17°. This is normal since it reached the negative limit set in your program.
    image322.png

  10. Now, enter a relative position setpoint of +100° and a motion time setpoint of 0.4s.
    image324.png

  11. You should notice that the target motion time is 1s which is much higher than what you asked for. This is because the target speed has been limited to the max speed 100°/s. Indeed, to perform a motion of 100° at a speed of 100°/s, the motion time is 1s.
    image326.png

  12. Change the max speed to 500°/s. You should now be able to perform your motion in 0.4s at a target speed of 250°/s.
    image328.png

 

3) Chart

  1. Open the Chart window.
  2. Start data acquisition. The servo position is then plotted, and new data are added every 10ms.
  3. In the HITI DATA Control Panels, change the relative position setpoint to +100° and the motion time setpoint to 5s. Then, click on the “START” to start the motion.

    image330.png

  4. When the motion is finished, stop data acquisition. You then get a position profile that is characteristic of Linear Position Profiles:
    • instant acceleration/deceleration
    • constant speed during the motion (20°/s).
      image332.png

 

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