A fun Spike project is a self-parking car. Such a car must have two wheels connected to motors, a balancing ball, a big frame for the hub, two cameras to measure distance, and a light camera to measure reflection. The reflection camera must go in the front of the car so it can easily follow a black track on the floor. The front distance camera is used to prevent the car from bumping inside the parking lot. The side distance camera is used to know when to turn into the parking lot. Let’s see how this all works below!


First, we define the ports where the motors for the wheels connect, in this case, A and B. Then we set the speed for the car at 30%. Then we start moving along the edge of the black line until we find the parking lot. The car seeks and follows the black line by checking how much light is reflected from the floor. A completely white surface would reflect 100% of the light back. A completely black surface would not reflect any light back. Therefore, in order to follow the edge of the black line we want the light camera to reflect about 50% of the light. When this happens the car should go straight. When the car is on the light portion of the floor it should turn right. This is obtained by moving the car with some power to the right. That’s what the first start moving block does. When the car is on the light part of the floor far away from the black track, the reflected light minus 50 is about 40, so it will turn right. When the car is mostly on the light part of the floor but catches a little bit of the black track, the reflected light minus 50 is about 20, so the car will turn right but less so. When the car is on top of the black track, the reflected light minus 50 is about -40, so it will turn to the opposite side (left). When the car is on the edge of the black track, the reflected light is about 50, so the car will not turn. A reflected light of 50% is the target the car should follow. The program above keeps adjusting the movement of the car until the side distance camera is less than 30 centimeters of something, which allows for finding the parking lot. Finally, we realized that turning with a power of 40 would be too much and the car would have a hard time to stabilize on the track, so we divide that by 1.5 to smooth the movement of the car.
Second, the car must start turning right about 90 degrees. We use the yaw angle to do this. The way angle measures how many degrees the hub turned from a given reference using the hub’s gyroscope. We start by setting it to zero. Then we keep on turning until the yaw angle reaches close to 90 degrees, at which point we stop the car from moving. To turn the car, we move the wheels at different speeds. In the code above we move the left wheel at speed 30 and the right wheel at speed 15 and, therefore, the car will turn right. This will make the car turn smoothly into the parking lot.
Finally, the car drives straight inside the parking lot until the front distance camera senses the wall, at which point the car stops and displays a check mark indicating that it is done! The full demonstration is below!