-
Notifications
You must be signed in to change notification settings - Fork 0
Implemented direct joystick logs #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,13 +15,28 @@ struct RefSpeed { | |
| int8_t rightSpeed; ///< Speed of the right wheel. | ||
| }; | ||
|
|
||
| /** | ||
| * Struct representing the reference displace | ||
| */ | ||
| struct RefDisplacement { | ||
| int8_t longDisp; ///< Forward/Backward displacement with + indicating forward | ||
| int8_t latDisp; ///< Side to side displacement with + indicating right | ||
|
Comment on lines
+21
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Based on the implementation in Apply this diff to use an appropriate integer type: struct RefDisplacement {
- int8_t longDisp; ///< Forward/Backward displacement with + indicating forward
- int8_t latDisp; ///< Side to side displacement with + indicating right
+ int16_t longDisp; ///< Forward/Backward displacement with + indicating forward
+ int16_t latDisp; ///< Side to side displacement with + indicating right
};Note: You'll also need to update the corresponding message field types in the ROS message definition to match. |
||
| }; | ||
|
|
||
| /** | ||
| * Reads a value from the joystick connected to the ADC and returns the reference speeds | ||
| * @param adc An instance of the adc the joystick is connected to | ||
| * @return A RefSpeed for the wheelchair containing the wheel speeds and the direction | ||
| */ | ||
| RefSpeed joystickToSpeed(Adafruit_ADS1115 &adc); | ||
|
|
||
| /** | ||
| * Reads a value from the joystick connected to the ADC and returns the reference displacement | ||
| * @param adc An instance of the adc the joystick is connected to | ||
| * @return A RefDisplacement for the joystick | ||
| */ | ||
| RefDisplacement joystickToDisplacement(Adafruit_ADS1115 &adc); | ||
|
|
||
| template <typename T> | ||
| /** | ||
| * Custom clamp function to keep a value between to values | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,28 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+12
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Integer overflow and misleading comment. This function has multiple critical issues:
Apply this diff to normalize displacement to a ±100 range and fix the comment: 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
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
+ * Output is a value -100 to 100 for joystick displacement
*/
- //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);
+ // Center the joystick values around 0
+ forwardJoystick = forwardJoystick - (8500+4400);
+ sidewaysJoystick = sidewaysJoystick - (8400+4400);
+
+ // Normalize to -100 to 100 range
+ const float MAX_INPUT = 13000.0f;
+ float longNorm = constrain(forwardJoystick / MAX_INPUT, -1.0f, 1.0f);
+ float latNorm = constrain(sidewaysJoystick / MAX_INPUT, -1.0f, 1.0f);
+
+ displacements.longDisp = (int8_t)roundf(longNorm * 100.0f);
+ displacements.latDisp = (int8_t)roundf(latNorm * 100.0f);
return displacements;
}Alternative: If you need the full raw ADC range, change the struct fields to 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.