Team Alpha
Software
Welcome to Software
Software is really the backbone of the whole project and ties together each element. The goal was to have a single program uploaded onto the micro controller which contained all code relating to obstacle avoidance, white line following and combat.
The first stage was obstacle avoidance. The device has 2 IR sensors on the front meaning that there are 4 possible outcomes that need to be programmed: left on, right on, both on and both off. The simplest way we can code this is too have an if, if else statement. The mouse should swerve left if only the right is on and right if only the left is on, but that raises the question what if both are on which way should the mouse turn? To solve this dilemma a new variable called “last_direction” was added this saved the direction the mouse was traveling so that the code knows which way to turn.

This code was repeated for the touch bars as well except a different function was called. This time making the mouse reverse away from what it hit.

The next stage was the white line following, originally it was planned to have 3 sensors however during testing this made the mouse very jittery, so 5 sensors were used in order to increase the sensitivity and allow for finer control of the motors. Because of the 5 seasons it meant there was 32 different combinations of them being on and off; fortunately not all of these had to be coded as some would be near impossible for the sensors to actually pick up and even if they did the code would carry on what it was doing until an input condition was recognise. For example, if the motor was going full speed forward then an input that hadn’t been coded was detected it would keep going forward and loop back through the code until a new recognised input was detected.
The hard part was finding a way to structure the code that wouldn’t take up too much memory space and allow for the mouse to cycle through the code as quick as possible. If statements were used each would have to compare 5 different parts together as shown below.

This bit of code would have to repeat multiple times with the greater than symbols flipped around for each different scenario which would make the code hard to follow and take lots of processing time.
Instead each sensor was given a power of two; sensor 1 being 20, sensor 2 being 21 and so on. This gives us the values 1, 2, 4, 8 and 16. If a sensor is above the threshold its number is added to a new variable, so if just sensor one is on, the output is 1, but if 1 and 2 are on the output is 3 and if all are on the output is 31. By doing this each different input has a unique number associated to it. This allows a switch case to be used which is much simpler to look at and runs faster as less comparisons are needed. A small section of that is shown here.

The only thing left is to work out the outputs. In order to make the line following as smooth as possible the motors speed needs to be adjusted. In order to do this pulse width modulation is required. This involves turning on and off the motor quickly so that the total power is reduced and so is the speed. For this to work effectively the functions left, more_left and hard_left was created (and the same for rights) each changed the speed of the left or right wheel by differing amounts to create sharper or gradual turns.
The last issue for line following was what happens if there is no line. This problem was again solved by the last_direction variable which would save the last direction the robot was travelling in, this would make it so that if it over shot one of the corner it would have already been tuning the corner so would be able to complete it without losing itself in the black void.
The final part was combat, but this was never started due to COVID-19. The plan was to combine both the previous sections into one as the code would be very similar with a few alterations to what happens when different input scenarios are detected, the main 2 being staying inside the box rather than following the line and not avoiding what it detects and instead charging at it.