# Joint Control

## Selected Joint

The low-level motor controller allows fine-grained control over the robot's joints. You can access various joint states and five commands to your selected joints:

{% code title="C++" lineNumbers="true" %}

```cpp
#include <iostream>
#include "bysense_joint.h"  // Include the header file for the Joint class

int main() {
    // Assuming joint IDs are integers from 1 to 12 for 12 joints
    int joint_id = 1; // Change this value to access different joints (1 to 12)

    // Create an instance of the Joint class for the specified joint ID
    bysense::core::Joint selected_joint;

    // Initialize the joint with the provided ID (assuming a constructor or initialization method exists)
    myJoint.set_ID(joint_id); // Set the joint ID

    // Set joint properties
    selected_joint.set_position_rad(1.5);        // Set the position to 1.5 radians
    selected_joint.set_posmax_rad(2.0);          // Set the maximum position to 2.0 radians
    selected_joint.set_posmin_rad(0.0);          // Set the minimum position to 0.0 radians
    selected_joint.set_torque_Nm(10.0);          // Set the torque to 10 Nm
    selected_joint.set_max_torque_Nm(20.0);      // Set the maximum torque to 20 Nm
    selected_joint.set_velocity_rads(1.0);        // Set the velocity to 1 rad/s
    selected_joint.set_max_velocity_rads(5.0);    // Set the maximum velocity to 5 rad/s

    // Retrieve and display joint properties
    std::cout << "Joint ID: " << selected_joint.get_ID() << std::endl;
    std::cout << "Joint ID Name: " << selected_joint.get_IDnames() << std::endl;
    std::cout << "Current Position (rad): " << selected_joint.get_position_rad() << std::endl;
    std::cout << "Max Position (rad): " << selected_joint.get_posmax_rad() << std::endl;
    std::cout << "Min Position (rad): " << selected_joint.get_posmin_rad() << std::endl;
    std::cout << "Current Torque (Nm): " << selected_joint.get_torque_Nm() << std::endl;
    std::cout << "Max Torque (Nm): " << selected_joint.get_max_torque_Nm() << std::endl;
    std::cout << "Current Velocity (rad/s): " << selected_joint.get_velocity_rads() << std::endl;
    std::cout << "Max Velocity (rad/s): " << selected_joint.get_max_velocity_rads() << std::endl;
    std::cout << "Current Controller Temperature (°C): " << selected_joint.get_temp_controller_C() << std::endl;
    std::cout << "Max Controller Temperature (°C): " << selected_joint.get_maxtemp_controller_C() << std::endl;

    // Set PID controller values
    selected_joint.set_kp(0.1);  // Set proportional gain
    selected_joint.set_ki(0.05); // Set integral gain
    selected_joint.set_kd(0.01); // Set derivative gain

    // Display PID controller values
    std::cout << "Kp: " << selected_joint.get_kp() << std::endl;
    std::cout << "Ki: " << selected_joint.get_ki() << std::endl;
    std::cout << "Kd: " << selected_joint.get_kd() << std::endl;

    return 0;
}

```

{% endcode %}

## All Joints

bysense has by default 12 joints. If you want to control all of those joints at once you have the change to use the joints class which contains all of the used joints as once.

{% code title="C++" lineNumbers="true" %}

```cpp
#include <iostream>
#include "bysense_joints.h"  // Include the header file for the Joints class

int main() {
    // Create an instance of the Joints class
    bysense::core::Joints bysense_joints;

    // Example: Set positions for all 12 joints
    std::array<double, 12> jointPositions = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5,
                                              0.6, 0.7, 0.8, 0.9, 1.0, 1.1};
    bysense_joints.set_JointsPos_rad(jointPositions);  // Set positions for all joints

    // Example: Set torques for all 12 joints
    std::array<double, 12> jointTorques = {5.0, 5.0, 5.0, 5.0, 5.0, 5.0,
                                             5.0, 5.0, 5.0, 5.0, 5.0, 5.0};
    bysense_joints.set_JointsTorque(jointTorques);  // Set torques for all joints

    // Example: Set maximum torques for all 12 joints
    std::array<double, 12> jointMaxTorques = {10.0, 10.0, 10.0, 10.0, 10.0, 10.0,
                                               10.0, 10.0, 10.0, 10.0, 10.0, 10.0};
    bysense_joints.set_JointsMaxTorque(jointMaxTorques);  // Set max torques for all joints

    // Example: Set velocities for all 12 joints
    std::array<double, 12> jointVelocities = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
                                               1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
    bysense_joints.set_JointsVel_rads(jointVelocities);  // Set velocities for all joints

    // Example: Set maximum velocities for all 12 joints
    std::array<double, 12> jointMaxVelocities = {2.0, 2.0, 2.0, 2.0, 2.0, 2.0,
                                                  2.0, 2.0, 2.0, 2.0, 2.0, 2.0};
    bysense_joints.set_JointsMaxVel_rads(jointMaxVelocities);  // Set max velocities for all joints

    // Example: Retrieve and display current joint positions
    std::array<double, 12> currentPositions = bysense_joints.get_JointsPos_rad();
    std::cout << "Current Joint Positions (rad): ";
    for (const auto& pos : currentPositions) {
        std::cout << pos << " ";
    }
    std::cout << std::endl;

    // Example: Retrieve and display current torques
    std::array<double, 12> currentTorques = bysense_joints.get_JointsTorque();
    std::cout << "Current Joint Torques (Nm): ";
    for (const auto& torque : currentTorques) {
        std::cout << torque << " ";
    }
    std::cout << std::endl;

    // Example: Retrieve joint temperatures
    std::array<double, 12> jointTemperatures = bysense_joints.get_jointTemp_C();
    std::cout << "Current Joint Temperatures (°C): ";
    for (const auto& temp : jointTemperatures) {
        std::cout << temp << " ";
    }
    std::cout << std::endl;

    // Example: Set PID controller gains for all joints
    std::array<double, 12> kp = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
                                  1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
    bysense_joints.set_jointkp(kp);  // Set proportional gains for all joints

    std::array<double, 12> ki = {0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
                                  0.1, 0.1, 0.1, 0.1, 0.1, 0.1};
    bysense_joints.set_jointki(ki);  // Set integral gains for all joints

    std::array<double, 12> kd = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01,
                                  0.01, 0.01, 0.01, 0.01, 0.01, 0.01};
    bysense_joints.set_jointkd(kd);  // Set derivative gains for all joints

    // Example: Retrieve joint IDs and names
    std::array<int, 12> jointIDs = bysense_joints.get_jointsID();
    std::cout << "Joint IDs: ";
    for (const auto& id : jointIDs) {
        std::cout << id << " ";
    }
    std::cout << std::endl;

    std::array<std::string, 12> jointNames = bysense_joints.get_jointsnames();
    std::cout << "Joint Names: ";
    for (const auto& name : jointNames) {
        std::cout << name << " ";
    }
    std::cout << std::endl;

    // Example: Get joint status
    std::array<std::string, 12> jointStatus = bysense_joints.get_status();
    std::cout << "Joint Status: ";
    for (const auto& status : jointStatus) {
        std::cout << status << " ";
    }
    std::cout << std::endl;

    return 0;
}

```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bywerks.gitbook.io/bysense/mission-development/joint-control.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
