Skip to content

Commit 1cd63d6

Browse files
committed
Clip joystick magnitude to 1.0.
1 parent 1175535 commit 1cd63d6

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

SerialPrograms/Source/CommonFramework/Globals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace PokemonAutomation{
2626
const bool IS_BETA_VERSION = true;
2727
const int PROGRAM_VERSION_MAJOR = 0;
2828
const int PROGRAM_VERSION_MINOR = 54;
29-
const int PROGRAM_VERSION_PATCH = 22;
29+
const int PROGRAM_VERSION_PATCH = 23;
3030

3131
const std::string PROGRAM_VERSION_BASE =
3232
"v" + std::to_string(PROGRAM_VERSION_MAJOR) +

SerialPrograms/Source/Controllers/JoystickTools.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@
88
#define PokemonAutomation_Controllers_JoystickTools_H
99

1010
#include <stdint.h>
11+
#include <cmath>
1112
#include <algorithm>
1213

1314
namespace PokemonAutomation{
1415
namespace JoystickTools{
1516

1617

18+
inline void clip_magnitude(double& x, double& y){
19+
double mag = x*x + y*y;
20+
if (mag <= 1.0){
21+
return;
22+
}
23+
24+
double scale = 1 / std::sqrt(mag);
25+
26+
x *= scale;
27+
y *= scale;
28+
}
1729
inline void max_out_magnitude(double& x, double& y){
1830
double mag = x*x + y*y;
1931
if (mag == 0){

SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase3_ProController.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,14 @@ void ProController_SysbotBase3::push_state(const Cancellable* cancellable, WallD
212212
if (m_left_joystick.is_busy()){
213213
double fx = JoystickTools::linear_u8_to_float(m_left_joystick.x);
214214
double fy = -JoystickTools::linear_u8_to_float(m_left_joystick.y);
215+
JoystickTools::clip_magnitude(fx, fy);
215216
left_x = JoystickTools::linear_float_to_s16(fx);
216217
left_y = JoystickTools::linear_float_to_s16(fy);
217218
}
218219
if (m_right_joystick.is_busy()){
219220
double fx = JoystickTools::linear_u8_to_float(m_right_joystick.x);
220221
double fy = -JoystickTools::linear_u8_to_float(m_right_joystick.y);
222+
JoystickTools::clip_magnitude(fx, fy);
221223
right_x = JoystickTools::linear_float_to_s16(fx);
222224
right_y = JoystickTools::linear_float_to_s16(fy);
223225
}

SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_ProController.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ void ProController_SysbotBase::send_diff(
256256
){
257257
double fx = JoystickTools::linear_u8_to_float(new_state.left_x);
258258
double fy = -JoystickTools::linear_u8_to_float(new_state.left_y);
259+
JoystickTools::clip_magnitude(fx, fy);
259260
// cout << "fx = " << fx << ", fy = " << fy << endl;
260261
int16_t ix = JoystickTools::linear_float_to_s16(fx);
261262
int16_t iy = JoystickTools::linear_float_to_s16(fy);
@@ -271,6 +272,7 @@ void ProController_SysbotBase::send_diff(
271272
){
272273
double fx = JoystickTools::linear_u8_to_float(new_state.right_x);
273274
double fy = -JoystickTools::linear_u8_to_float(new_state.right_y);
275+
JoystickTools::clip_magnitude(fx, fy);
274276
int16_t ix = JoystickTools::linear_float_to_s16(fx);
275277
int16_t iy = JoystickTools::linear_float_to_s16(fy);
276278
message += "setStick RIGHT ";

0 commit comments

Comments
 (0)