In the setup portion of the code, we assign all the pins to the appropriate sensors, turn the camera on, set the camera format to JPEG, set up the IMU, set all the motors to stopped, start an initial timer called command_timer, and connect to WiFi.
Regular operation of LASSIE works by maintaining a queue data structure, which contains all the commands that LASSIE should execute. The queue operates in a first in, first out order, so whenever a command is added, it will be completed after all the commands which were already in the queue, but before any commands added after it. Initially, the queue is empty, and while the queue is empty LASSIE will continuously check for new commands by making GET requests to the server. If no commands are received, it will continue to wait for new commands. When the GET request does return a string, it will send that string to the parseCommands method, which will add all the commands received to the queue. Then, LASSIE will begin by executing the next command in the queue. While that command is being executed, LASSIE will check for new commands by making GET requests, and any new commands will be added to the queue. Once the command is finished, it will remove that command from the queue and move on to the next one. Commands control all regular operations of LASSIE, including forward and backward movement, turns, taking images, making sensor readings, and playing audio clips.
The primary loop is located in the loop function, which checks if the time since command_timer started has exceeded 1000ms. If it has, the code enters the controller() function. Inside the controller function, it checks the queue for any commands, and if the queue is not empty, it will begin executing the next command in the queue. If there is no command, then it will call the lookup function to check for new commands from the server, then return to the loop function. If there is a command, the ESP32 will react to that command, check for any remaining commands from the last lookup() call, react to those appropriately, and run a lookup() call if there are no remaining commands. With this format, LASSIE can respond to multiple commands sent from the server all in one go, thus creating a smooth operating system.
If the command is to move in a certain way, the code will parse a number after the command “move forward 50” for example, and move forward in 50 intervals of about 20 ms each.
To move forward, the ESP32 digital writes the designated RF (Right Forward) and LF (Left Forward) motors as high, and the RB (Right Backward) and LB (Left Backward) motors as low. The reverse of that moves the chassis backward. To turn right, the RB and LF motors are high and the RF and LB motors are low. The reverse of that turns the chassis to the left. After each movement, the stopper() function sets all the motors to low to stop LASSIE. While LASSIE is moving forward, if the distance to an object is less than 15 cm as read by the ultrasonic distance sensor, the stopper() function will stop LASSIE.
If the command is to read the temperature, the IMU reads the temperature data (in Celsius) and sends a POST request with that data up to the server where it is saved in a database.
If the command is to read the distance to an object, the ultrasonic distance sensor’s trigPin is set to low and the echoPin is set to high, the distance is read, and the data is sent to the server via a POST request where it is then saved in a database.
If the command is to take a picture, the camera will take a picture in JPEG format. The JPEG data is then base64 encoded as it is being gathered. The encoded data is sent to the server via POST request using JSON. Once on the server, the data is decoded and saved in a database.
If the command is to play an alert, the MP3 player is instructed to play the appropriate audio recording from the SD card.
Once it makes it through the controller() function, the command_timer is reset, and the loop function runs again.
When there are no new commands coming from the server, the motors and sensors are all set to low to conserve battery.