# 3. Getting Ready ## 3.1 Install CanMV IDE
### 3.1.1 Serial driver installation (1) Connect K210 to the computer using a Type-C cable. Before installing the software, you can check if your computer already has the CH340 driver. If the computer can recognize the corresponding serial port, there is no need to install the serial port driver. You can check the ports by connecting the module to the computer and opening the **"Device Manager"**, as shown in the figure below:
(2) If the driver is not available, you can find the [Serial Driver Installation Package](Appendix.md) in the same directory as this section.
(3) The installation method is as follows: double-click to open the installation package, click **"Install"** and wait for the prompt indicating **"successful installation"**.
### 3.1.2 CanMV IDE Installation
CanMV is developed based on the OpenMV project, so CanMV IDE is essentially similar to the OpenMV IDE. The main modifications include changes in connection methods, communication protocols, and other related components. The IDE is developed using Qt Creator.
The installation process is as follows:
(1) Double-click to open the [canmv-ide-windows-v2.9.2.exe](Appendix.md) file located in the same directory as this document.
(2) Click **"Next (N)"**.
(3) Click **"Browse..."** to choose the installation path, then click **"Next (N)"**.
(4) Check the **"I accept the agreement"** option, then click **"Next (N)"**.
(5) Click **"Next (N)"**.
(6) Click **"Install"**.
(7) Wait for the installation to complete.
(8) Check **"Launch CanMV IDE"** and click **"Finish (F)"**.
(9) At this point, the installation of CanMV IDE software is complete. The software will automatically open after installation.
## 3.2 MicroPython Introduction
### 3.2.1 MicroPython Introduction
MicroPython is a streamlined and efficient implementation of the Python 3 programming language. It includes a small portion of the Python standard library and is optimized to run on microcontrollers and in constrained environments. It provides a low-level Python operating system for controlling various electronic projects.
MicroPython is rich in advanced features, such as interactive prompts, arbitrary precision integers, closures, list comprehensions, generators, and exception handling. Despite these advanced features, it is compact enough to run in just 256KB of code space and 16KB of RAM.
The goal of MicroPython is to be as compatible as possible with regular Python, allowing you to easily transition code from desktop to microcontrollers or embedded systems. This means you can use the Python language on small embedded devices.
* **Reference:**
(1) MicroPython offical website: [MicroPython - Python for microcontrollers](https://micropython.org/)
(2) MicroPython help documentation: [mPython Help Documentation](https://mpython.readthedocs.io/zh/master/index.html)
### 3.2.2 Principle of running MicroPython on K210 vision module
The firmware of the K210 Vision Module has already set up the MicroPython runtime environment. You can communicate directly using the Type-C interface and run MicroPython. The operational principle is illustrated in the following diagram:
CanMV IDE on the PC communicates with the K210 Vision Module through a serial port, transmitting the code from the editor to the module. The K210 Vision Module parses the content based on commands, invokes the MicroPython framework to execute the content, and eventually sends back runtime information and results through the serial port to the PC.
## 3.3 CanMV IDE Interface Introduction
This section provides a brief overview of the CanMV IDE interface.
### 3.3.1 CanMV IDE Interface
(1) The CanMV IDE interface (when not connected to the K210 Vision Module) is as follows:
① Menu Bar: Includes options for file operations, K210 flashing tools and visual-related tools;
② Quick Button: Provides convenient shortcuts for some commonly used development functions;
③ User Code Editing Area: Area for users to develop MicroPython programs;
④ Device Connection: The upper button is for connecting to the K210 Vision Module device, and the lower ones are for program execution and termination;
⑤ Debug Information Area: Displays debug information printed to the serial port and includes a search function;
⑥ Image Parameter Area: The upper part is the image preview area, and the lower part includes color space parameters;
⑦ Firmware Information: Displays the connected serial port number and other firmware information.
(2) The CanMV IDE interface (when connected to the K210 Vision Module) is as follows:
When connected to the K210 Vision Module, the device connection area in the bottom left corner changes to the above status.
(3) The CanMV IDE interface (when running a program after connection) is as follows:
When running a program, the device connection area in the bottom left corner changes to the above status. Press
to stop online execution.
## 3.4 CanMV IDE Basic Usage
In this section, you will learn how to import, run routines, and download them to the K210 module and SD card.
### 3.4.1 Connect K210 Vision Module
(1) Connect the PC and the K210 vision module using a Type-C data cable, as shown in the following picture:
(2) Double-click
to launch the CanMV IDE software.
(3) Click the connect icon in the lower-left corner of the device connection area to establish a connection, or use the shortcut **"Ctrl+E"**.
(4) After selecting the correct port for the K210 module, check **"Advanced Settings"** and choose **"Mode-3"** for the initial connection. Click **"OK"**.
:::{Note}
If there are multiple ports on the PC, ensure to select the correct port number for the K210 module. Otherwise, you may encounter connection issues or face difficulties in downloading programs.
:::
(5) After a successful connection, the connection icon will change to the one shown in the following picture.
### 3.4.2 Routine Test
Next, we will conduct the first test through CanMV IDE to learn importing, running online, and program downloading.
* **How to import a Routine**
(1) Through the menu bar, select **"File" -> "Open File"** to import.
(2) Open the CanMV IDE window and directly drag the program onto the interface.
After importing, the interface will look as follows:
* **Online Program Execution**
After importing the program, you can directly run it online without downloading. Click the **"Run"** button in the lower-left corner of the interface to observe the program's running effect. During execution, you will see the fill light starting to light up and blink.
To stop the program's execution, click the **"Stop"** button.
* **Program Download**
(1) Download the program into the K210 module, and it will automatically run after the module is powered up again.
:::{Note}
If an SD card is inserted into the K210 module, the program will be prioritized to be saved in the main.py file on the SD card.
:::
(2) While maintaining the connection, click on the menu bar, select **"Tools -> Save the Currently Open Script (main.py) to CanMV Cam"**.
(3) After clicking, a prompt as shown in the following picture will appear. Click **"Yes"** and wait for the completion prompt to appear.
(4) Once completed, disconnect from CanMV IDE, and the program will run automatically. The running effect will be the same as described in the previous section [3.4.2 Routine Test -> Online Program Execution](#anchor_3_4_2_2).
## 3.5 Call Custom Library File
### 3.5.1 Custom Library File Description
A Python custom library refers to Python functions or class modules that developers write according to their needs, designed to solve specific problems or accomplish particular tasks.
In this section, we will learn how to import and call custom library files in CanMV IDE.
### 3.5.2 Import the Library File
We will use the example of importing the **"LibraryDef.py"** library. There are two ways to import a custom library:
(1) Using a card reader, import the custom library file into the root directory of the TF card, and then call it after inserting the TF card.
(2) Importing through CanMV IDE. The specific steps can be found in the following text:
① After opening CanMV IDE, import the library file, connect the K210 module, and click **"Save File to CanMV Cam"**.
② In the popup window, fill in the file name and select the path in the respective fields. The library file **"LibraryDef.py"** can be found in the same directory as this document. After completing the information, click **"OK"**.
:::{Note}
The file name must include the .py extension; otherwise, it won't take effect.
:::
### 3.5.3 Call the Library File
Import the **"use_def_test"** program test file. The program file can be found in the same directory as this document.
Then, after connecting the vision module, click
, and if you see the printed information in the **"Serial Terminal"** as shown in the picture below, it means the import was successful.
## 3.6 Learn Factory Firmware Burning
Before leaving the factory, the K210 vision module has already been programmed with the factory firmware. If you encounter unexpected firmware damage during subsequent use or wish to upgrade to a newer version of the firmware, you can refer to the following tutorial.
The K210 vision module features the K210 microcontroller. Therefore, the operations on it are equivalent to reprogramming the MicroPython firmware onto the K210 MCU.
### 3.6.1 Burn Through Kflash GUI
(1) In the [kflash_gui_v1.6.5_2_windows.7z](Appendix.md) burning tool compression package in this section's directory, extract it and find the [Kflash_gui.exe](Appendix.md) software, then open it.
(2) Choose the corresponding options as shown in the following picture. The firmware is located in the same directory as this course (Hiwonder_K210_canmv_V1_1.bin). After selecting, click **"Download"** to start downloading the firmware.
(3) Wait for the successful completion of the downloading process.
### 3.6.2 Burning Effect
When the K210 is powered on, you should see the vision module displaying the current captured image.
(1) Press the **K1** button to toggle the on/off state of the fill light.
(2) Press the **K2** button to toggle the on/off state of the blue LED light.
(3) When the blue LED light is on, the current image will freeze, and the message **"Paint mode, Press K2 to exit"** will be displayed on the screen. At this point, you can draw lines freely on the touchscreen. Press the **K2** button again to clear the screen, and the real-time image display will resume with the blue LED light turned off.
## 3.7 MicroPython API Interface Specification
### 3.7.1 Peripheral Interface
The peripherals of the WonderMV Vision Module can be accessed through the HiWonder library. The following are the interface specifications for the HiWonder library.
* **Button**
| hiwonder . hw_key() | |
|---|---|
| Function Description | Returns a button object. |
| Parameter List | key_num:(1 for SW1 button, 2 for SW2 button) |
| Return Value | 1 for pressed, 0 for released |
| Example | hw_key() . key_scan( key_num ) |
| Note | / |
| hiwonder . fill_light() | |
|---|---|
| Function Description | Returns a fill light control object. |
| Parameter List | key_num:(1 for SW1 button, 2 for SW2 button) |
| Return Value | onoff:1 for on, 0 for off |
| Example | fill_light() . fill_onoff( onoff )) |
| Note | / |
| hiwonder . hw_led() | |
|---|---|
| Function Description | Returns an LED light control object to control the on/off state of the LED. |
| Parameter List | onoff:1 for on, 0 for off |
| Return Value | / |
| Example | led_onoff( onoff ) |
| Note | / |
| hiwonder . hw_i2c() | |
|---|---|
| Function Description | Scan the IIC bus for slave devices and return an IIC peripheral control object. |
| Parameter List | onoff:1 for on, 0 for off |
| Return Value | A list object, containing all the slave addresses detected during the IIC bus scan. |
| Example | hw_i2c . scan() |
| Note | Note: The LCD touch and the external IIC interface share the same IIC bus. Even if there is no IIC device connected, you may still detect one IIC slave address, which corresponds to the touch screen. Ensure to select the correct IIC slave address when sending data. |
| hw_i2c . readfrom_into( addr , buf , stop=True ) | |
|---|---|
| Function Description | Read data and place it into the 'buf' variable. |
| Parameter List | addr: Slave address. buf: bytearray type, with defined length to store the read data. stop: Whether to generate a stop signal. Reserved; currently, only the default value 'True' can be used. |
| Return Value | A list object containing all the slave addresses detected during the scan. |
| Example | / |
| Note | / |
| hw_i2c . readfrom(addr, len, stop=True) | |
|---|---|
| Function Description | Read data and place it into the 'buf' variable. |
| Parameter List | addr: Slave address len: Type, defined length to store the read data. stop: Whether to generate a stop signal. Reserved; currently, only the default value 'True' can be used. |
| Return Value | The read data, in bytes type. |
| Example | / |
| Note | / |
| hw_i2c . readfrom_mem( addr , memaddr , nbytes , mem_size=8 ) | |
|---|---|
| Function Description | Read data and place it into the 'buf' variable. |
| Parameter List | addr: Slave address. memaddr: Slave register address. nbytes: Length to read. mem_size: Register width, default is 8 bits. |
| Return Value | Address value |
| Example | / |
| Note | / |
| hw_i2c . readfrom_mem_into(addr, memaddr, buf, mem_size=8) | |
|---|---|
| Function Description | Read the value of the slave register into the specified variable. |
| Parameter List | addr: Slave address. memaddr: Slave register address. buf: bytearray type, with defined length to store the read data. mem_size: Register width, default is 8 bits. |
| Return Value | Returns the value of the slave register. |
| Example | / |
| Note | / |
| hw_i2c . writeto(addr, buf, stop=True) | |
|---|---|
| Function Description | Write data to the target address. |
| Parameter List | addr: Slave address. buf: Data to be sent. stop: Whether to generate a stop signal. Reserved; currently, only the default value 'True' can be used. |
| Return Value | Number of bytes successfully sent. |
| Example | / |
| Note | / |
| hw_i2c . writeto_mem(addr, memaddr, buf, mem_size=8) | |
|---|---|
| Function Description | Write data to the slave register. |
| Parameter List | addr: Slave address. memaddr: Slave register address. buf: Data to be written. mem_size: Register width, default is 8 bits. |
| Return Value | / |
| Example | / |
| Note | / |
| hiwonder . hw_uart() | |
|---|---|
| Function Description | Read UART information. |
| Parameter List | addr: Slave address. memaddr: Slave register address. buf: Data to be written. mem_size: Register width, default is 8 bits. |
| Return Value | Returns a UART object. |
| Example | / |
| Note | / |
| hw_uart() . send_bytearray( buf ) | |
|---|---|
| Function Description | Send buf variable data |
| Parameter List | buf: Data to be sent |
| Return Value | Bytearray type |
| Example | / |
| Note | / |
| hw_uart() . rec_bytes() | |
|---|---|
| Function Description | Read from the serial port. |
| Parameter List | / |
| Return Value | Bytearray type |
| Example | / |
| Note | Note: Read up to 200 bytes at a time, which is generally sufficient for use. |
| cmath | |
|---|---|
| cmath.cos(z) | Returns the cosine of z. |
| cmath.exp(z) | Returns the exponential of z. |
| cmath.log(z) | Returns the natural logarithm of z. Branch cut along the negative real axis. |
| cmath.log10(z) | Returns the base-10 logarithm of z. Branch cut along the negative real axis. |
| cmath.phase(z) | Returns the phase angle of the complex number "z" in the range (-pi, +pi). |
| cmath.polar(z) | Returns the polar form of z as a tuple. |
| cmath.rect(r, phi) | Returns a complex number with modulus r and phase phi of z. |
| cmath.sin(z) | Returns the sine of z. |
| cmath.sqrt(z) | Returns the square root of z. |
| cmath.e | Base of the natural logarithm. |
| cmath.pi | Ratio of the circumference of a circle to its diameter. |
| gc | |
|---|---|
| gc.enable() | Enable automatic waste collection. |
| gc.disable() | Allocation of heap memory is still possible after disabling, and waste collection can still be manually initiated using gc.collect(). |
| gc.collect() | Run waste collection. |
| gc.mem_alloc() | Return the number of bytes allocated in the heap RAM. |
| gc.mem_free() | Return the number of bytes available in the heap RAM; return -1 if the remaining waste amount is unknown. |
| math | |
|---|---|
| math.acos(x) | Return the arccosine value of x. |
| math.acosh(x) | Return the inverse hyperbolic cosine value of x. |
| math.asin(x) | Return the arcsine value of x. |
| math.asinh(x) | Return the inverse hyperbolic sine value of x. |
| math.atan(x) | Return the arctangent value of x. |
| math.atan2(y, x) | Return the principal value of the arctangent of y/x. |
| math.atanh(x) | Return the inverse hyperbolic tangent value of x. |
| math.cos(x) | Return the cosine value of x. |
| math.cosh(x) | Return the hyperbolic cosine value of x. |
| math.degrees(x) | Convert radians x to degrees. |
| math.exp(x) | Return the exponential value of x. |
| math.fabs(x) | Return the absolute value of x. |
| math.fmod(x, y) | Return the remainder of x/y. |
| math.frexp(x) | Decompose a floating-point number into a tuple of its mantissa and exponent, such that x == m * 2 ** e holds true. If x == 0, the function returns (0.0, 0). Otherwise, the relationship 0.5 <= abs(m) < 1 holds true. |
| math.isnan(x) | Return True if x is not a number. |
| math.ldexp(x, exp) | Return x multiplied by (2 ** exp). |
| math.log(x) | Return the natural logarithm of x. |
| math.log10(x) | Return the base-10 logarithm of x. |
| math.log2(x) | Return the base-2 logarithm of x. |
| math.modf(x) | Return a tuple of two floating-point numbers, the fraction and integer part of x. Both return values have the same sign as x. |
| math.pow(x, y) | Return x raised to the power of y. |
| math.radians(x) | Convert degrees x to radians. |
| math.sin(x) | Return the sine value of x. |
| math.sinh(x) | Return the hyperbolic sine value of x. |
| math.sqrt(x) | Return the square root of x. |
| math.tan(x) | Return the tangent value of x. |
| math.tanh(x) | Return the hyperbolic tangent value of x. |
| math.trunc(x) | Return an integer, rounding "x" towards zero. |
| uos | |
|---|---|
| uos.urandom(n) | Return a bytes object containing n random bytes. Whenever possible, it is generated by the hardware random number generator. |
| uos.chdir(path) | Change the current directory. |
| uos.getcwd() | Get the current directory. |
| uos.listdir([dir]) | List the current directory if no parameters are provided. Otherwise, list the given directory. |
| uos.mkdir(path) | Create a new directory. |
| uos.rmdir(path) | Delete a directory. |
| uos.remove(path) | Delete a file. |
| uos.rename(old_path,new_path) | Rename a file. |
| uos.stat(path) | Get the status of a file or directory. |
| FPIOA() | |
|---|---|
| Function Description | Return an FPIOA object. |
| Parameter List | / |
| Return Value | FPIOA object |
| Example | / |
| Note | / |
| FPIOA() . set_function(pin, func) | |
|---|---|
| Function Description | Set the peripheral function corresponding to the pin(pin mapping) |
| Parameter List |
pin: Pin number, ranging from 0 to 47. Please refer to the circuit diagram for specific pin connections. func: Peripheral function, pass an integer value. You can check the peripheral function table by using fm.fpioa.help() or refer to the table below. |
| Return Value | FPIOA object |
| Example | / |
| Note | / |
| FPIOA() . get_Pin_num(func) | |
|---|---|
| Function Description | Get which pin a peripheral is mapped to. |
| Parameter List | func: Peripheral function, pass an integer value. You can check the peripheral function table by using fm.fpioa.help() or by referring to the table below. |
| Return Value | Function List |
| Example | / |
| Note | / |
| Peripheral Fucntion(func) | Brief Description |
|---|---|
| ...... | ...... |
| GPIOHS0 | GPIO High speed 0 |
| GPIOHS1 | GPIO High speed 1 |
| GPIOHS2 | GPIO High speed 2 |
| GPIOHS3 | GPIO High speed 3 |
| GPIOHS4 | GPIO High speed 4 |
| GPIOHS5 | GPIO High speed 5 |
| GPIOHS6 | GPIO High speed 6 |
| GPIOHS7 | GPIO High speed 7 |
| GPIOHS8 | GPIO High speed 8 |
| GPIOHS9 | GPIO High speed 9 |
| GPIOHS10 | GPIO High speed 10 |
| GPIOHS11 | GPIO High speed 11 |
| GPIOHS12 | GPIO High speed 12 |
| GPIOHS13 | GPIO High speed 13 |
| GPIOHS14 | GPIO High speed 14 |
| GPIOHS15 | GPIO High speed 15 |
| GPIOHS16 | GPIO High speed 16 |
| GPIOHS17 | GPIO High speed 17 |
| GPIOHS18 | GPIO High speed 18 |
| GPIOHS19 | GPIO High speed 19 |
| GPIOHS20 | GPIO High speed 20 |
| GPIOHS21 | GPIO High speed 21 |
| GPIOHS22 | GPIO High speed 22 |
| GPIOHS23 | GPIO High speed 23 |
| GPIOHS24 | GPIO High speed 24 |
| GPIOHS25 | GPIO High speed 25 |
| GPIOHS26 | GPIO High speed 26 |
| GPIOHS27 | GPIO High speed 27 |
| GPIOHS28 | GPIO High speed 28 |
| GPIOHS29 | GPIO High speed 29 |
| GPIOHS30 | GPIO High speed 30 |
| GPIOHS31 | GPIO High speed 31 |
| GPIO0 | GPIO pin 0 |
| GPIO1 | GPIO pin 1 |
| GPIO2 | GPIO pin 2 |
| GPIO3 | GPIO pin 3 |
| GPIO4 | GPIO pin 4 |
| GPIO5 | GPIO pin 5 |
| GPIO6 | GPIO pin 6 |
| GPIO7 | GPIO pin 7 |
| UART1_RX | UART1 Receiver |
| UART1_TX | UART1 Transmitter |
| UART2_RX | UART2 Receiver |
| UART2_TX | UART2 Transmitter |
| UART3_RX | UART3 Receiver |
| UART3_TX | UART3 Transmitter |
| ...... | ...... |
| GPIO(ID, MODE, PULL, VALUE) | |
|---|---|
| Function Description | Return a GPIO control object. |
| Parameter List |
ID:GPIO pin used (be sure to use constants from GPIO to specify) MODE:GPIO mode. GPIO.IN input mode. GPIO.OUT output mode. PULL:GPIO pull-up/pull-down mode. GPIO.PULL_UP pull-up. GPIO.PULL_DOWN pull-down. GPIO.PULL_NONE neither pull-up nor pull-down. |
| Return Value | GPIO control object. |
| Example | / |
| Note | / |
| GPIO.value([value]) | |
|---|---|
| Function Description | Modify or read the GPIO pin state. |
| Parameter List | [value]: Optional parameter, 1 for high level; 0 for low level; if this parameter is empty, it returns the current state of the GPIO pin. |
| Return Value | If the [value] parameter is empty, it returns the current state of the GPIO pin. |
| Example | / |
| Note | / |
| GPIO.irq(CALLBACK_FUNC, TRIGGER_CONDITION, GPIO.WAKEUP_NOT_SUPPORT, PRORITY) | |
|---|---|
| Function Description | Configure an interrupt handler to be called when the trigger source of the pin is in an active state. If the pin mode is pin.in, the trigger source is the external value on the pin. |
| Parameter List |
CALLBACK_FUNC: Interrupt callback function to be called when the interrupt is triggered. TRIGGER_CONDITION: Interrupt trigger mode for the GPIO pin. GPIO.IRQ_RISING: Rising edge trigger GPIO.IRQ_FALLING: Falling edge trigger GPIO.IRQ_BOTH: Both rising and falling edges trigger GPIO.WAKEUP_NOT_SUPPORT: Set to interrupt mode, default to GPIO.WAKEUP_NOT_SUPPORT. PRORITY: Interrupt priority. |
| Return Value | If the [value] parameter is empty, it returns the current state of the GPIO pin. |
| Example | / |
| Note | / |
| GPIO.disirq() | |
|---|---|
| Function Description | Disable interrupts. |
| Parameter List | / |
| Return Value | / |
| Example | / |
| Note | / |
| GPIO.mode(MODE) | |
|---|---|
| Function Description | Set GPIO input/output mode. |
| Parameter List |
MODE: Mode selection. GPIO.IN: Input mode GPIO.PULL_UP: Pull-up input mode GPIO.PULL_DOWN: Pull-down input mode GPIO.OUT: Output mode |
| Return Value | / |
| Example | / |
| Note | / |
| KPU.load(offset, file_path) | |
|---|---|
| Function Description | Load a model from flash or the file system. |
| Parameter List |
The parameters "offset" and "file_path" are mutually exclusive. Pass one of them directly as an argument, without any keywords. offset: Model's offset in flash, such as 0xd00000 indicating the model is burned starting from 13M, and 0x300000 indicating it's at 3M in flash. file_path: The file name of the model in the file system, such as "/sd/xxx.kmodel". |
| Return Value | If loaded correctly, it returns the KPU network object (kpu network). Otherwise, it raises an error. |
| Example | / |
| Note | / |
| KPU.load_flash(model_addr, is_dual_buf, batch_size, spi_speed) | |
|---|---|
| Function Description | Load a model from flash or the file system. |
| Parameter List |
model_addr: The offset address of the preprocessed model burned into the flash memory. Note that a preprocessed model file description is required here. is_dual_buf: 0 for single-buffer loading, which dynamically loads the model file with less RAM usage and slower speed. 1 for enabling dual-buffer loading, requiring more RAM and relatively faster execution speed. batch_size: When is_dual_buf is set to 1, batch_size needs to be configured, with a recommended value range of 0x4000 to 0x10000, which can determine the optimal value for the model. If is_dual_buf is 0, set batch_size to 0. spi_speed: When loading the model file using SPI flash, we temporarily set the flash to high-speed mode and set the required SPI clock frequency. This value should be <= 80000000 (actual frequency, set value may not equal actual frequency). |
| Return Value | If loaded correctly, it returns the KPU network object (kpu network). Otherwise, it raises an error. |
| Example | / |
| Note | / |
| KPU.init_yolo2(kpu_net, threshold, nms_value, anchor_num, anchor) | |
|---|---|
| Function Description | This function initializes parameters for the YOLOv2 network model. It is only used when employing YOLOv2. |
| Parameter List |
kpu_net: The KPU network object, which is the model object loaded by KPU.load(). Threshold: Probability threshold. Only when the probability of an object exceeds this value will the result be output. Value range: [0, 1]. nms_value: Box IoU threshold. To prevent multiple boxes from being drawn around the same object, if the intersection over union (IoU) of two boxes drawn around the same object is less than this value, only the one with the higher probability will be retained. anchor_num: Number of anchor points. It is fixed to len(anchors)//2. anchor: Anchor parameters consistent with the model parameters. This parameter is fixed for the same model and determined during model training. It cannot be changed to other values. |
| Return Value | success:bool type, indicating whether the operation was successful. |
| Example | / |
| Note | / |
| KPU.deinit(kpu_net) | |
|---|---|
| Function Description | Release the memory occupied by the model while keeping the variable intact. You can use del kpu_net_object to delete it. |
| Parameter List | kpu_net: KPU network object |
| Return Value | success:bool type, indicating whether the operation was successful. |
| Example | / |
| Note | / |
| KPU.run_with_output(img_obj) | |
|---|---|
| Function Description | Run object detection model using KPU. |
| Parameter List | img_obj:Image object. |
| Return Value | / |
| Example | / |
| Note | / |
| KPU.regionlayer_yolo2() | |
|---|---|
| Function Description | Obtain detection results. |
| Parameter List |
The data includes the following six components: x, y, w, h: Representing the top-left coordinates (x, y) of the bounding box, as well as its width (w) and height (h). class: Class index. prob: Probability value, ranging from 0 to 1. |
| Return Value | Returns a two-dimensional list, each sublist represents a recognized target object, and the target object information list package. |
| Example | / |
| Note | / |
| sensor.reset([, freq=24000000[, set_regs=True[, dual_buff=False]]]) | |
|---|---|
| Function Description | Reset and initialize a monocular camera. |
| Parameter List |
freq: Set the camera clock frequency. A higher frequency results in a higher frame rate, but it may degrade image quality. The default is 24MHz. If there are color spots in the camera (ov7740), you can reduce it, for example, to 20MHz. set_regs: Allows the program to write to camera registers. By default, it's set to True. If you need to customize the reset sequence, you can set it to False, and then use the sensor.__write_reg(addr, value) function to customize writing register sequences. dual_buff: Default is False. Allows the use of double buffering, which increases the frame rate but also increases memory usage (approximately 384KiB). choice: Specifies the type of camera to search for: ov type (1), gc type (2), mt type (3). If this parameter is not passed, it searches for all types of cameras. |
| Return Value | / |
| Example | / |
| Note | / |
| sensor.set_framesize(framesize[, set_regs=True]) | |
|---|---|
| Function Description | Used to set the frame size of the camera output. The development board's screen is configured with a resolution of 320x240 pixels. It is recommended to set it to QVGA format. |
| Parameter List |
framesize: Frame size. set_regs: Allows the program to write to camera registers. By default, it's set to True. If you need to customize the frame size setting sequence, you can set it to False, and then use the sensor.__write_reg(addr, value) function to customize writing register sequences. |
| Return Value | success:bool type,indicating whether the operation was successful. |
| Example | / |
| Note | / |
| sensor.run(enable) | |
|---|---|
| Function Description | Image capture functionality control |
| Parameter List | enable: 1 to start capturing images; 0 to stop capturing images. |
| Return Value | success:bool type,indicating whether the operation was successful. |
| Example | / |
| Note | / |
| sensor.snapshot() | |
|---|---|
| Function Description | Take a photo using the camera. |
| Parameter List | / |
| Return Value | img: The returned image object |
| Example | / |
| Note | / |
| sensor.shutdown(enable) | |
|---|---|
| Function Description | Close camera. |
| Parameter List | enable: True means turning on the camera False means turning off the camera |
| Return Value | / |
| Example | / |
| Note | / |
| sensor.skip_frames(n, [, time]) | |
|---|---|
| Function Description | Skip a specified number of frames or skip images within a specified time interval to allow the camera image to stabilize after changing camera settings. |
| Parameter List |
n: Skip n frames of images. time: Skip images within the specified time interval, in milliseconds. If neither n nor time is specified, this method skips n frames but returns after waiting for time milliseconds. |
| Return Value | / |
| Example | / |
| Note | / |
| lcd.init(type=1, freq=15000000, color=lcd.BLACK, invert = 0, lcd_type = 0) | |
|---|---|
| Function Description | Initialize the LCD screen display. |
| Parameter List |
type: Device type (reserved for future use), can be left as default by the user. freq: Frequency of the LCD (referring to the communication speed of the SPI). color: Initial color for the LCD initialization. It can be a 16-bit RGB565 color value, such as 0xFFFF, or an RGB888 tuple, such as (236, 36, 36). Default is lcd.BLACK. invert: Invert the display of the LCD. lcd_type: LCD type, can be left as default by the user. |
| Return Value | / |
| Example | / |
| Note | / |
| lcd.display(image, roi=Auto, oft=(x, y)) | |
|---|---|
| Function Description | Display an image (GRAYSCALE or RGB565) on the LCD screen. |
| Parameter List |
Image: Image object. roi: A tuple representing the region of interest (ROI) rectangle ((x, y, w, h)). If not specified, it defaults to the image rectangle. oft: Set the offset coordinates. If these coordinates are set, the surrounding area will not be automatically filled. |
| Return Value | / |
| Example | / |
| Note | / |
| lcd.clear(color) | |
|---|---|
| Function Description | Clear the LCD screen to black or a specified color. |
| Parameter List | color: The color used for initializing the LCD. It can be a 16-bit RGB565 color value, such as 0xFFFF, or an RGB888 tuple, such as (236, 36, 36). |
| Return Value | / |
| Example | / |
| Note | / |
| lcd.rotation(dir) | |
|---|---|
| Function Description | Set the orientation of the LCD screen. |
| Parameter List | dir: Value range [0, 3], representing the clockwise rotation direction from 0 to 3. |
| Return Value | The current direction, with values ranging from 0 to 3. |
| Example | / |
| Note | / |
| lcd.mirror(invert) | |
|---|---|
| Function Description | Set whether the LCD screen is displayed in mirror mode. |
| Parameter List | invert: Whether to display the LCD screen in mirror mode. Set to True or False. |
| Return Value | The current setting for mirror display. Returns True or False. |
| Example | / |
| Note | / |
| lcd.fill_rectangle(x, y, w, h, color) | |
|---|---|
| Function Description | Fill the specified area of the LCD. |
| Parameter List |
X: Starting coordinate x. Y: Starting coordinate y. w: Fill width. h: Fill height. color: Fill color, which can be a tuple, such as (255, 255, 255), or an RGB565 uint16 value, such as red 0x00F8. |
| Return Value | The current setting for mirror display. Returns True or False. |
| Example | / |
| Note | / |
| touchscreen . init(i2c=None, cal=None) | |
|---|---|
| Function Description | Initialize the touchscreen. |
| Parameter List |
i2c: Currently supports touchscreens with I2C communication. Pass an instance object of the I2C. This parameter may be renamed or removed in the future. cal: Calibration data, represented as a tuple of 7 integers. This data can be obtained through the touchscreen.calibrate() function. |
| Return Value | / |
| Example | / |
| Note | / |
| touchscreen . read() | |
|---|---|
| Function Description | Read the current screen status and the coordinate value of the pressed point |
| Parameter List | / |
| Return Value |
A tuple consisting of 3 integer values (status, x, y), and note that this value will maintain the previous status: status: The status, with possible values: - touchscreen.STATUS_PRESS: The screen is pressed - touchscreen.STATUS_MOVE: The screen is being pressed and moved - touchscreen.STATUS_RELEASE: The screen is released (not being pressed) x: The x-axis coordinate. y: The y-axis coordinate. |
| Example | / |
| Note | / |
(2) Follow the on-screen instructions in the images to proceed with the installation.
### 3.8.2 K210 Data Transmission and Reception Test
(1) Connect the serial port of the K210 Vision Module to the USB to TTL converter tool. Power the other end with a Type-C connector. The wiring is as shown in the following diagram:
:::{Note}
Connect the RX of the K210 to the TX of the TTL tool, and the TX of the K210 to the RX, otherwise it may affect normal communication.
:::
(2) We'll use the program provided in [4. Basic Hardware Course->4.9 UART Serial Communication Experiment](4.Basic_Hardware_Course.md#uart-serial-communication-experiment) on the K210 side as an example. The program will send the following string.
For instructions on how to run or download the program, please refer to [3.4 CanMV IDE Basic Usage](#anchor_3_4).
(3) After opening the Serial Port Assistant, select the port number of the Serial Port Assistant (the port where the USB to TTL converter tool is plugged in). Set the baud rate to 115200, and choose Hex for both receive and send settings.
:::{Note}
Do not select the Type-C interface used for connecting the K210 to CanMV, otherwise it will be occupied.
:::
For example, in this test, the Type-C interface is COM34, while the USB to TTL converter tool is COM25. Port numbers may vary depending on the user's actual situation.
(4) After clicking
, you should be able to see the string message sent by the K210 module.
(5) Now, we can try sending string messages to the K210 Vision Module to test its receiving function. You can send a string of characters using the Serial Port Assistant. As shown in the figure below:
(6) In the serial terminal of the CanMV IDE interface, you should see that the K210 has successfully received the data, as shown in the figure below:
## 3.9 Usage of TF Card
:::{Note}
Do not insert or remove the TF card while the K210 module is powered on.
:::
The K210 module has a TF card slot located on the right side of the module. We recommend users to use first-tier brand TF cards such as SanDisk, with a capacity of up to 64GB.
After the K210 module detects the TF card upon power-up, it will automatically mount it to the K210's file system and prioritize reading and writing operations from/to the TF card. Therefore, if you place boot.py on the TF card and insert it into the module, the boot.py from the TF card will be executed upon powering up the module.
Small files such as Python scripts within a few hundred KB can be transferred via CanMV IDE, while larger files such as AI models can be copied using a card reader.
### 3.9.1 Detection via CanMV IDE
After inserting the TF card into the K210 module, you can open CanMV IDE, connect to the port, create a new program file, and then enter the following code:
python
import os
print(os.listdir('/'))
Click on
, then view the printed information in the serial terminal.
If there is no prompt indicating successful mounting, it may be due to the TF card not being detected or the TF card format being incorrect, leading to failure to recognize it. You may need to remove the TF card, clean the gold fingers of the TF card, or initialize the TF card. The method for initializing the TF card is as follows:
### 3.9.2 TF Card Initialization (Only when unable to use normally)
You can use the TF card initialization tool for this purpose, which can be found in the same directory as this program.
(1) Double-click to install it, follow the prompts, and wait for the installation to complete.
(2) After opening the TF card formatting tool, select the drive letter of the TF card, and then click on **"Format"**.
