# Post Comp Software Debrief
### Plan
**Technical**
- Planned for a full autonomy stack
- Perception
- Odometry sources from a T265 localization camera
- Global localization from apriltags, bluetooth beacons
- Mapping with 2 D435 cameras (large obstacles) and 2D LiDAR (craters)
- Kalman filtering to fuse apriltag and odometry information
- Planning + Control
- Navigation using RRTstar that produces global plans and MPC to follow the path according to system dynamics
- Task-level control
- Excavation
- utilize current feedback to determine when motors are under high load or when excavation layer is done mining
- utilize weight sensor to determine weight of material mined
- Deposition
- utilize hall sensors to determine joint limits for stored and full extension states
- Actuation
- utilize hall sensors to determine stored and full extension states for the lead screw and an additional magnet to trigger when excavation needs to run
- Drivetrain
- use encoders for velocity control
**Development Process**
- Use iterative development process (plan, implement, test cycles ~ 2 weeks)
1. Localization
2. Mapping
3. Navigation
4. Controls
5. Filtering, final adjustments (ex: move python to C++), Networking
6. Behavior planning
- Use simulator to test algorithms early on
- Use dummybot (previous year's robot) for field testing
### What happened
#### Technical
**Pre-battery fire**
1. localization - done by 11-13-21 ([commit](https://github.com/PurdueLunabotics/purdue_lunabotics/commit/bbc415cde9a2a1bb864bdc49cbe67e77a29412e1))
2. Mapping - done by 1-15-22 ([commit](https://github.com/PurdueLunabotics/purdue_lunabotics/commit/f1677ed59bfa56f46a2012bdf8208ddd05144596), [demo](https://drive.google.com/file/d/1C_caV8ohygM5bCA7l21vpzJx5owz6SHV/view?usp=sharing))
3. Navigation - rrtstar done by 3-17-22 ([commit](https://github.com/PurdueLunabotics/purdue_lunabotics/commit/5135f9642570f4489c16135e5d893acde3d3e63e)), full mpc/rrtstar integration done by 5-5-22 ([commit](https://github.com/PurdueLunabotics/purdue_lunabotics/commit/8d1eaeec400210f3fcc052f49e93643b1014cf48), [demo](https://drive.google.com/file/d/1q_-0F5SwRFeFFhrwNXpyUaUWpCDihTGV/view?usp=sharing))
4. Controls - done by 5-16-22 ([commit](https://github.com/PurdueLunabotics/purdue_lunabotics/commit/682a34cc910c82e150626572a17cac289ec8884b), [demo](https://drive.google.com/file/d/13cE18Q2wBE60Ge9KeXdAMQlM2SPWdFTw/view?usp=sharing))
5. Networking - done by 9-29-21 ([commit](https://github.com/PurdueLunabotics/purdue_lunabotics/commit/d7a1012985839ff973568eb19b27936fa08321dd)) but improvements were made throughout year
6. Behavior planning - Basic behavior tree made by 3-26-22, but not integrated with rest of the components ([commit](https://github.com/PurdueLunabotics/purdue_lunabotics/commit/80f7933ebafe7b4abbec16d1fbe231b81ef5d959))
7. Filtering - kalman filtering algorithm complete, but not integrated into ROS done by 4-30-22 ([commit](https://github.com/PurdueLunabotics/purdue_lunabotics/commit/768e12dc48cdf98c11421a23f6d529e07d4448c8), Evan's repo [commit](https://github.com/EvanLandau/kalman-filter-test/commit/abc61537ff913c707e5144922e4703939ee67e75))
8. Final optimizations (ex: move python to C++) - NC
**Post-battery fire**
Damage:
- Jetson Xavier is unusable, switch to Jetson nano
- T265 Localization camera is unusable, planned to switch to custom implementation of visual odometry with depth cameras, but didn't end up happening (see [rednose](https://github.com/commaai/rednose/tree/7663289f1e68860f53dc34337ef080dde69a2586))
- Networking - done by 5-24-22 ([commit](https://github.com/PurdueLunabotics/purdue_lunabotics/commit/a021ce89e1c3074f89a2d589d3800c3edf16d631))
- Had everything ready to go before inspection had all the SSID priority levels set so that it connects to the router first, small oversight that the SSID required is Team_## (rather I made it PurdueLunabotics).
- Realizing in the last minute that then forgot to change the priority level of Team_10 to be higher than the KSC wifi (this was already done to the PurdueLunabotics SSID), I made the change quickly but made the following silly mistake, resulting in the networking failure:
```bash
# What I typed
nmcli connection modify Team_10 connection.autoconnect-priority 10
# What I should've typed
nmcli connection modify "Team_10 1" connection.autoconnect-priority 10
```
- Manual control - NC,
- Manual control interface and networking were the same code used in previous tests pre-battery fire and are known to work
- Did not get sabertooth motor controller interfacing to work. Had 4 hours with the system to get it working. ([docs for reference to the following information](https://www.dimensionengineering.com/datasheets/Sabertooth2x32.pdf))
- Tried analog control with DIP switch settings of ON-ON-OFF-OFF-ON-OFF
- Motors would move when off, could have been that the last switch was set to ON and then the motors are actuated backward by default
- Tried serial control with single TX pin on teensy connected to S1 and used default `Sweep.ino` code, but no motor movement was observed
- Still need to debug and implement control using the motor controller
**Development Process**
- Iterative development (plan, implement, test cycles ~ 1-2 months)
- Eventually made simulator - March 16th ([commit](https://github.com/PurdueLunabotics/mining_arena_gazebo/commit/c9313f49867323c91ddcdb5b8a0a11697e0dd5d7))
- Testing
1. Did localization testing on cartbot (mounted all of our cameras on a cart and tested localization) (Sept-Nov)
2. Moved to dummybot (prev. year's robot) with manual control (November),
3. Did navigation testing in a simulator and matplotlib visualizer (Jan-April)
4. Did controls testing on the actual fully assembled robot (April-May)
- Had less than 4-5 hours total with new assembled robot to test sensors (electrical wiring mishaps, USB connector having incorrect protocol, battery fire, etc)
- Did not get to test system in a pit
### Observed Problems + Initial Mitigation Ideas
#### During Development
##### Technical
- Attempted bluetooth localization for global localization without dependence on the pose of the robot to an apriltag, but the RSSI signals recieved were very noisy and unusable
- Switch to different protocol and transciever - [Decawave Two-way Ranging](https://www.sewio.net/uwb-sniffer/analyzing-decawave-two-way-ranging-twr/) ([module dataseheet](https://www.mouser.com/datasheet/2/412/DWM1000_Data_Sheet-1950396.pdf))
- Camera calibration is correlated to localization error. Our slight localization errors with the cameras were due to inaccurate extrinsic calibration
- Set up extrinsic calibration using apriltags
1. place the `base_link` of the robot at a predefined pose from the apriltag
2. get the apriltag pose from the camera frame
3. recursively compute the `base_link` -> `camera` frame
- Hall sensors have a hysteresis of 5mm, so putting magnets close to each other results in overlapping magnetic fields (problem with the linear actuator magnets)
- Use actuators with analog feedback mechanisms
- Use smaller magnets or design system such that the magnet placement is greater than the hysteresis distance + nominal detection distance
- Encoders need to be atleast 500-1000 ticks per rotation to allow for finer velocity control (we used 100)
- Ensure this requirement is met when purchasing new encoders
#### During Competition
- During the first run, the robot was positioned in the corner of the field and not in view of the apriltags, so it was not possible to just rotate and detect the apriltags. Therefore, hardcoding initial state behaviors can result in problematic edge cases
- Need global localization without tags
- Even for Alabama (top scoring team), autonomy took significantly longer (1 run = 8 min)
### Moving forward
- We now have a full testing system and a basic perception, planning, and control pipeline that we can greatly improve in the next year
- Test pit
- Working robot mechanically and electrically
- Plan/develop/test mitigation procedures for above problems
- Find more problems from tests
- Repeat
- Salvage the Jetson Xavier
- Deep clean all the parts with isopropyl alcohol and dry them in BIDC oven
- Fingers crossed that it works :hand_with_index_and_middle_fingers_crossed:
- Lunar construction (vague topic of next year's comp)
- Potentially stacking bricks or boxes?
- [Scara robot arm](https://www.google.com/search?q=scara+robot+arm&client=ubuntu&hs=HV0&sxsrf=ALiCzsZXYefHcH19BpJGmJS2Efz8pIZHbA:1654300874361&source=lnms&tbm=isch&sa=X&ved=2ahUKEwib597_vpL4AhXoZDABHS9FAY0Q_AUoAXoECAIQAw&biw=958&bih=483&dpr=2)
- Still large focus is on robust localization, mapping, and navigation, and behavior tree setup
- Will put out incremental updates throughout summer on how the testing goes