Skip to content
This repository was archived by the owner on Apr 9, 2026. It is now read-only.

Implemented direct joystick logs - #1

Merged
alexanderlindquis merged 1 commit into
mainfrom
feature/joystick-logs
Oct 27, 2025
Merged

Implemented direct joystick logs#1
alexanderlindquis merged 1 commit into
mainfrom
feature/joystick-logs

Conversation

@alexanderlindquis

@alexanderlindquis alexanderlindquis commented Oct 15, 2025

Copy link
Copy Markdown
Contributor

Added joystick logs

Summary by CodeRabbit

  • New Features
    • Adds joystick-based displacement reference (longitudinal and lateral) alongside existing speed reference.
    • UI/telemetry pipeline now transmits displacement data with existing speed and sensor readings, improving situational awareness in both normal and debug modes.
    • Gracefully skips displacement usage when joystick input is unavailable.
  • Documentation
    • Added inline documentation for the new displacement capability and its data fields.

@coderabbitai

coderabbitai Bot commented Oct 15, 2025

Copy link
Copy Markdown

Walkthrough

Adds joystick displacement support. Introduces RefDisplacement and joystickToDisplacement(), reads ADC to compute longitudinal/lateral displacement. Updates transmitMsg signature to include displacement in both ROS and ROS_DEBUG builds. main.cpp now computes thetaRef from joystick ADC and passes it to transmitMsg alongside existing speed and sensor data.

Changes

Cohort / File(s) Summary
Joystick Displacement API
MicrocontrollerCode/include/JoystickFunctions.h
Adds public struct RefDisplacement { longDisp, latDisp } and declaration RefDisplacement joystickToDisplacement(Adafruit_ADS1115 &adc); with Doxygen comments. Retains existing speed API.
Joystick Displacement Implementation
MicrocontrollerCode/src/JoystickFunctions.cpp
Implements joystickToDisplacement(...): reads two ADC channels, recenters via fixed midpoints/offsets, returns RefDisplacement. Existing joystickToSpeed unchanged.
Main Loop Integration
MicrocontrollerCode/src/main.cpp
In loop(), creates thetaRef via joystickToDisplacement(joystickAdc) when no ADC error. Updates calls to transmitMsg(...) to pass thetaRef first, preserving existing omega and data flow.
micro-ROS Message Transmission
MicrocontrollerCode/src/microRosFunctions.cpp
Changes transmitMsg signature: now accepts RefDisplacement first (ROS and ROS_DEBUG). Populates outgoing message fields long_disp and lat_disp from thetaRef. Existing assignments remain.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant J as Joystick ADC
  participant F as JoystickFunctions
  participant M as main.cpp (loop)
  participant U as microRosFunctions
  participant R as micro-ROS Transport

  rect rgba(230,245,255,0.6)
  note over M: Each loop iteration
  M->>J: Read raw joystick channels
  M->>F: joystickToDisplacement(adc)
  F-->>M: RefDisplacement (longDisp, latDisp)
  M->>U: transmitMsg(thetaRef, omegaRef, sensors...)
  U->>R: Publish message (disp + speed + sensors)
  end
Loading
sequenceDiagram
  autonumber
  participant J as Joystick ADC
  participant F as JoystickFunctions
  participant M as main.cpp (loop)
  participant U as microRosFunctions (ROS_DEBUG)

  rect rgba(240,255,240,0.6)
  note over M: ROS_DEBUG build
  M->>F: joystickToDisplacement(adc)
  F-->>M: RefDisplacement
  M->>U: transmitMsg(thetaRef, omegaRef)
  U-->>M: Debug output includes displacement
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I nudge the stick—a whisker’s grace,
New longs and lats now take their place.
We hop the wire, transmit with cheer,
Displacement sings for bots to steer.
Carrots aligned, the speeds agree—
A bun of bytes, now latency-free! 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Title Check ⚠️ Warning The title “Implemented direct joystick logs” suggests the PR is focused on logging, but the actual changes introduce a RefDisplacement struct, a joystickToDisplacement API, and update transmitMsg signatures to include displacement data. It does not mention the new displacement measurement functionality or the expanded message interface. As a result, the title fails to convey the primary additions and updates made in this PR. Rename the pull request to clearly reflect the main changes, for example “Add joystick displacement measurement and include it in transmit messages,” so that the title summarizes the new API and message signature updates.
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Docstrings were successfully generated.
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/joystick-logs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (2)
MicrocontrollerCode/src/main.cpp (2)

154-154: Optional: Add space after comma for consistency.

Minor formatting suggestion for readability.

Apply this diff:

-    transmitMsg(thetaRef,omegaRef, usDistances, pirSensors, fanSpeeds, imuData);
+    transmitMsg(thetaRef, omegaRef, usDistances, pirSensors, fanSpeeds, imuData);

167-167: Optional: Add space after comma for consistency.

Minor formatting suggestion for readability.

Apply this diff:

-    transmitMsg(thetaRef,omegaRef);
+    transmitMsg(thetaRef, omegaRef);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7ba7bd8 and b54a0c7.

📒 Files selected for processing (4)
  • MicrocontrollerCode/include/JoystickFunctions.h (1 hunks)
  • MicrocontrollerCode/src/JoystickFunctions.cpp (1 hunks)
  • MicrocontrollerCode/src/main.cpp (3 hunks)
  • MicrocontrollerCode/src/microRosFunctions.cpp (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
MicrocontrollerCode/include/JoystickFunctions.h (1)
MicrocontrollerCode/src/JoystickFunctions.cpp (4)
  • joystickToSpeed (34-147)
  • joystickToSpeed (34-34)
  • joystickToDisplacement (12-32)
  • joystickToDisplacement (12-12)
MicrocontrollerCode/src/main.cpp (2)
MicrocontrollerCode/src/JoystickFunctions.cpp (2)
  • joystickToDisplacement (12-32)
  • joystickToDisplacement (12-12)
MicrocontrollerCode/src/microRosFunctions.cpp (4)
  • transmitMsg (376-405)
  • transmitMsg (376-376)
  • transmitMsg (411-418)
  • transmitMsg (411-411)
🔇 Additional comments (4)
MicrocontrollerCode/include/JoystickFunctions.h (1)

33-38: LGTM!

The function declaration and documentation are clear and appropriate.

MicrocontrollerCode/src/main.cpp (1)

107-111: LGTM! Verify that zero displacement is the intended behavior during ADC errors.

The thetaRef variable is correctly zero-initialized and only populated when the joystick ADC is functioning. When joystick_adc_error is true, zero displacement values will be transmitted, which appears consistent with the existing omegaRef handling pattern.

Confirm that transmitting zero displacement during ADC errors aligns with the system's error handling strategy and won't cause unexpected behavior in downstream ROS nodes.

MicrocontrollerCode/src/microRosFunctions.cpp (2)

376-378: LGTM! Signature and assignments are correct.

The function signature update and displacement field assignments are implemented correctly. The displacement data will be transmitted alongside existing sensor data.

Note: The correctness of transmitted displacement values depends on fixing the int8_t overflow issue identified in MicrocontrollerCode/include/JoystickFunctions.h and the normalization issue in MicrocontrollerCode/src/JoystickFunctions.cpp.


411-415: LGTM! Debug version correctly mirrors the ROS implementation.

The ROS_DEBUG version of transmitMsg is correctly updated to include displacement data, maintaining consistency with the full ROS implementation.

Comment thread MicrocontrollerCode/include/JoystickFunctions.h
Comment on lines +21 to +23
struct RefDisplacement {
int8_t longDisp; ///< Forward/Backward displacement with + indicating forward
int8_t latDisp; ///< Side to side displacement with + indicating right

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: int8_t will overflow with raw ADC displacement values.

Based on the implementation in MicrocontrollerCode/src/JoystickFunctions.cpp (lines 29-30), the displacement calculation produces values in the range of approximately -4400 to +4400 (from ADC readings 0-17390 minus offset 12900). Storing these in int8_t fields (range: -128 to 127) will cause severe integer overflow, resulting in incorrect displacement data being transmitted.

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.

Comment on lines +12 to +32
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;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Integer overflow and misleading comment.

This function has multiple critical issues:

  1. Integer overflow: The calculation forwardJoystick - (8500+4400) produces values in the range of approximately -4400 to +4400 (from ADC readings 0-17390 minus offset 12900), but these are assigned to int8_t fields (range: -128 to 127) in the RefDisplacement struct. This will cause severe overflow, corrupting the displacement data.

  2. Misleading comment: Line 25 states "Output is a value -100 to 100 for the speed of the motor" but the function neither scales to this range nor relates to motor speed—it's displacement data. The comment appears to be copy-pasted from joystickToSpeed without updating.

  3. No normalization: Unlike joystickToSpeed which normalizes and scales to ±100, this function returns raw centered ADC values without any processing.

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 int16_t as suggested in the header file review and keep the raw values. Choose based on whether downstream consumers need raw ADC values or normalized joystick position.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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;
}
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 joystick displacement
*/
// 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;
}

@coderabbitai

coderabbitai Bot commented Oct 27, 2025

Copy link
Copy Markdown

Note

Docstrings generation - SKIPPED
Skipped regeneration as there are no new commits. Docstrings already generated for this pull request at #2.

coderabbitai Bot added a commit that referenced this pull request Oct 27, 2025
Docstrings generation was requested by @arturomatlin.

* #1 (comment)

The following files were modified:

* `MicrocontrollerCode/src/JoystickFunctions.cpp`
* `MicrocontrollerCode/src/main.cpp`
* `MicrocontrollerCode/src/microRosFunctions.cpp`
@arturomatlin arturomatlin self-assigned this Oct 27, 2025
@arturomatlin arturomatlin added the enhancement New feature or request label Oct 27, 2025
@alexanderlindquis
alexanderlindquis merged commit 462ef60 into main Oct 27, 2025
1 check passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants