Date and Time Due 6pm Friday 29 September 2017. Requirements

1 ENSE504 Introduction to Computing 2017 Assignment 2 Pololu Robot Line Sensors 1. Administration Assignment Value 10% of the total mark for this paper. Marks will be posted on Blackboard as soon as they are all available. Date and Time Due 6pm Friday 29 September 2017. Requirements Design and implement a C++ program for the following specification. The program is to be written using the online mbed compiler for the Pololu robot. Submitting Your Assignment You must upload a copy of your C++ program source code (.cpp) file to Turnitin by 6pm on Friday 29 September 2017. DO NOT email files to me or give me printed copies of your program. To make a submission to Turnitin: ï‚· Make a text file copy of your C++ program code:  copy the entire contents of your C++ (cpp) file from the mbed website,  paste it into a text file on the PC,  save the text file, with your ID number in the file name. ï‚· Log in to Blackboard (formerly AUTonline) and go to the page for this paper. ï‚· Click on the “Assessment” button. ï‚· Click on the “Current Assessments” folder. ï‚· Click on the “View/Complete” link for the “Assignment 2 Program”. ï‚· After uploading the file, be sure to click the “Submit” button before leaving Turnitin. If you do not click the Submit button, the upload will not be complete. ï‚· After the upload is complete, you will see a Turnitin Digital Receipt. You can save or print this as evidence that you have submitted your file successfully. ï‚· You can upload the file more than once. I will only receive the last file you upload. This means, if you upload a file and then decide to modify it, you can upload the modified file and it will replace the file uploaded earlier. Full details about using Turnitin are in the document AUTonline Student Information which is available on the Online Support tab on the Blackboard DCT:ECMS Student Home page. Plagiarism The Faculty of Design and Creative Technologies rules for plagiarism are contained in the document Academic Integrity Guidance for Students which is available on the Forms and Guides tab on the Blackboard DCT:ECMS Student Home page. I expect students will discuss assignments with each other, but each student is responsible for producing their own work. DO NOT DIRECTLY COPY OTHER PEOPLE’S WORK. The Turnitin software automatically checks assignment submissions for similarity to other student’s work and provides a report of similarities. When plagiarism is detected, disciplinary action may be taken as outlined in the DCT document referred to above. 2 2. Program Overview Requirements ï‚· Write a program to operate the Pololu robot so it follows a black line on the floor. ï‚· Use the design in the given structure diagrams. ï‚· The robot should stop if it goes off the line. ï‚· If the line is a loop, the robot should run until you stop it by turning off its power. Suitable lines will be provided (straight line, circle and ellipse). Program Design A program design is provided in the given structure diagrams. Write your program from these diagrams. The diagrams only show operational program statements. You also need to write include statements, function prototypes, variable and constant definitions, etc, even though these are not shown on the diagrams. Assessment The assessment of your program for this assignment will be based mainly on (but may not be limited to) the following points: ï‚· Source code compiles and links (“builds”) without any errors or (significant) warnings. ï‚· The program runs according to the requirements. ï‚· Uses the design on the given structure diagrams. ï‚· Source code is correctly formatted (good layout). ï‚· Source code is appropriately commented. ï‚· Uses function prototypes. ï‚· Uses appropriate named constants and variable names. ï‚· Uses local variables whenever appropriate. ï‚· Displays appropriate information on the PC (time, line position, sensor values). ï‚· The display on the PC is well formatted. The program will be tested on a straight line, a circle and an ellipse, as available in the lab. 3. Program Specification Introduction The Pololu robot has five light reflectance sensors under the front edge of the lower circuit board. These detect the darkness of the surface directly below the sensor. The Atmel microcontroller on the lower circuit board measures the reflection of light and calculates a value from 0 (white) to 1000 (black) for each sensor. The m3pi class has a function (method) named readsensor that gets the values of the five light sensors. In your program, you need to define a suitable int array and use that as a parameter when calling the readsensor function. The function returns the five sensor values in the array. The array contains the values of the sensors from left to right. Array index 0 has the value of the leftmost sensor through to array index 4 with the value of the rightmost sensor. The sensor spacing is designed to work well with black lines that are 1.8cm wide. Before using the sensors, it is necessary to calibrate them to compensate for ambient light conditions. The m3pi class has the function sensor_auto_calibrate to do this. Call this function once near the beginning of the program before using the sensors. Then call the readsensor function to get the sensor values, as often as necessary for your program. When your program uses the light sensors, place the robot sensors over a black line, then start the robot. The calibrate function will turn the robot left and right across the line, to measure the light reflection from white and black regions under ambient light conditions. 3 Example Program Code m3pi robot(p23, p9, p10); int main() { int sensor[5]; // array for the 5 sensor values float line; robot.sensor_auto_calibrate(); // calibrate the sensors while(1) { robot.readsensor(sensor); // get the 5 sensor values } } Typical Sensor Values The low values of the sensors are very noisy, because the reflectance of white surfaces varies significantly with shadows and other lighting variations. A sensor value less than about 300 is white, a value greater than about 600 is black, and in between 300 and 600 the reflectance is grey. Grey occurs as the sensor moves across the boundary between black and white regions. These values come from my (limited) experiments with the sensors. Checking the Robot is On the Line If any of the three middle sensors indicate black (sensor value ≥ 600) the robot is on the line somewhere. If all three sensors are white or grey, the robot is off the line and the motors should be stopped. To check the robot is on the line, you need to write a statement like this: bool onLine; if (sensor[1] >= 600 || sensor[2] >= 600 || sensor[3] >= 600) { onLine = true; } else { onLine = false; } 4 Line Position Calculation The sensors can be used as a group to estimate the position of the centre of the line relative to the middle sensor of the robot. A suitable formula is: 𝑙𝑖𝑛𝑒= (𝑓𝑙𝑜𝑎𝑡)(𝑠𝑒𝑛𝑠𝑜𝑟[3]−𝑠𝑒𝑛𝑠𝑜𝑟[1])𝑠𝑒𝑛𝑠𝑜𝑟[1]+𝑠𝑒𝑛𝑠𝑜𝑟[2]+𝑠𝑒𝑛𝑠𝑜𝑟[3] line = 0 indicates the centre of the black line is directly beneath the middle sensor of the robot, line = -1 indicates the black line is to the left of the robot (under sensor 1) and line = +1 indicates the black line is to the right of the robot (under sensor 3). Other values between -1 and +1 indicate the position of the line to the left or right of the robot. Ideally the line position should be zero so the middle sensor (sensor 2) is on the centre of the line. (Other formulas exist that also use sensors 0 and 4. However, I want to use them for a different purpose, so we will leave them out of the line position formula.) The sensor values are integers, and (𝑓𝑙𝑜𝑎𝑡) is used to c
hange the type of the numerator to avoid an integer divide operation that would truncate the value of 𝑙𝑖𝑛𝑒 to zero. This is a “type cast”. Position Control The robot can be made to follow the line by using the calculated position to control wheel speeds. You need to select a maximum wheel speed, because at full speed the robot cannot turn quickly enough to stay on a curved line. A maximum speed of 0.3 works well. A simple way to control the robot is to set the difference between the wheel speeds proportional to the line position. Then the further the line is from the centre of the robot, the more quickly the robot will turn back towards the centre of the line. However, do not set the wheel speed to a negative value. The following logic will achieve this. Program Logic If line ≥ 0, the line is towards the right side of the robot. The robot needs to turn towards the right, Therefore, set the left motor speed higher than the right motor speed. Use: leftSpeed = maximumSpeed; rightSpeed = maximumSpeed * (1 – line); Similarly, if line < 0, the line is towards the left side of the robot. The robot needs to turn towards the left. Therefore, set the right motor speed higher than the left motor speed. Use: rightSpeed = maximumSpeed; leftSpeed = maximumSpeed * (1 + line); Notice the different + and – signs in the two formulas, because the line position variable has a different sign in each case. Finally, remember the left_motor and right_motor functions are faulty. Use the left_motor function to set the right motor speed and use the right_motor function to set the left motor speed. Try this with various types of lines: straight line, circle and ellipse. NOTE: This strategy for calculating the wheel speeds is an example of proportional control, because the difference in the wheel speeds is proportional to the position of the line relative to the required (zero) position. 5 mbed Timer Class The mbed library has a Timer class (data type). A Timer object (variable) behaves like a stopwatch. A Timer object measures time (in microseconds) and the class provides several functions that allow your program to read the time that has elapsed since the time was last reset and started. Your program can read the elapsed time in seconds, milliseconds or microseconds. The Timer class has the following methods (functions). void reset(void); Resets the timer to zero. If the timer has already started, this function does not stop the timer. void start(void); This causes the timer to start measuring time. This does not reset the timer to zero. void stop(void); This causes the timer to stop measuring time. This does not reset the timer to zero. float read(void); This returns the elapsed time in seconds. The return value is a float so this includes fractions of a second. int read_ms(void); This returns the elapsed time in milliseconds. The return time is an int so this is a whole number and its maximum value (231) means the maximum time elapsed is about 25 days. int read_us(void); This returns the elapsed time in microseconds. The return time is an int so this is a whole number and its maximum value (231) means the maximum time elapsed is about 35 minutes. Using the Timer Variable Use a Timer variable to measure time so your program can periodically transmit data to the PC using the Wixel wireless communications. This reduces the amount of data being transmitted. Define a Timer variable near the beginning of the program, just after the robot and wixel definitions. Also define a constant with the required time period, in milliseconds. Timer timer; // Define a timer for accurate timing const int PERIOD = 50; // milliseconds In the main function, define an int variable tNext that is the time when the next data transmission should occur. int tNext = 0; Just before the start of the main do-while loop, reset and start the timer: timer.reset(); timer.start(); Inside the do-while loop, read the timer and if the timer value is greater than or equal to tNext, transmit data to the PC and add PERIOD to tNext. Note that t is an int variable. t = timer.read_ms(); if (t >= tNext) { wixel.printf(. . .); // transmit data to PC tNext = tNext + PERIOD; } After the do-while loop (if the robot goes off the line), stop the robot, read the timer and transmit the final display to the PC. robot.stop(); t = timer.read_ms(); // get the time when the robot stopped wixel.printf(. . .); // transmit final data to PC 6 3. Structure Diagrams main Function 7 Other Functions

Leave a Reply

Your email address will not be published. Required fields are marked *