Haptic Controller
Loading...
Searching...
No Matches
haptic_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.0726;
12static constexpr float link_1_length = 0.0942;
13
15static constexpr float operator_x_offset = 0.0f;
16static constexpr float operator_y_offset = 0.0f;
17static constexpr float operator_z_offset = -0.05f;
18
19static constexpr float fk_joint_0_offset = convert_angular_units(
20 21.532f, AngleUnits::DEGREES,
22static constexpr float fk_joint_1_offset = convert_angular_units(
23 -42.44f, AngleUnits::DEGREES,
25
27static constexpr float transmission_0 = -1.f / 7.f;
28static constexpr float transmission_1 = -1.f / 3.f;
29
32static constexpr Limits<float> joint_0_limits{-18.719, 107.719};
33
36static constexpr Limits<float> joint_1_limits{38, 90.0};
37
38static constexpr Limits<float> joint_0_soft_limits = convert_angular_units(
39 {0.0, 90.0},
42
43static constexpr Limits<float> joint_1_soft_limits = convert_angular_units(
44 {0.0, 80.0},
47
48static constexpr float joint_0_cali_offset = convert_angular_units(
49 -18.719, AngleUnits::DEGREES,
51
52static constexpr float joint_1_cali_offset = convert_angular_units(
55
56static constexpr Limits<float> joint_0_vel_limits{-10.0, 10.0};
57static constexpr Limits<float> joint_1_vel_limits{-10.0, 10.0};
58
59static constexpr float motor_torque_limit = 5.0f; // N*m
60
64std::vector<float> forward_kinematics(std::vector<float> joint_angles)
65{
66 const auto theta_0 = joint_angles.at(0);
67 const auto theta_1 = joint_angles.at(1);
68
69 const auto theta_0_hat = theta_0 - fk_joint_0_offset;
70 const auto theta_1_hat = theta_1 - fk_joint_1_offset;
71
72 const float xe = link_0_length * cos(theta_0_hat) + link_1_length * cos(theta_0_hat + theta_1_hat) - operator_x_offset;
73 const float ye = 0.f - operator_y_offset;
74 const float ze = -link_0_length * sin(theta_0_hat) - link_1_length * sin(theta_0_hat + theta_1_hat) - operator_z_offset;
75
76 return {xe, ye, ze};
77}
78
82std::vector<float> inverse_kinematics(std::vector<float> pos)
83{
84 const auto radius = pos.at(0) * pos.at(0) + pos.at(2) * pos.at(2);
85 const auto theta_1 = (radius - link_0_length * link_0_length - link_1_length * link_1_length) /
86 (2 * link_0_length * link_1_length);
87 const auto theta_0 = std::atan2(pos.at(2), pos.at(0)) - std::atan2(
88 link_1_length * sin(
89 theta_1),
90 link_0_length + link_1_length * cos(theta_1));
91
92 return {-theta_0 - fk_joint_0_offset, -theta_1 - fk_joint_1_offset};
93}
94
95std::vector<float> motors_to_joints(std::vector<float> motor_vars)
96{
97 const auto phi_0 = motor_vars.at(0);
98 const auto phi_1 = motor_vars.at(1);
99 const auto theta_0 = phi_0 * transmission_0;
100 const auto theta_1 = phi_1 * transmission_1 - theta_0;
101 return {theta_0, theta_1};
102}
103
104std::vector<float> joints_to_motors(std::vector<float> joint_vars)
105{
106 const auto theta_0 = joint_vars.at(0);
107 const auto theta_1 = joint_vars.at(1);
108
109 const auto phi_0 = theta_0 / transmission_0;
110 const auto phi_1 = theta_1 / transmission_1 + theta_0 / transmission_1;
111 return {phi_0, phi_1};
112}
113
114std::vector<float> phi_to_theta(std::vector<float> motor_phis)
115{
116 auto joint_thetas = motors_to_joints(motor_phis);
117 joint_thetas.at(0) += joint_0_cali_offset;
118 joint_thetas.at(1) += joint_1_cali_offset;
119 return joint_thetas;
120}
121
122std::vector<float> theta_to_phi(std::vector<float> joint_thetas)
123{
124 joint_thetas.at(0) -= joint_0_cali_offset;
125 joint_thetas.at(1) -= joint_1_cali_offset;
126 const auto motor_phis = joints_to_motors(joint_thetas);
127 return motor_phis;
128}
129
130std::vector<float> tau_to_torque(std::vector<float> joint_taus)
131{
132 const auto tau_0 = joint_taus.at(0);
133 const auto tau_1 = joint_taus.at(1);
134
135 const auto torque_0 = tau_0 * transmission_0 - tau_1 * transmission_0;
136 const auto torque_1 = tau_1 * transmission_1;
137
138 return {torque_0, torque_1};
139}
140
141std::vector<float> torque_to_tau(std::vector<float> motor_torques)
142{
143 const auto torque_0 = motor_torques.at(0);
144 const auto torque_1 = motor_torques.at(1);
145
146 const auto tau_0 = torque_0 / transmission_0 + torque_1 / transmission_1;
147 const auto tau_1 = torque_1 / transmission_1;
148
149 return {tau_0, tau_1};
150}
151
152Matrix spatial_jacobian(std::vector<float> joint_angles)
153{
154 const auto theta_0 = joint_angles.at(0);
155 const auto theta_1 = joint_angles.at(1);
156
157 const auto theta_0_hat = theta_0 - fk_joint_0_offset;
158 const auto theta_1_hat = theta_1 - fk_joint_1_offset;
159
160 Matrix J_s{2, 2};
161 J_s(0, 0) = -link_0_length * sin(theta_0_hat) - link_1_length * sin(theta_0_hat + theta_1_hat);
162 J_s(0, 1) = -link_1_length * sin(theta_0_hat + theta_1_hat);
163 J_s(1, 0) = -link_0_length * cos(theta_0_hat) - link_1_length * cos(theta_0_hat + theta_1_hat);
164 J_s(1, 1) = -link_1_length * cos(theta_0_hat + theta_1_hat);
165
166 return J_s;
167}
168
169std::vector<float> soft_limit_torques(std::vector<float> joint_thetas)
170{
171 static constexpr float discouraging_stiffness = 0.0f;
172 float joint_0_d_t = -discouraging_stiffness * joint_0_soft_limits.over_limits(joint_thetas.at(0));
173 float joint_1_d_t = -discouraging_stiffness * joint_1_soft_limits.over_limits(joint_thetas.at(1));
174 // float joint_2_d_t = -discouraging_stiffness * joint_2_soft_limits.over_limits(joint_thetas.at(2));
175
176 return {0.0, 0.0};
177}
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 > 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)
std::vector< float > joint_thetas
Definition main.cpp:70
std::vector< float > pos
Definition main.cpp:72
Limit struct to store upper and lower limits.
Definition helpers.hpp:34
T over_limits(T compare) const
Definition helpers.hpp:38