Keyboard control

A HITI Servo can perform Continuous Motions, that is motions which have a defined direction but no defined target position. This is particularly useful when you need to move your servo at controlled speed using your computer keyboard.

In this example, we will use the LEFT and RIGHT keys to move the servo in the negative and positive direction respectively, and we will set travel limits to limit the position range.

Refer to section Keyboard Panel to learn how to configure your keyboard to control Digital Data.

 

1) Sketch: 5_MotionControl \ 3_ServoKeyboardControl

We start our program by assigning several HITI Data to different control parameters for controlling and monitoring the Servo from HITIPanel :

  • Current position
  • Continuous Speed control (setpoint, max, target, current)
  • “MOVE -” and “MOVE +” virtual switches
  • Virtual travel limits
#include <HITIComm.h>
#include <HC_Servo.h>


// pins assignment 
const int pin_Servo = 8;

// Analog Data assignment
const int ad_PosCurrent        = 0; // position (current)
const int ad_ConSpeSetpoint    = 1; // continuous speed (setpoint, max, target,current)
const int ad_ConSpeMax         = 2;
const int ad_ConSpeTarget      = 3;
const int ad_ConSpeCurrent     = 4;

// Digital Data assignment
const int dd_MoveNeg           = 0; // virtual switches
const int dd_MovePos           = 1;
const int dd_NegLimitIsReached = 2; // travel limits
const int dd_PosLimitIsReached = 3;

// continuous speed control
float conSpeSetpoint = 10.0; // °/s
float conSpeMax      = 100;  // °/s

// HITI Servo
HC_Servo servo;

During the setup(), we initialize the Servo (ID 0, pin 8, normal direction, position offset of +3.2°, initial position of 90°).

Next, we set 2 virtual travel limits: a negative limit at 21° and a positive limit at 167.5°.

Finally, we end the setup() by sending the initial value 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
            false,       // false => normal direction
            3.2,         // position offset = +3.2°
            90.0);       // initial position = 90°

    // set virtual travel limits -/+ (°)
    servo.travelLimits(21.0, 167.5);

    // send initial values of control parameters to HITIPanel
    HC_analogDataWrite(ad_ConSpeSetpoint, conSpeSetpoint);
    HC_analogDataWrite(ad_ConSpeMax,      conSpeMax);
}

Inside the loop(), we start by retrieving the value of the speed setpoint set through HITIPanel and we apply it using continuousSpeed(). This parameter is the only one required to define a Continuous Motion.

Afterwards, we trigger the servo motion in the positive or negative direction using moveNegative() and movePositive(), according to the activation of the virtual switches from HITIPanel.

Finally, we send the following data to HITIPanel:

  • Continuous speed (Target, Current)
  • Position (Current)
  • Virtual travel limits (has the servo reached one of them?)
// run repeatedly after setup()
void loop()
{
    // communicate with HITIPanel
    HC_communicate();


    // set new control parameters --------------------------------------------

    // read from HITIPanel
    conSpeSetpoint = HC_analogDataRead(ad_ConSpeSetpoint); // speed
    conSpeMax      = HC_analogDataRead(ad_ConSpeMax);

    // set speed setpoint and max value
    servo.continuousSpeed(conSpeSetpoint);
    servo.maxSpeed(conSpeMax);


    // trigger motion --------------------------------------------------------

    // move -/+ on trigger (virtual switches)
    servo.moveNegative(HC_digitalDataRead(dd_MoveNeg));
    servo.movePositive(HC_digitalDataRead(dd_MovePos));


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

    // control parameters
    HC_analogDataWrite(ad_PosCurrent,    servo.getCurrentPosition());       // position
    HC_analogDataWrite(ad_ConSpeTarget,  servo.getContinuousTargetSpeed()); // speed
    HC_analogDataWrite(ad_ConSpeCurrent, servo.getContinuousCurrentSpeed());

    // 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. Click on MOVE+ to start moving the servo in the positive direction at 10°/s. Click once again to stop it. If you don’t, it won’t stop until it reaches the positive limit at 167.5°.
    image349.png

 

3) Keyboard Control

  1. Open the Keyboard Dialog box (CTRL+K or Tools\Keyboard).
    Arduino-computer-control-software-customizable-interface-panel-servo-motion-position-speed-control-keyboard-2.png

  2. As you can see, the LEFT and RIGHT keys are already connected to Digital Data 0 and 1. It means that pressing/releasing LEFT or RIGHT will activate/deactivate Digital Data 0 or 1. Also, you can see that the keyboard control is enabled.
  3. So, let’s try this. Click “OK” to close the Keyboard Dialog box and go back to the HITI DATA Control Panels. Then press LEFT or RIGHT to move your servo. Enjoy!

 

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