I want to share my project below that helped me to get into the wonderful world of robotC with EV3 hardware.I am adding the source code below. Feel free to use it for noncommercial usage. Mail me for any question.

/*Date : 12012016Project Target:---------------Target of this program is to find the dimensions (X*Y) of a rectangular flat table without edges (dining table)Robot will detect the table edges and not falling down using the color sensor pointing down.A short video of the edge detection mechanism is shown at: https://www.youtube.com/watch?v=uDj8odiibdEThis just demonstrate the functionality of edge detection using the color sensor.

General:--------In this program (Movie at youTube: https://www.youtube.com/watch?v=AAuMDGnGMjU ), Starting point: the robot can be placed anywhere around a table pointing at any direction.The Robot will travel till the edge of the table is detected, and will rotate until it is alined exactly 90deg with the edge, and then turn 180deeg and go to the oposite direction,while measuring the distance. Same will be done for both table edges, and at the end a comment will be send to the computer debug browser with he exact dimentions found.The accuracy of the measure is around 1.5%.THe turning is done using the Gyro measurments and the rotation is based on PID mechansim.

Details:--------- At the begining there are few positions that are not allowed becauae of failing risk. In each of those the robot will stop and will ask to be re-placed.This is done in order to minimize the risk of falling from table.There are stil rare cases that falling may happpend. Make sure to keep an eye on the robot operation at any time.

Situations are as follows:1. Sesnor is placed out of the table. robot will not move (5 beeps will be heared)2. While reaching the edge, the robot understand that it is exactly located on table corner (while scanning as explained below).This is a bit risky and thereofore we wil stop (5 beeps will be heared).3. At the begining of operation robot will do a 360deg scan. If an "out of table" was found it will stop. It means that the location is also risky for falling (5 beeps will be heared).

If all the conditions above are met, we start the measuring process. The stages are as below:

1. From staring point, robot will move is a straight line toward edge of table. Color sensor that is pointing down will detect when it is out of tableand the robot will fully stop.2. After a complete stop the robot will move about 5cm ahead in order to make sure that the sensor is out of the table so it can be used as a "Protractor" to measure the external angle.3. Robot will now rotate left until the sensor hits the table edge again and will stop.4. This is a starting position for measuring the external angle.5. Robot will slowly move the color sensor to the other direciton until meeting the table edge again, and stop6. Once stop, we know the exact angle (<180 deg), and we now tell the robot to turn 1/2 of that angle back.This is done using an accurate PID controller. At this point robot is 90 deg allined to the table boundary.7. Robot will now travel back until the sensor is on edge again, and now rotate exactly 180 deg back (there is a complensation meachansim here sincethe backward movement is not accurtate as the robot is designed to go only straight).8. The exact length of the robot is known (form the point that the sensor is on edge to the oposite position of the sensor after turning 180 deg).9. Now it travels to other side and the distance is being measured using the robot encoder.10. One dimentions is known by now and the robot turn 90deg and start the same process of measuring the other side.

// We have 8 possible places to store data, we call each one a datalog series// This example uses the first four to store values.#define DATALOG_SERIES_0 0#define DATALOG_SERIES_1 1#define DATALOG_SERIES_2 2#define DATALOG_SERIES_3 3#define DATALOG_SERIES_4 4#define DATALOG_SERIES_5 5

// this flag is set for type first time that the "PID fix target" has been reached. // Once it is set it is not possible any more to go to maximum speed int fix_pid_limit = 0; int exit_flag1 = 0; int time_point1 = 0; int stability_achieved = 0; float k_speed = 1; // initialize all variables for both motor B and motor C int actualDegrees=getGyroDegrees(gyroSensor);

// This function will scan a full circle and mark the value for each sensor deg.// It should return an array that contain ther sensor value for each degint scan_clockwise (int power, int threshold){ resetGyro(gyroSensor); sleep(500); resetGyro(gyroSensor); sleep(200);

// since this proc is activated when the sensor is over table we first need to wait for first exit from table while ( SensorValue[Color] > threshold) { sleep(1); } sleep(100); while (SensorValue[Color] < threshold) { // actualDegrees = getGyroDegrees(gyroSensor); // currentDistance_color = SensorValue[Color];

// This will move the sensor head to left until meeting table (sensoe is out of table)int move_until_enter_edge_from_outside (int power, int threshold, int motor_ratio){ // Note that the encoder here counts only forward. It is currenlty not supporting backward movement of the robot.

// If degree is more than 180 than it means that we are on a corner. // it is illegel state and we want to restasrt the flow. if (deg >= 180) { multi_beep (5); writeDebugStreamLine("EV3 reached a risky situation on the corner of the table. Please manually move to a safer location and restart!"); sleep(2000); break; }

datalogDataGroupStart(); datalogAddValue( DATALOG_SERIES_1, getGyroDegrees(gyroSensor) ); datalogAddValue( DATALOG_SERIES_4, 990 ); datalogDataGroupEnd(); // Angle achieved after turn. We keep it since when we turn 180deg we want to use it as a reference. //achieved_angle = getGyroDegrees(gyroSensor); // Move backward until the color sdensor is on the boundary. This move can cause a little drift in angle that wil lbe fixed // latyer on the 180 turn move_until_enter_edge_from_outside(-scan_power/2,threshold,0); sleep(500); // We need to rotate 180 deg (from the original angle after exactTurn was running). //drift = getGyroDegrees(gyroSensor) - achieved_angle;

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum