Skip to content

Full refactor#53

Open
siddharth-narayan wants to merge 12 commits into
Comet-Robotics:mainfrom
siddharth-narayan:refactor
Open

Full refactor#53
siddharth-narayan wants to merge 12 commits into
Comet-Robotics:mainfrom
siddharth-narayan:refactor

Conversation

@siddharth-narayan
Copy link
Copy Markdown

@siddharth-narayan siddharth-narayan commented Apr 7, 2026

Closes #51

Refactor everything

Some highlights:

  • Added Classes to represent control options, like Robot, Encoder, Light, MotionController and more.
  • Refactored quite a bit of control.cpp into the control folder
  • Started using enums for status instead of unknown sentinel values
  • Changed debug method to serial_printf
  • Changed PID strategy to distance + angle control
  • Added geometry calculation, refactoring previous inline calculation
  • Replaced #ifndef... with #pragma once
  • And many more code cleanliness changes

ymmot239 and others added 4 commits April 8, 2026 17:29
It introduces a new TrapezoidProfile, Magnetometer, and Encoder.
It contains the following squashed commits:

testing

it goes in a circle lol

Add new profile/controller

Use new trapezoid profile implementation

Tune PID? encoders are dead now

tweak integral value

add magnetometer library

build magnet class for robot

erm this should work

haha fix magnet object now it works

raise debug level

Move magnet into control, log heading

Update src/robot/magnet.cpp

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

move readDegrees from header to cpp

reset the default offsets to the correct sign

Remove .DS_Store

Fix feedforward overpowering PID

Low pass filter over magnetometer readings

Ensure no state changes (it really didn't before anyway, might revert later)

Add minimum motor power config value

Increase BMM350 Data Rate

Fix feedforward overpowering PID

More tuning?

Ensure no state changes (it really didn't before anyway, might revert later)

Add minimum motor power config value

Tune PID some more

comment out heading reading... looks like its blocking the main thread slightly and causing PID oscillations

Temp fix for now to hopefully prevent blocking the thread? unsure

nvm only works if data ready interrupt pin is enabled

add todo, commented out interrupt code

lol. fixed magnetometer issues (and a lot of other pid stability issues)

all we needed to do was measure the actual loop time instead of the expected loop time

Add interrupt to magnetometer anyway

Wait for magnet ready before finishing bot setup

Add heading controller

Properly wait for magnet to initialize

configurable error tolerance, fix multiplier on heading controller output

continuous pid control for heading

normalize current reading, ignore low pass filter for now

fix calibration, disable interrupt pin again for more consistent analysis

back to high accuracy...

add calibration script

fix calibration cpp

update pid consts

Update drive test to track elapsed time, calculate accel theoretically correct

whoops sign error

new tuning

correctly multiply soft iron matrix

Added minimum speed into trapezoid profile, and feed in current position and velocity instead of expected

Straight line

it will usually travel straight now

Precision updates

Made everything into doubles.
Found a better approximation for the robot
Is close but still unstable

Tuned a modified PID

Changed PID to work on the forward and angular velocity  of the robot as opposed to the speed of the individual motors.
Feedback is based on the x, y, and rotation of the robot.
Gets within about 5cm per forward move.
Todo: realign robot to 0 degrees after forward move is completed

Update control.cpp

Added stop when at the destination.
Adjusted power to fit between -1 and 1 for the motors. As a bonus, the robot drives even straighter.
Tested backwards and longer distances with good results.
Needs a bit more tuning, but is very close.

Update control.cpp

Lowered threshold to stop the robot and moved logging to strings that can be enabled

Update control.cpp

made trapezoidal profile work correctly and fixed some of the debugging info
Need to edit the profile to work based on distance instead of time

Re-wrote trapezoid profile

Made trapezoid profile position based instead of time based.
Makes the robot move slightly smoother.

Rotation

Added rotation support using existing turn function.
So far, looks to be reasonably accurate.

PID refactor

Very rough readability changes.
Makes the new pid work with tile movement
This commit is the *starting point* for refactoring.
It does not compile.

Changes made:
- Added Robot, Motor and Light classes
- Refactored quite a bit of control.cpp to make it much clearer
- Moved control related code into the `control` folder
- Started using enums for centering status instead of unkown sentinel values
- Changed up the frame system for printing every `n` frames
- Moved tests into their own file and separated them from the rest of the code
- Moved Encoder code to the Motor class
- Added serial_printf to more easily debug
- Started centering refactor in center_tick
- Started work on pid_tick
- And more

A lot of functionality was removed. Tests were commented out to get code to compile at some points during development
- Changes were made for compilation success
- Added geometry calculations for easier logic encapsulation
- Refactor actual calculation code: centering, turning, and PID
- Created MotionController class -- uses both PID and TrapezoidalProfile as used in previous controlLoop code.
- Removed commented old code, was likely not usable
- Remove old trapezoidalProfile code
- Most of the PID stuff *should* be good to go
- MotionController uses an updated PID strategy involving merging two distance/angle PID controllers
- Finish PID aligning phase, fill in more centering steps, fill in old drive functions with new goal strategy
- Replace all instances of serialLog and serialLogLn with serial_printf
- Remove #ifndef #define pattern, replace with #pragma once
- Alphabitize #include statements and separate library headers, paired headers, and other headers
- Add new tests
- Added FPS counter to check speed
- Deleted old code, including splines, trapezoidalProfile
- Robot has DriveType status enum
- Misc print improvements
- Readded server communication
- Improved motor power accuracy (analogWriteResoultion)
- Robot can drive backwards to destination instead of always turning around
- Added connection to server back in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor embedded code

2 participants