Described here is MicroPython code for programming the car (Freenove does not include MicroPython code for their ESP32 car). A good starting point is the Python Tutorial supplied by Freenove in their Basic Starter Kit (see ‘FNK0061: Basic Starter Kit for ESP32” link found at https://freenove.com/tutorial) included with this repository (see Python_Tutorial.pdf). The tutorial will help you to set up the Thonny IDE for program development and transfer of MicroPython files to the ESP32 on the car. In addition the Basic Starter Kit includes a Python folder with sample code that can help in learning MicroPython (the Basic Starter Kit includes a breadboard and components that do not come with the car, but you’ll get the idea).
Following is a list / summary of the MicroPython files:
i2c_test.py: read IR sensors and write values to screen
motor_test.py: MicroPython version of Freenove 01.1_Car_Move_and_Turn sketch
motor.py: motor commands library
pca9685.py: pca9685 commands library used for reading / writing to the i2c bus
servo_test.py: MicroPython version of Freenove 01.2_Servo sketch
servo.py: servo commands library
------Track_car.py: MicroPython version of Freenove 04.2_Track_Car sketch
Ultrasonic_car.py: MicroPython version of Freenove 02.2_Ultrasonic_Ranging_Car sketch
Ultrasonic_Ranging.py: MicroPython version of Freenove 02.1_Ultrasonic_Ranging sketch
battery_level.py: When car attached to the computer, read and display he battery level in the Thonny interface and determine that car is turned on or off. Light the blue light on the ESP32 board if car is on.
**** Camera: Note that MicroPython distribution does not provide firmware to use the camera module. Per the Freenove Python Tutorial for the camera based code use the firmware in Lemariva's Github project, MicroPython-camera-driver. Project link:https://github.com/lemariva/MicroPython-camera-driver. To date that firmware runs all the programs listed here as well as those that use the camera with no problems.
picoweb_video_v2.py : This is a demo taken from the Freenove ESP32 basics Python set of code
camera_test.py : program to take a picture with the camera and store it in jpeg format to a file on the ESP32.
camera_test_v2.py : Same as above but includes servo movement to capture 3 separate images from 3 different servo positions
**** Inertial management unit (IMU): The following programs all use the gy-521 / mpu-6050 additional card This is board is not included with the Freenove kit. However it is inexpensive and easily interfaces with the the car via the i2c extra pins included with the car.. MPU6050.py : mpu6050 commands library.
Car_test_accel_v6.py : This requires the gy-521 board that has the mpu-6050 chip. It is the motor_test.py program above but includes capture and saving of the three axis of the accelerometers and the Z axis of the gyro. The output of the accelerometers is in meters/second**2 and the gyro output is in degrees/second. From these measurements the code derives the distance that the car has traveled and the change in angle the car is pointing. Those estimates are stored to an onboard file. The program requires library Car_lib.py
Car_rotate.py : program makes use of the gyro sensor to make left and right 90 and 180 degree turns. The program requires library Car_lib.py
Track_car_v2_3.py : A version of the track_car.py noted above. However this version includes use of the pmu-6050. Additional logic is included that performs: - if no line detected, back up until a line detected - if all sensors on likely the car at an intersection, left or right 90 degree turn, or the end. See case 7 in the program for details.
Car_lib.py : Library of routines used to process mpu6050 data. They include motor_move (move the wheels), accel_calibration (calibration of the mpu6050), and GyroSensor (read the gyro sensor and derive pointing angle).
To run the code on ESP32 all the libraries should be downloaded to the car. Those libraries are:
The target code can be run from either the computer stored version or the ESP32 stored version. However regardless if running from the computer or the ESP32 the programs only load libraries that are stored on the ESP32 so ensure that the libraries used are stored there on the ESP32 on the car.
By default the boot.py file on the ESP32 is the first thing run at boot up or when the reset button is pushed. The boot.py file that is created when the MicroPython firmware is installed to the ESP32 is just some comments that are not executed. To have to car run your code at start up or by pressing the ‘reset’ button on the ESP32 the boot.py file needs to be changed to include the code to run the desired program. For example to run the line tracking code the boot.py file should be edited to include the following:
exec(open('Track_car.py').read(),globals())sudo apt-get install git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0 minicom
mkdir -p ~/esp
cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf.git
cd ~/esp/esp-idf
./install.sh all
. ./export.sh
alias get_idf='. $HOME/esp/esp-idf/export.sh'
idf.py -p /dev/ttyUSB0 flash
sudo minicom -b 115200 -8 -D /dev/ttyUSB0 < /dev/tty
chmod +x ~/etc/flash.sh
cd ~/esp
cp -r $IDF_PATH/examples/get-started/hello_world .
cd ~/esp/hello_world
idf.py set-target esp32
idf.py menuconfig
idf.py build
~/esp/flash.sh
make -C mpy-cross
cd ports/esp32
make submodules
make
cd micropython/examples/usercmodule/cppexample
make -C mpy-cross
cd ports/esp32
make submodules
make