-
Notifications
You must be signed in to change notification settings - Fork 48
Using the IR API
One of the major extensions to Infernal Robotics introduced in version 0.20 was the API to access and manipulate servos from outside of IR GUI.
###Implementation To access the API from your plugin you need to add the IRWrapper.cs file to your project https://github.com/MagicSmokeIndustries/InfernalRobotics/blob/master/InfernalRobotics/InfernalRobotics/API/IRWrapper.cs
Edit the Namespace definition at the top of the IRWrapper you have added to be specific to your plugin
using System.Text;
// TODO: Change this namespace to something specific to your plugin here.
namespace InfernalRobotics.API
{
public class IRWrapper
{
Don't forget to add a using
statement to the top of your code to use your namespace
In the Start Method of your Monobehaviour (or anytime after that) initialize the wrapper.You need to do this after KSP has awaken all the DLLs, if you try and Init the wrapper in the Awake method then the alphabetical order of loading may cause a failure
internal override void Start()
{
IRWrapper.InitWrapper();
if (IRWrapper.APIReady)
{
//do stuff
}
}
Our API provides the following wrapper objects to help you interact with IR:
- bool APIReady - true if API is ready to use. See the example above.
- IRAPI IRController
Main instance of IRAPI. Provides access to
- readonly List ServoGroups - basically a List of objects implementing IControlGroup interface.
This is a simplified representation of Servo Groups as you see them in the IR UI. It has the following properties:
- string Name - The name of the group, cannot be empty.
- float Speed - speed multiplier the user set for this group int he UI
- bool Expanded - whether this group is expanded or collapsed in IR UI
- string ForwardKey - a key user assigned to be the Forward Key
- string ReverseKey - a key user assigned to be the Reverse Key
- readonly List Servos - a list of IServo objects, representing each servo in the group.
It also implements the following methods to operate groups of servos, names are self-explanatory.
- void MoveRight()
- void MoveLeft()
- void MoveCenter()
- void MoveNextPreset()
- void MovePrevPreset()
- void Stop()
Represents a single servo. Does not differentiate between rotating and translating servos. Provides access to the following properties:
- string Name - The name of the servo, cannot be empty.
- readonly uint UID - Unique Identifer of the servo (within the craft, not globally unique).
- setonly Highlight - Can only be set. Set to true to highlight the servo part on the ship. Don't forget to set to false when you no longer need it.
- readonly float Position - position of the servo in external representation (i.e. inverted servos display this differently)
- readonly float MinConfigPosition - minimum possible position of the servo as designed by part creator
- readonly float MaxConfigPosition - maximum possible position of the servo as designed by part creator
- float MinPosition - minimum position of the servo as tweaked by user in the UI
- float MaxPosition - maximum position of the servo as tweaked by user in the UI
- float ConfigSpeed - speed of the servo as designed by part creator
- float CurrentSpeed - current speed at which the servo is moving, usable only in-Flight
- float Speed - current speed multiplier set by user in the UI
- float Acceleration - current speed multiplier set by user in the UI
- readonly bool IsMoving - True is servo is moving
- readonly bool IsFreeMoving - True is servo is uncontrollable (example - docking washer).
- bool IsAxisInverted - Servo's inverted status. True if servo's axis is inverted.
- bool IsLocked - Servo's locked status. True if servo is locked.
It also implements the following methods to individual servo, names are self-explanatory.
- void MoveRight()
- void MoveLeft()
- void MoveCenter()
- void MoveNextPreset()
- void MovePrevPreset()
- void Stop()
- void MoveTo(float position, float speed) - gives command to move to designated position with speed multiplier equal to speed
Below are some example code snippets from our first addon utilizing the API - IR Sequencer
var allServos = new List<IRWrapper.IServo>();
if (currentMode == 0)
{
foreach (IRWrapper.IControlGroup g in IRWrapper.IRController.ServoGroups)
{
allServos.AddRange (g.Servos);
}