MicroPython for Freenove 4WD Car

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:

**** 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.

**** 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.

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())

Useful Links

Building ESP32 IDF on Ubuntu Linux

  1. Get the tools:
    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
  2. Create the ESP32 IDF diretory:
    mkdir -p ~/esp
  3. Get the ESP32 IDF:
    cd ~/esp
    git clone --recursive https://github.com/espressif/esp-idf.git
  4. Install the IDF:
    cd ~/esp/esp-idf
    ./install.sh all
  5. Export the IDF symbols:
    . ./export.sh
  6. Add the following to ~/.bashrc:
    alias get_idf='. $HOME/esp/esp-idf/export.sh'
  7. Create ~/esp/flash.sh with the following lines:
    idf.py -p /dev/ttyUSB0 flash
    sudo minicom -b 115200 -8 -D /dev/ttyUSB0 < /dev/tty
  8. Enable execution of flash.sh
    chmod +x ~/etc/flash.sh
  9. Build Hello World:
    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
  10. Download Hello World into Flash:
    ~/esp/flash.sh

Building MicroPython on Ubuntu Linux

  1. Build the ESP32 IDF, see previous section
  2. make -C mpy-cross
  3. cd ports/esp32
    make submodules
    make

Building MicroPython C++ Extensions on Ubuntu Linux

  1. Set example directory:
    cd micropython/examples/usercmodule/cppexample
  2. Build the ESP32 IDF, see previous section
  3. make -C mpy-cross
  4. cd ports/esp32
    make submodules
    make