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

Commit 462ef60

Browse files
Merge pull request #1 from TourGuideSeniorDesign/feature/joystick-logs
Implemented direct joystick logs, will change detected errors in followup commit
2 parents 7ba7bd8 + b54a0c7 commit 462ef60

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

MicrocontrollerCode/include/JoystickFunctions.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,28 @@ struct RefSpeed {
1515
int8_t rightSpeed; ///< Speed of the right wheel.
1616
};
1717

18+
/**
19+
* Struct representing the reference displace
20+
*/
21+
struct RefDisplacement {
22+
int8_t longDisp; ///< Forward/Backward displacement with + indicating forward
23+
int8_t latDisp; ///< Side to side displacement with + indicating right
24+
};
25+
1826
/**
1927
* Reads a value from the joystick connected to the ADC and returns the reference speeds
2028
* @param adc An instance of the adc the joystick is connected to
2129
* @return A RefSpeed for the wheelchair containing the wheel speeds and the direction
2230
*/
2331
RefSpeed joystickToSpeed(Adafruit_ADS1115 &adc);
2432

33+
/**
34+
* Reads a value from the joystick connected to the ADC and returns the reference displacement
35+
* @param adc An instance of the adc the joystick is connected to
36+
* @return A RefDisplacement for the joystick
37+
*/
38+
RefDisplacement joystickToDisplacement(Adafruit_ADS1115 &adc);
39+
2540
template <typename T>
2641
/**
2742
* Custom clamp function to keep a value between to values

MicrocontrollerCode/src/JoystickFunctions.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99
int diffParam = 30;
1010
int deadzoneParam = 30;
1111

12+
RefDisplacement joystickToDisplacement(Adafruit_ADS1115 &adc){
13+
int forwardJoystick = adc.readADC_SingleEnded(0); //a0 is forward/backward
14+
int sidewaysJoystick = adc.readADC_SingleEnded(1); //a1 is left/right
15+
16+
RefDisplacement displacements;
17+
/*
18+
* Joystick middle values: ~8500
19+
* a0 middle value: ~8500
20+
* a1 middle value: ~8300
21+
* a0 deadzone 10000 - 6500
22+
* a1 deadzone 11000 - 6000
23+
* Joystick Min: 0
24+
* Joystick Max: 17390
25+
* Output is a value -100 to 100 for the speed of the motor
26+
*/
27+
28+
//Converting the speeds so they start around 0 and then go positive and negative
29+
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)
30+
displacements.latDisp = sidewaysJoystick - (8400+4400);
31+
return displacements;
32+
}
33+
1234
RefSpeed joystickToSpeed(Adafruit_ADS1115 &adc){
1335
int forwardJoystick = adc.readADC_SingleEnded(0); //a0 is forward/backward
1436
int sidewaysJoystick = adc.readADC_SingleEnded(1); //a1 is left/right

MicrocontrollerCode/src/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,14 @@ void loop() {
104104
//TODO might want to figure out how to put these on core1 so that they can run in parallel
105105
//uint32_t start = millis();
106106
RefSpeed omegaRef{};
107+
RefDisplacement thetaRef{};
107108
if (!joystick_adc_error) {
108109
omegaRef = joystickToSpeed(joystickAdc);
110+
thetaRef = joystickToDisplacement(joystickAdc);
109111
}
110112

113+
114+
111115

112116
//uint32_t joystickTime = millis() - start;
113117
USData usDistances{};
@@ -147,7 +151,7 @@ void loop() {
147151

148152
microRosTick();
149153

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

152156
if (currentMillis - lastErrorTime >= error_timer) {
153157
lastErrorTime = currentMillis;
@@ -160,7 +164,7 @@ void loop() {
160164
#elif ROS_DEBUG
161165

162166

163-
transmitMsg(omegaRef);
167+
transmitMsg(thetaRef,omegaRef);
164168

165169

166170
#elif DEBUG

MicrocontrollerCode/src/microRosFunctions.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ static void lidar_subscription_callback(const void *msgin){
373373
}
374374

375375
#ifdef ROS
376-
void transmitMsg(RefSpeed omegaRef, USData ultrasonicData, PIRSensors pirSensors, FanSpeeds fanSpeeds, IMUData imuData) {
376+
void transmitMsg(RefDisplacement thetaRef, RefSpeed omegaRef, USData ultrasonicData, PIRSensors pirSensors, FanSpeeds fanSpeeds, IMUData imuData) {
377+
sensorMsg.long_disp = thetaRef.longDisp;
378+
sensorMsg.lat_disp = thetaRef.latDisp;
377379
sensorMsg.left_speed = omegaRef.leftSpeed;
378380
sensorMsg.right_speed = omegaRef.rightSpeed;
379381
sensorMsg.ultrasonic_front_0 = ultrasonicData.us_front_0;
@@ -406,9 +408,11 @@ void transmitMsg(RefSpeed omegaRef, USData ultrasonicData, PIRSensors pirSensors
406408

407409
#elif ROS_DEBUG
408410

409-
void transmitMsg(RefSpeed omegaRef){
411+
void transmitMsg(RefDisplacement thetaRef, RefSpeed omegaRef){
410412
msg.left_speed = omegaRef.leftSpeed;
411413
msg.right_speed = omegaRef.rightSpeed;
414+
msg.long_disp = thetaRef.longDisp;
415+
msg.lat_disp = thetaRef.latDisp;
412416

413417
RCSOFTCHECK(rclc_executor_spin_some(&executor, RCL_MS_TO_NS(10)));
414418
}

0 commit comments

Comments
 (0)