Haptic Controller
Loading...
Searching...
No Matches
robot_thumb.hpp
Go to the documentation of this file.
1#pragma once
2#include <math.h>
3#include "wiring.h"
4#include "utils/angles.hpp"
5#include <vector>
6#include "utils/helpers.hpp"
7#include "utils/matrix.hpp"
8// #include "utils/vector.hpp"
9
11static constexpr float link_0_length = 0.036;
12static constexpr float link_1_length = 0.03085;
13static constexpr float link_2_length = 0.05225;
14
16static constexpr float transmission_0 = 6.f / 44.f;
17static constexpr float transmission_1 = 1.f / 7.9375f;
18static constexpr float transmission_2 = 1.f / 7.9375f;
19
20// Transmission Matrix
21// [ t_0, 0 ]
22// [ 0 , t_1 ]
23// [ 0 , t_2 ]
24
27static constexpr Limits<float> joint_0_limits{0.0, 60.0};
28
31static constexpr Limits<float> joint_1_limits{0.0, 40.0};
32static constexpr Limits<float> joint_2_limits{0.0, 40.0};
33
34static constexpr Limits<float> joint_0_soft_limits = convert_angular_units(
35 {15.0, 50.0},
38
39static constexpr Limits<float> joint_1_soft_limits = convert_angular_units(
40 {15.0, 50.0},
43
44static constexpr Limits<float> joint_2_soft_limits = convert_angular_units(
45 {15.0, 50.0},
48
49static constexpr float motor_torque_limit = 5.0f; // N*m
50
51static constexpr float joint_0_cali_offset = convert_angular_units(
54
55static constexpr float joint_1_cali_offset = convert_angular_units(
58static constexpr float joint_2_cali_offset = convert_angular_units(
61
62static constexpr Limits<float> joint_0_vel_limits{-10.0, 10.0};
63static constexpr Limits<float> joint_1_vel_limits{-10.0, 10.0};
64
68std::vector<float> forward_kinematics(std::vector<float> joint_angles)
69{
70 const auto theta_0 = joint_angles.at(0);
71 const auto theta_1 = joint_angles.at(1);
72 const auto theta_2 = joint_angles.at(2);
73
74 const auto x1 = link_0_length * sin(theta_0);
75 const auto y1 = link_0_length * cos(theta_0);
76 const auto z1 = 0;
77
78 const auto x2 = x1 + link_1_length * cos(theta_1) * sin(theta_0);
79 const auto y2 = y1 + link_1_length * cos(theta_1) * cos(theta_0);
80 const auto z2 = z1 + link_1_length * sin(theta_1);
81
82 const auto x3 = x2 + link_2_length * cos(theta_1 + theta_2) * sin(theta_0);
83 const auto y3 = y2 + link_2_length * cos(theta_1 + theta_2) * cos(theta_0);
84 const auto z3 = z2 + link_2_length * sin(theta_1 + theta_2);
85
86 return {x3, y3, -z3};
87}
88
93std::vector<float> inverse_kinematics(std::vector<float> pos)
94{
95 const auto x = pos.at(0);
96 const auto y = pos.at(1);
97 const auto z = pos.at(2);
98
99 const auto theta_0 = std::atan2(y, x);
100
101 const auto radius = x * x + y * y + z * z;
102 const auto theta_1 = std::acos(
103 radius - link_1_length * link_1_length - link_2_length * link_2_length) /
104 (2 * link_1_length * link_2_length);
105
106 return {theta_0, theta_1, theta_1};
107}
108
109std::vector<float> motors_to_joints(std::vector<float> motor_vars)
110{
111 const auto phi_0 = motor_vars.at(0);
112 const auto phi_1 = motor_vars.at(1);
113
114 const auto theta_0 = phi_0 * transmission_0;
115 const auto theta_1 = phi_1 * transmission_1;
116 const auto theta_2 = phi_1 * transmission_2;
117
118 return {theta_0, theta_1, theta_2};
119}
120
121std::vector<float> joints_to_motors(std::vector<float> joint_vars)
122{
123 const auto theta_0 = joint_vars.at(0);
124 const auto theta_1 = joint_vars.at(1);
125 // Theta 2 is redundant and discarded.
126
127 const auto phi_0 = theta_0 / transmission_0;
128 const auto phi_1 = theta_1 / transmission_1;
129 return {phi_0, phi_1};
130}
131
132std::vector<float> phi_to_theta(std::vector<float> motor_phis)
133{
134 auto joint_thetas = motors_to_joints(motor_phis);
135 joint_thetas.at(0) += joint_0_cali_offset;
136 joint_thetas.at(1) += joint_1_cali_offset;
137 joint_thetas.at(2) += joint_2_cali_offset;
138 return joint_thetas;
139}
140
141std::vector<float> theta_to_phi(std::vector<float> joint_thetas)
142{
143 joint_thetas.at(0) -= joint_0_cali_offset;
144 joint_thetas.at(1) -= joint_1_cali_offset;
145 joint_thetas.at(2) -= joint_2_cali_offset;
146 const auto motor_phis = joints_to_motors(joint_thetas);
147 return motor_phis;
148}
149
150std::vector<float> tau_to_torque(std::vector<float> joint_taus)
151{
152 const auto tau_0 = joint_taus.at(0);
153 const auto tau_1 = joint_taus.at(1);
154 const auto tau_2 = joint_taus.at(2);
155
156 const auto torque_0 = tau_0 * transmission_0;
157 const auto torque_1 = tau_1 * transmission_1 + tau_2 * transmission_2;
158
159 return {torque_0, torque_1};
160}
161
162std::vector<float> torque_to_tau(std::vector<float> motor_torques)
163{
164 const auto torque_0 = motor_torques.at(0);
165 const auto torque_1 = motor_torques.at(1);
166
167 const auto tau_0 = torque_0 / transmission_0;
168 // This is honestly just a guess Technically the ratio can be anything so long as it sums to one?
169 const auto tau_1 = 0.5f * torque_1 / transmission_1;
170 const auto tau_2 = 0.5f * torque_1 / transmission_2;
171
172 return {tau_0, tau_1, tau_2};
173}
174
176{
177 return Matrix();
178}
179
180std::vector<float> soft_limit_torques(std::vector<float> joint_thetas)
181{
182 static constexpr float discouraging_stiffness = 0.0;
183 float joint_0_d_t = -discouraging_stiffness * joint_0_soft_limits.over_limits(joint_thetas.at(0));
184 float joint_1_d_t = -discouraging_stiffness * joint_1_soft_limits.over_limits(joint_thetas.at(1));
185 // float joint_2_d_t = -discouraging_stiffness * joint_2_soft_limits.over_limits(joint_thetas.at(2));
186
187 return {joint_0_d_t, joint_1_d_t, 0.0f};
188}
constexpr float convert_angular_units(float ang, const AngleUnits curr_unit, const AngleUnits desr_unit)
Converts an angle from one unit to another.
Definition angles.hpp:18
@ RADIANS
Definition angles.hpp:9
@ DEGREES
Definition angles.hpp:10
Class that treats a std::vector as a matrix and implements simple operations.
Definition matrix.hpp:8
std::vector< float > joint_thetas
Definition main.cpp:70
std::vector< float > pos
Definition main.cpp:72
std::vector< float > motors_to_joints(std::vector< float > motor_vars)
std::vector< float > inverse_kinematics(std::vector< float > pos)
Performs inverse kinematics for the robot thumb.
std::vector< float > phi_to_theta(std::vector< float > motor_phis)
Matrix spatial_jacobian(std::vector< float > joint_thetas)
std::vector< float > theta_to_phi(std::vector< float > joint_thetas)
std::vector< float > torque_to_tau(std::vector< float > motor_torques)
std::vector< float > joints_to_motors(std::vector< float > joint_vars)
std::vector< float > tau_to_torque(std::vector< float > joint_taus)
std::vector< float > forward_kinematics(std::vector< float > joint_angles)
Performs forward kinematics for the robot thumb.
std::vector< float > soft_limit_torques(std::vector< float > joint_thetas)
Limit struct to store upper and lower limits.
Definition helpers.hpp:34
T over_limits(T compare) const
Definition helpers.hpp:38