LEGO_EV3_Python_Examples/Object_Sorting/README.md

123 lines
9.4 KiB
Markdown
Raw Permalink Normal View History

2024-08-27 08:36:47 +00:00
# EV3_Python Object sorter
2023-09-21 10:17:22 +00:00
2024-09-10 15:29:26 +00:00
The following document describes a project including a program written in Python for the Lego Mindstorm EV3.
The purpose of the prject is to let a robot sort cubes based on their color.
Before using the program on the EV3 Brick you need to prepare it as described in the README document of the framework.
2023-09-21 10:17:22 +00:00
## Used Hardware
2024-08-27 08:44:27 +00:00
<img align="right" src="Pictures/Proj2_RobotStructure.png" width="200">
2023-09-21 10:46:28 +00:00
The program was written for the LEGO EV3 Brick. It is the base of every Mindstorm EV3 robot. Additionally, for identification of an object / color, a camera from an external company is used.
2023-09-21 10:17:22 +00:00
The built / used robot has two motors, one on each side, with two wheels each connected via a caterpillar track.
But the program should work with all robots that have one driven wheel per side.
#### Camera
2024-09-10 15:43:32 +00:00
The camera used for this program is the **Pixy2** for Lego Mindstorms. To work with the camera, you have to teach it the object you want to sort. For this, you need PixyMon on your PC. Additionally you need to set up the Pixy2 and "install" the module `pixycamev3`. You can download the needed file form the specific PixyCam GitHub but it is also inculded into our repository.
If you want to get detailed instructions on either of these points, look in the [Quick Start Guide](https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:pixy_lego_quick_start) on the PixyCam Website.
2023-09-21 10:17:22 +00:00
2024-08-27 08:36:47 +00:00
* **Camera**: Connected to input port **1**
2023-09-21 10:17:22 +00:00
2024-09-10 15:43:32 +00:00
* Taught **Signature 1**: green cube, leave in sorting area
* Taught **Signature 2**: orange cube, sort out
2023-09-21 10:17:22 +00:00
2023-09-21 10:46:28 +00:00
The camera should be mounted on top of the robot and directed downwards for good results in detection.
2023-09-21 10:17:22 +00:00
#### Motors
2024-08-27 08:36:47 +00:00
The robot drives forward when the wheels turn counter-clockwise. If you use a robot with a clockwise direction of rotation, you need to change the parameters given in the arguments to `'normal'`. (See Additional Parameters in [Initialization](#initialization))
2023-09-21 10:17:22 +00:00
* **Left** Motor: Connected to output-port **A**
* **Right** Motor: Connected to output-port **D**
#### Sensors
2023-09-21 10:46:28 +00:00
To track the distance between the robot and the cube, a LEGO Ultrasonic Sensor is used. To identify the outer line of the sorting area, one LEGO Color Sensor is used. For keeping track of the rotation on the back side of the robot, a LEGO gyro sensor is mounted.
2023-09-21 10:17:22 +00:00
* **Ultrasonic Sensor**: Connected to input-port **2**
* **Color Sensor**: Connected to input-port **3**
2024-08-27 08:36:47 +00:00
* **Gyro Sensor**: Connected to input-port **4**
2023-09-21 10:17:22 +00:00
2024-08-27 08:44:27 +00:00
#### Objects to sort <img align="right" src="Pictures/Proj2_ColoredCubes.png" width="200">
2023-09-21 10:46:28 +00:00
The sorted objects are simple cubes made of paper. You can find the tutorial for folding the cube on [YouTube](https://youtu.be/VjooTcZRwTE?si=HaiStBDw1cQu3K7o). To ensure the ultrasonic sensor recognizes the cube, it is 5.5 cm high and long. The two colors should be distinct from each other.
2024-08-27 08:44:27 +00:00
2023-09-21 10:17:22 +00:00
## Program
2024-08-27 08:44:27 +00:00
<img align="right" src="Pictures/Proj2_Flowchart.png" width="500">
2024-09-10 15:43:32 +00:00
The purpose of the program is to sort different objects. In detail, this means that the robot should push all orange cubes out of the sorting area and leave all green cubes inside.
2023-09-21 10:17:22 +00:00
2024-08-27 08:36:47 +00:00
The main part of the program is the class "Object_Sorter". Due to the fact that the robot gets a lot of inputs and does not need to check everything at the same time, the class contains five different state methods. The robot will switch automatically between the states when certain events happen. You can identify the current state by looking at the LEDs on the EV3-brick.
2023-09-21 10:17:22 +00:00
* **Search**: Both LEDs are black
2023-09-21 10:46:28 +00:00
* **Get**: Only the left LED is green
2024-09-10 15:43:32 +00:00
* **Sort**: Both LEDs are orange
* **Avoid**: Both LEDs are green
2023-09-21 10:17:22 +00:00
* **Edge**: Both LEDs are red
2024-08-27 08:36:47 +00:00
The different methods of the class are explained in detail below.
### Initialization
During the initialization phase of the object sorter all the arguments given are assigned.
In- and Outputs:
* `ultrasonic_input`: Input Port of the Ultrasonic Sensor (Default:`INPUT_2`)
* `color_input`: Input Port of the Color Sensor (Default:`INPUT_3`)
* `gyro_input`: Input Port of the Gyro Sensor(Default:`INPUT_4`)
* `camera_port`: Input Port of the Pixy2 camera (Default: `1`)
* `leftmotor_output`: Output Port of the left motor (Default:`OUTPUT_A`)
* `"rightmotor_output`: Output Port of the right motor (Default:`OUTPUT_D`)
Additional Parameters:
* `leftmotor_polarity`: Polarity (direction of rotation) of the left motor (Default:`'inversed'`)
* `rightmotor_polarity`: Polarity (direction of rotation) of the right motor (Default:`'inversed'`)
* `signature_dice_remains`: Number of signature of the cube that should remain inside the outline (Default:`1`)
2024-09-06 12:23:36 +00:00
* `signature_dice_sorted`: Number of signature of the cube that should be sorted out of the area (Default:`2`)
2024-08-27 08:36:47 +00:00
* `speed_drive`: Speed of motor during normal driving (Default:`30`)
* `speed_search`: Speed of motor during search mode (Default:`10`)
* `distance_us`: Detection range, where the "caught" cube should be in, relevant for the ultrasonic sensor (Default:`12`)
2024-09-06 12:25:07 +00:00
* `max_reflection_outline`: Maximal reflection of outline (Default:`10`)
2023-09-21 10:17:22 +00:00
2024-08-27 08:36:47 +00:00
### Method: Search
The target of the method is to search for new cubes to sort. The robot spins around until one of five events causes a change.
2023-09-21 10:17:22 +00:00
2024-08-27 08:36:47 +00:00
**Robot drives over black line**: If the robot drives across the line, it switches to the state "[Edge](#method-edge)".
2023-09-21 10:17:22 +00:00
2024-08-27 08:36:47 +00:00
**Robot has a cube in his arms**: When the Ultrasonic Sensor detects an object in front of it, this means there is already a cube in its arms, and it switches to state "[Sort](#method-sort)".
2023-09-21 10:17:22 +00:00
2024-09-10 15:43:32 +00:00
**Camera detects green cube**: If the camera finds an green cube, it changes to the state "[Avoid](#method-avoid)".
2023-09-21 10:17:22 +00:00
2024-09-10 15:43:32 +00:00
**Camera detects orange cube**: If the camera finds a orange cube, it changes to the state "[Get](#method-get)".
2023-09-21 10:17:22 +00:00
2024-08-27 08:36:47 +00:00
**Robot spun without finding something**: When the robot has turned for 2 full rounds (720°) and has not detected one of the four things above, it will drive a bit forward. If it reaches the outline, it changes to the state "[Edge](#method-edge)". Otherwise, it starts the state "[Search](#method-search)" again at the new spot.
2023-09-21 10:17:22 +00:00
2024-08-27 08:36:47 +00:00
### Method: Get
2024-09-10 15:43:32 +00:00
The purpose of the method is to drive towards the orange cube and "catch" it in its arms.
2024-08-27 08:36:47 +00:00
To make that happen, the method checks the current position of the cube and corrects the driving direction if necessary. Due to the fact that there are several reasons why the camera does not consistently recognize the cube, the robot has a routine to find the cube again (driving backwards, turning left). If the routine does not work, the program changes back to state "[Search](#method-search)".
Otherwise, the ultrasonic sensor detects the cube in the arms, and the program changes to state "[Sort](#method-sort)", or the robot moves over the line, and it switches to state "[Edge](#method-edge)".
2023-09-21 10:17:22 +00:00
2024-08-27 08:36:47 +00:00
### Method: Sort
2024-09-10 15:43:32 +00:00
The task of the method is to drive the cube to the outline of the sorting area and avoid the green cubes on the way.
If the camera detects an green cube, the program switches to the state "[Avoid](#method-avoid)", whereas if it doesn't, the robot continues straight ahead until the outline is reached and the program changes to the state "[Edge](#method-edge)".
2024-08-27 08:36:47 +00:00
### Method: Avoid
2024-09-10 15:43:32 +00:00
The detected green cube should be avoided by the robot. Therefore, the camera looks for the green cube again and gets its position. If it is out of reach of the robot, the drive path will not change, but if the cube is on the way, the robot corrects its driving direction. After that, the program switches back to the state before. ("[Search](#method-search)" or "[Sort](#method-sort)")
2024-08-27 08:36:47 +00:00
### Method: Edge
The robot has reached the outline. It drives backward and turns 100° to the right. The program switches back to the state "[Search](#method-search)" afterward.
### Method: Run
This method only is used to start the operations of the object sorter and continue in an endless while-loop.
2023-09-21 10:17:22 +00:00
## Problems and Improvements
### Robot does not detect outer line
2023-09-21 10:46:28 +00:00
During programming, I was challenged by the problem that the outer line is sometimes not detected. After trying different approaches to fix the bug, I suspect that the processor of the EV3 is slowed down by the camera and, for this reason, does not detect the line when the sensor is above it.
2023-09-21 10:17:22 +00:00
2023-09-21 10:46:28 +00:00
Another reason can be that the arms project shadows or the line doesn't differ enough from the floor. Make sure that the given values and the physical structure of the robot make sense for your circumstances.
2023-09-21 10:17:22 +00:00
2024-08-27 08:36:47 +00:00
You can change all the input values such as the minimal line reflection and the driving rates with a dictionary of the arguments given to the class. (See [Initialization](#initialization))
2023-09-21 10:17:22 +00:00
### Camera does not detect cubes
2023-09-21 10:46:28 +00:00
If the camera does not detect the cubes, there are multiple reasons. Sometimes, due to lightning changes, the camera does not identify the different colors correct. You can go back to PixyMon and see if the cubes are even detected. If not, you have to teach the object / cube to the camera again, preferably in the environment where the sorting happens. Also, there is a [section](https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:some_tips_on_generating_color_signatures_2) on the PixyCam Website about improving the detection accuracy you should read.
2023-09-21 10:17:22 +00:00
### Ultrasonic sensor does not detect cube
2023-09-21 10:46:28 +00:00
The ultrasonic sensor should be mounted as close to the floor as possible to make sure the cube gets detected. You should build the arms in such a way that they are not in the sensor's field of vision. But they should hold the cube as centered as possible in front of the robot. Make sure that you have followed all the construction advice.