Posted 13 September 2018
Now that I have worked out most of the problems associated with the MPU6050 6DOF IMU module, it was time to integrate the new heading-based turn algorithm into the main Wall-E2 operating system. As I have done in many past projects over the last half-century or so, I started this process by documenting the entire OS, with particular emphasis on how Wall-E2 currently navigates around his world. When I started doing this in the 1970’s, the medium I used was an MIT Engineering notebook, hand-written in ink. Over the ensuing decades the medium has changed, but not the basic idea – the process of putting coherent sentences and paragraphs onto paper (or screen) forces me to think through what is – and is not – important/true. I have solved many a seemingly intractable problem not with an oscilloscope or debugging tool, but by simply writing things down. In the current iteration of this process, I use Microsoft Word (not for any particular reason, except that it is available and familiar) initially, and then dump the results into a post like this one – see below ;-).
Description of FourWD_WallE2_V1 Navigation Algorithm
At the start of each pass through loop(), the software determines the current OPMODE given the current environment and the immediately previous OPMODE. The existing OPMODEs are NONE, CHARGING, IRHOMING, WALLFOLLOW, and DEADBATTERY
- NONE: Default OPMODE when no other mode can be found to apply to the situation. As of this writing, the only use for this OPMODE is to initialize the PrevOpMode and CurrentOpMode loop variables in Setings()
- CHARGING: set in GetOpMode() if the charger is physically connected (CHG_CONNECT_PIN goes HIGH) and the CHG_SIG_PIN is active (LOW). In the CurrentOpMode Switch the PrevOpMode is also set to CHARGING (so that both prev and current op modes are CHARGING), the motors are stopped, and MonitorChargeUntilDone() is called.
- MonitorChargeUntilDone() blocks until charging is complete, or the BATT_CHG_TIMEOUT value is reached or the charger is physically disconnected (manually pulled out for some reason).
- IRHOMING: Set in GetOpMode() when the call to IRBeamAvail() (checks IR beacon signal strength) returns TRUE. In the CurrentOpMode Switch the PrevOpMode is also set to IRHOMING (so that both prev and current op modes are IRHOMING). A blocking call is made to IRHomeToChgStn() with an ‘Avoidance Distance’ of 0 for ‘hungry’ or 30cm (for ‘full- no need to charge’. The idea here is that in the ‘full’ case, the robot will continue to home until near the charging station, and then break off.
- IRHomeToChgStn(): sets up a PID and enters a loop, exited only when either the charger connects, or the robot gets stuck or it gets too close to the charging station (this can only happen in the ‘full’ case). ‘Is Stuck’ is determined in IsStuck() if the front distance variance gets too small (i.e. the front distance isn’t changing).
- WALLFOLLOW: This is the OpMode that is assigned by GetOpMode() when none of the other mode conditions apply. IOW, this is what the robot does when it isn’t doing anything else. In the WALLFOLLOW Case section of the CurrentOpMode Switch, the wall-following operation is further broken down into a TrackingCase Switch, with TRACKING_LEFT, TRACKING_RIGHT, TRACKING_NEITHER sub-modes, with state mode variables maintained for both the current and previous tracking modes. Each time through the loop(), the various tracking cases make one adjustment to the left/right motor speeds – there are no blocking calls at all in the entire WALLFOLLOW section, with the exception of the ‘BackupAndTurn()’ calls in the TRACKING_LEFT and TRACKING_RIGHT cases when an obstruction or the ‘stuck’ condition is detected.
- BackupAndTurn( bool bIsLeft, int motor_speed): The idea here is for the robot to back up and do a course change to extract itself from some situation. Up until now, this has been accomplished by making a timed turn one way or the other, but this hasn’t worked well because the correct time for turning on carpet is wildly different than the correct time on hard flooring. The new heading sensor is intended to solve this problem.
- Now that Wall-E2 can make accurate turns, the question becomes “what’s the best way to do obstruction-avoidance or stuck-recovery turns?”. If Wall-E2 is following a wall when it gets stuck, maybe it should back up slightly and try to go around, or maybe it should just turn around and go back the way it came. Maybe a simple obstruction should be treated one way, but a ‘stuck’ condition treated another? The ‘go back the way I came’ model is simple enough but might result in an uninteresting ‘ping-pong’ shuttle track where it stays until it runs out of battery. A more complex response might allow the robot to go around obstacles and continue its journey? Maybe it backs up slightly (wall-following in reverse, maybe?), and then makes an X degree turn away from the wall, runs straight for a second, and then starts wall following again.
The current ‘BackupAndTurn()’ routine takes bIsLeft, a Boolean representing the current tracking direction (left or right) and a motor speed. All it does is call RollingTurnRev(bIsLeft, 1500), where 1500 is the time in millisecond to run the motors.
RollingTurnRev() just calls RunBothMotorsMsec() with the motor speed on one side set to MAX and on the other to OFF (we know this won’t work on Wall-E2, because the wheel base is too wide – he just locks up ☹.
RollingTurnRev() is called in two places; ExecDiscManeuver() and BackupAndTurn(). BackupAndTurn() is called from 4 places; TRACKING_NEITHER/RIGHT/LEFT, and IRHomeToChgStn(). In all these cases, the robot knows which (if any) wall is closer, so it can execute the proper rolling turn
From what I see so far, it appears all these cases can be handled by a turn routine that does the following:
- Moves straight backward for just a second or so (or maybe even less)
- Makes a 45° forward rolling turn away from the nearest wall. If there is no nearest wall, go opposite the way it went last time (requires a global Boolean to save this value)
- Makes another 45 turn in the opposite direction to the first one. This will have the effect of a side-step maneuver, as shown in this post.
After this review, it was clear that all I had to do to integrate the new heading-based turn capability into Wall-E2’s OS was to replace the RollingTurnRev() function with a new ‘RollingTurn()’ function that takes flags for FWD/REV and for CCW/CW, and a parameter for the number of degrees to turn. Since I had already demonstrated all the code blocks in stand-alone test programs, all I had to do was copy the appropriate code pieces into the appropriate spots in Wall-E2’s OS, and then spiff things up a bit here and there. When I was done, I had a single function that could facilitate a range of maneuvers.
To test the newly integrated capability, I added some code to Wall-E2’s setup() function to perform a series of S-turns, each of which demonstrates a typical avoidance maneuver. For convenience, I told Wall-E2 to execute a ‘K-turn’ reversal and then S-turn his way back to me. As can be seen in the following short video, this worked fairly well!
Now that I have the basic heading-based turn capability integrated into Wall-E2, the next step will be to demonstrate that Wall-E2 can use its new superpowers to avoid obstacles in ‘the real world’ (as real as it gets for Wall-E2, anyway).