This repository was archived by the owner on Apr 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoystickFunctions.cpp
More file actions
154 lines (131 loc) · 5.33 KB
/
Copy pathJoystickFunctions.cpp
File metadata and controls
154 lines (131 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Created by Robbie on 11/18/24.
//
#include "JoystickFunctions.h"
#include <Adafruit_ADS1X15.h>
#include <algorithm>
int diffParam = 30;
int deadzoneParam = 30;
RefDisplacement joystickToDisplacement(Adafruit_ADS1115 &adc){
int forwardJoystick = adc.readADC_SingleEnded(0); //a0 is forward/backward
int sidewaysJoystick = adc.readADC_SingleEnded(1); //a1 is left/right
Serial.println("Joystick longitudinal displacement: " + String(forwardJoystick) + " latitudinal displacement: " + String(sidewaysJoystick));
RefDisplacement displacements;
/*
* Joystick middle values: ~8500
* a0 middle value: ~8500
* a1 middle value: ~8300
* a0 deadzone 10000 - 6500
* a1 deadzone 11000 - 6000
* Joystick Min: 0
* Joystick Max: 17390
* Output is a value -100 to 100 for the speed of the motor
*/
//Converting the speeds so they start around 0 and then go positive and negative
displacements.longDisp = forwardJoystick - (8500+4400); //The second value is used to zero it out when the ADC gain is set to 0 instead of the default (2/3)
displacements.latDisp = sidewaysJoystick - (8400+4400);
return displacements;
}
RefSpeed joystickToSpeed(Adafruit_ADS1115 &adc){
int forwardJoystick = adc.readADC_SingleEnded(0); //a0 is forward/backward
int sidewaysJoystick = adc.readADC_SingleEnded(1); //a1 is left/right
/*
* Joystick middle values: ~8500
* a0 middle value: ~8500
* a1 middle value: ~8300
* a0 deadzone 10000 - 6500
* a1 deadzone 11000 - 6000
* Joystick Min: 0
* Joystick Max: 17390
* Output is a value -100 to 100 for the speed of the motor
*/
//Converting the speeds so they start around 0 and then go positive and negative
forwardJoystick = forwardJoystick - (8500+4400); //The second value is used to zero it out when the ADC gain is set to 0 instead of the default (2/3)
sidewaysJoystick = sidewaysJoystick - (8400+4400);
// Serial.print("Forward joystick: ");
// Serial.println(forwardJoystick);
// Serial.print("Sideways joystick: ");
// Serial.println(sidewaysJoystick);
//setting the speeds
RefSpeed speeds{};
const float MAX_INPUT = 13000.0f;
const float BACKWARD_X_THRESH = 0.65f; // 0.0…1.0
// 1) normalize
float x = constrain(sidewaysJoystick / MAX_INPUT, -1.0f, 1.0f);
float y = constrain(forwardJoystick / MAX_INPUT, -1.0f, 1.0f);
// 1b) pure backward shortcut
if (fabsf(x) < BACKWARD_X_THRESH && y < 0.0f) {
int8_t rev = (int8_t)roundf(y * 100.0f);
speeds.leftSpeed = rev;
speeds.rightSpeed = rev;
//Deadzone
if(speeds.leftSpeed < deadzoneParam && speeds.leftSpeed > -deadzoneParam && speeds.rightSpeed < deadzoneParam && speeds.rightSpeed > -deadzoneParam){
speeds.leftSpeed = 0;
speeds.rightSpeed = 0;
return speeds;
}
return speeds;
}
// 2) magnitude clamp
float mag = hypotf(x, y);
mag = constrain(mag, 0.0f, 1.0f);
// 3+4) decide pivot vs mix
float outer, inner;
if (fabsf(x) > 0.0f && y <= 0.0f) {
// any sideways + backward → pivot (inner=0, outer=1)
outer = 1.0f;
inner = 0.0f;
} else {
// forward or straight back (x==0) → smooth mix
float angle = atan2f(fabsf(x), fabsf(y));
float turn_prop = angle / (M_PI_2);
outer = 1.0f;
inner = 1.0f - turn_prop;
}
// 5) assign inner/outer to left/right
float left_f = (x >= 0.0f) ? inner : outer;
float right_f = (x >= 0.0f) ? outer : inner;
// 6) direction: any sideways → forward pivot; otherwise follow y
int dir = (fabsf(x) > 0.0f) ? +1 : (y >= 0.0f ? +1 : -1);
// 7) apply magnitude & direction
left_f *= mag * dir;
right_f *= mag * dir;
// 8) clamp & scale to –100…+100
left_f = constrain(left_f, -1.0f, 1.0f);
right_f = constrain(right_f, -1.0f, 1.0f);
speeds.leftSpeed = (int8_t)roundf(left_f * 100.0f);
speeds.rightSpeed = (int8_t)roundf(right_f * 100.0f);
//Deadzone
if(speeds.leftSpeed < deadzoneParam && speeds.leftSpeed > -deadzoneParam && speeds.rightSpeed < deadzoneParam && speeds.rightSpeed > -deadzoneParam){
speeds.leftSpeed = 0;
speeds.rightSpeed = 0;
return speeds;
}
//Middle zone to have the same speed
int diff = abs(speeds.leftSpeed - speeds.rightSpeed);
if(diff > -diffParam && diff < diffParam){
// Check if the speeds are positive or negative and set accordingly
if (speeds.leftSpeed > 0 && speeds.rightSpeed > 0) {
// Both speeds are positive, set the smaller one to the larger one
if (speeds.leftSpeed < speeds.rightSpeed) {
speeds.leftSpeed = speeds.rightSpeed;
} else {
speeds.rightSpeed = speeds.leftSpeed;
}
} else if (speeds.leftSpeed < 0 && speeds.rightSpeed < 0) {
// Both speeds are negative, set the larger one to the smaller one
if (speeds.leftSpeed > speeds.rightSpeed) {
speeds.leftSpeed = speeds.rightSpeed;
} else {
speeds.rightSpeed = speeds.leftSpeed;
}
}
}
return speeds;
}
template <typename T>
T clamp(T value, T min, T max) {
if (value < min) return min;
if (value > max) return max;
return value;
}