Haptic Controller
Loading...
Searching...
No Matches
robot_finger.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.048;
12static constexpr float link_1_length = 0.048;
13static constexpr float link_2_length = 0.032;
14
16static constexpr float transmission_0 = -1.f / 4.f;
17static constexpr float transmission_1 = -1.f / 4.f;
18static constexpr float transmission_2 = -3.f / 8.f;
19
20// [ t0 0 ]
21// [ -t0 t1 ]
22// [ 0 t2 ]
23
25static constexpr Limits<float> joint_0_limits = convert_angular_units(
26 {0.0, 90.0},
29static constexpr Limits<float> joint_1_limits = convert_angular_units(
30 {0.0, 90.0},
33static constexpr Limits<float> joint_2_limits = convert_angular_units(
34 {0.0, 90.0},
37
38static constexpr Limits<float> joint_0_soft_limits = convert_angular_units(
39 {15.0, 70.0},
42
43static constexpr Limits<float> joint_1_soft_limits = convert_angular_units(
44 {15.0, 70.0},
47
48static constexpr Limits<float> joint_2_soft_limits = convert_angular_units(
49 {15.0, 70.0},
52
53static constexpr float motor_torque_limit = 5.0f; // N*m
54
55static constexpr float joint_0_cali_offset = convert_angular_units(
58
59static constexpr float joint_1_cali_offset = convert_angular_units(
62
63static constexpr float joint_2_cali_offset = convert_angular_units(
66
67static constexpr Limits<float> joint_0_vel_limits{-10.0, 10.0};
68static constexpr Limits<float> joint_1_vel_limits{-10.0, 10.0};
69
73std::vector<float> forward_kinematics(std::vector<float> joint_angles)
74{
75 const auto theta_0 = joint_angles.at(0);
76 const auto theta_1 = joint_angles.at(1);
77 const auto theta_2 = joint_angles.at(2);
78
79 const float xe = link_0_length * cos(theta_0) + link_1_length * cos(theta_0 + theta_1) +
80 link_2_length * cos(theta_0 + theta_1 + theta_2);
81 const float ye = 0.f;
82 const float ze = -link_0_length * sin(theta_0) - link_1_length * sin(theta_0 + theta_1) -
83 link_2_length * sin(theta_0 + theta_1 + theta_2);
84
85 return {xe, ye, ze};
86}
87
91std::vector<float> inverse_kinematics(std::vector<float> pos)
92{
93 const auto radius = pos.at(0) * pos.at(0) + pos.at(2) * pos.at(2);
94 const auto theta_1 = (radius - link_0_length * link_0_length - link_1_length * link_1_length) /
95 (2 * link_0_length * link_1_length);
96 const auto theta_0 = std::atan2(pos.at(2), pos.at(0)) -
97 std::atan2(
98 link_1_length * sin(theta_1),
99 link_0_length + link_1_length * cos(theta_1));
100
101 return {-theta_0, -theta_1};
102}
103
104std::vector<float> motors_to_joints(std::vector<float> motor_vars)
105{
106 const auto phi_0 = motor_vars.at(0);
107 const auto phi_1 = motor_vars.at(1);
108 const auto theta_0 = phi_0 * transmission_0;
109 const auto theta_1 = phi_1 * transmission_1 - theta_0;
110 const auto theta_2 = 0.0f;
111 return {theta_0, theta_1, theta_2};
112}
113
114std::vector<float> joints_to_motors(std::vector<float> joint_vars)
115{
116 const auto theta_0 = joint_vars.at(0);
117 const auto theta_1 = joint_vars.at(1);
118 // Theta 2 redudnat and discarded.
119
120 const auto phi_0 = theta_0 / transmission_0;
121 const auto phi_1 = theta_1 / transmission_1 + theta_0 / transmission_1;
122 return {phi_0, phi_1};
123}
124
125std::vector<float> phi_to_theta(std::vector<float> motor_phis)
126{
127 auto joint_thetas = motors_to_joints(motor_phis);
128 joint_thetas.at(0) += joint_0_cali_offset;
129 joint_thetas.at(1) += joint_1_cali_offset;
130 joint_thetas.at(2) += joint_2_cali_offset;
131 return joint_thetas;
132}
133
134std::vector<float> theta_to_phi(std::vector<float> joint_thetas)
135{
136 joint_thetas.at(0) -= joint_0_cali_offset;
137 joint_thetas.at(1) -= joint_1_cali_offset;
138 joint_thetas.at(2) -= joint_2_cali_offset;
139 const auto motor_phis = joints_to_motors(joint_thetas);
140 return motor_phis;
141}
142
143std::vector<float> tau_to_torque(std::vector<float> joint_taus)
144{
145 const auto tau_0 = joint_taus.at(0);
146 const auto tau_1 = joint_taus.at(1);
147 const auto tau_2 = joint_taus.at(2);
148
149 const auto torque_0 = tau_0 * transmission_0 - tau_1 * transmission_0;
150 const auto torque_1 = tau_1 * transmission_1 + tau_2 * transmission_2;
151
152 return {torque_0, torque_1};
153}
154
155std::vector<float> torque_to_tau(std::vector<float> motor_torques)
156{
157 const auto torque_0 = motor_torques.at(0);
158 const auto torque_1 = motor_torques.at(1);
159
160 const auto tau_0 = torque_0 / transmission_0 + torque_1 / transmission_1;
161 const auto tau_1 = torque_1 / transmission_1;
162
163 return {tau_0, tau_1};
164}
165
166Matrix spatial_jacobian(std::vector<float> joint_angles)
167{
168 const auto theta_0 = joint_angles.at(0);
169 const auto theta_1 = joint_angles.at(1);
170 const auto theta_2 = joint_angles.at(2);
171
172 Matrix J_s(2, 3);
173
174 J_s(
175 0,
176 0) = -link_0_length * sin(theta_0) - link_1_length * sin(theta_0 + theta_1) - link_2_length * sin(theta_0 + theta_1 + theta_2);
177 J_s(0, 1) = -link_1_length * sin(theta_0 + theta_1) - link_2_length * sin(
178 theta_0 + theta_1 + theta_2);
179 J_s(0, 2) = -link_2_length * sin(theta_0 + theta_1 + theta_2);
180 J_s(
181 1,
182 0) = -link_0_length * cos(theta_0) - link_1_length * cos(theta_0 + theta_1) - link_2_length * cos(theta_0 + theta_1 + theta_2);
183 J_s(1, 1) = -link_1_length * cos(theta_0 + theta_1) - link_2_length * cos(
184 theta_0 + theta_1 + theta_2);
185 J_s(1, 2) = -link_2_length * cos(theta_0 + theta_1 + theta_2);
186
187 return J_s;
188}
189
190std::vector<float> soft_limit_torques(std::vector<float> joint_thetas)
191{
192 static constexpr float discouraging_stiffness = 0.175f;
193 float joint_0_d_t = -discouraging_stiffness * joint_0_soft_limits.over_limits(joint_thetas.at(0));
194 float joint_1_d_t = -discouraging_stiffness * joint_1_soft_limits.over_limits(joint_thetas.at(1));
195 // float joint_2_d_t = -discouraging_stiffness * joint_2_soft_limits.over_limits(joint_thetas.at(2));
196
197 return {joint_0_d_t, joint_1_d_t, 0.0f};
198}
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 haptic finger.
std::vector< float > phi_to_theta(std::vector< float > motor_phis)
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 haptic finger.
Matrix spatial_jacobian(std::vector< float > joint_angles)
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