1. Goal
Learn the color block detection function of the AI vision perception module and use CoCube to react to different color blocks.
2. Materials

3. Software Platform
4. Algorithm Knowledge
Reference Tutorial
Documentation: Algorithm Introduction — Color Block Detection (Blob)
The running status of Sengo1's screen is shown below. The screen is divided into 100 equal parts horizontally (X) and vertically (Y). The top-left corner of the camera image is the origin (0,0), the bottom-right corner is (100,100), and the center is (50,50). When a corresponding color block is detected, it returns its X, Y, W, and H.

Block Descriptions
- Sengo 1 Initialization Block
The initialization block must be executed before using the camera. It is usually placed under the "when started" hat block.

- Sengo1 Set Mode Block

- Color Detection Algorithm Parameter Settings
Select the color to detect and the size of the recognition area.

- Sengo1 Color Block Result Reading


Returns the detection results of the color block, including center x-coordinate, center y-coordinate, width (w), and height (h).
5. Programming
Connect Device: Connect the MicroBlocks IDE to the CoCube robot via wired or wireless connection, and attach the AI vision module to the front of the CoCube robot.
Load Block Libraries: Add "CoCube Sengo1" or "CoCube Sengo2" from the robot library. The choice depends on the camera hardware model; there is no significant difference in usage. Sengo2 has more features. This tutorial uses the Sengo1 library.

- Camera Module Initialization: Initialize Sengo1 on startup. Set the camera's algorithm mode to "Blob Detect" for color recognition. Initialize variables "dx" and "dy" to 0, which will be used to judge the target color block's position.

- Repeatedly Get Color Block Position: Use the "Sengo1 get blob" attribute to observe the position and size labels when the target color block is on the screen. Determine the center parameters. Use "dx" and "dy" to calculate the deviation from the screen center and decide how CoCube should adjust its direction and position.


Question: What is this block equivalent to?

- Write Your Own Program: With the debugging code above, try combining more functions!
6. Challenge
Based on the color recognition function, implement a feature to follow a moving blue cylinder.
Note: You need to manually set the color recognition parameters for the Sengo1 camera, changing the detected color from the factory default red to blue.

Reference Code: Case: Sengo1 Color Detection
Reference Code: Case: Sengo2 Color Detection