Line follower code nxt robot program source code
Explain that they will be exploring how loops work and how to incorporate these into their programs. Setup Use the technic beams available in the EV3 core set to simulate green and red lights. Place the beams on the table so the Color Sensor can detect them while rolling over them. Students should use the same function of the Color Sensor to recognize when the robot is crossing a line. Use a thick approx. Have the students simulate alarm signal for the driver if the robot is crossing this line.
This feature is often available in new cars. Start motors B and C drive forward with a curve away from the line. Wait for the Color Sensor to detect the color white. Repeat steps 2 to 6 forever. Differentiation Option The students will create an automated, driverless vehicle that can follow a line. Have the students explore how an automated vehicle might be guided along a road or track.
The students will need to be introduced to the Switch Block, which will operate inside a loop. Explain that the Switch Block can be used to automate a program that allows the wheeled robot to operate autonomously. Also explain that the Switch Block can be used to control the flow of a program and that the default Switch Block, using the Touch Sensor, is a classic example of Boolean logic. In other words, the wheeled robot will turn left and then right depending on whether the line i.
Find a suitable video online to demonstrate an example of this to the students. Once the wheeled robot is following the line, can it be improved to behave more like a car i.
Note Students will once again use the Color Sensor, but this time they will need to program it so that it responds to reflected light intensity. They will need to take reflected light intensity readings from the Port View in order to gauge what value to input into the Wait Block. This will work best using black or blue tape on a very light or white surface.
You will need to spend some time explaining the concept of a switch and how it is an example of Boolean logic. A possible extension from here would be to add a second Color Sensor, and combine the line-follow and traffic light programs to simulate automated passenger services, such as a train system in an airport.
Share Allow the students to select the tool s they find most appropriate for capturing and sharing their creations, unique thinking, and learning process. The program consists of three loops that run at the same time. The first loop reads the current reflected light value and stores it in the variable called "Number 1". The second and third loop control the left and right motors. The reflected light sensor gives a value between 0 and A typical value for a black surface is around 20 and for a white surface something around These values depend of on the ambient right conditions and the distance of the sensor from the surface.
The motor power itself as well is a value between 0 and In order to compile Ada NXT programs, all you need to do is to have an appropriate makefile in the current directory. It is recommended that you use a different subdirectory for each part of the assignment.
For compiling use "make all" command. Compiler will compile all the required drivers and at the end will generate a compressed file with same name as the main procedure no extension in Windows, with.
In order to start with the lab, you first need to change a setting in the original firmware of the robot. Now put it into reset mode by pressing the reset button at the back of the NXT, upper left corner beneath the USB connector for more than 5 seconds. The brick will start ticking shortly after. This means you robot is ready for uploading the code into its ram. In this lab, the robot will be always on "reset mode" when you upload a program as the code of the previous run can not reside in the ram after turning it off.
The original firmware can be flashed back with the help of TA which you please do before handing back the box. Successful upload will show something similar address may be different: Image started at 0xc.
Now you can disconnect the robot and try testing it. You can turn off the robot by pressing the middle orange button. If you like to work at home, you can install the compilation and upload toolchain yourself. Since this depends heavily on your setup, we can't give you any direct support. However, installation in Windows is farely simple and instructions for Windows and Linux installation can be found in instruction file.
The program you will write is a simple "hello world! Priority'First ; begin Tasks. Note that we assigned lowest priority to this procedure by using attribute 'First which indicates the first value of a range.
This procedure is calling a procedure background the main procedure of Tasks of package Tasks. Your code should do the following:. Light sensors are bit tricky to initialize. Try different procedures of the nxt-display package to master output in the display. For more advanced kind of display you can use the nxt-display-concurrent package from facilities. Make sure your code compiles without error and executes as desired on the NXT brick. Try to measure light values of different surfaces light ones, dark ones, In this part, you will learn how to program event-driven schedules with NXT.
The target application will be a LEGO car that drives forward as long as you press a touch sensor and it senses a table underneath its wheels with the help of a light sensor.
For this purpose, build a LEGO car that can drive on wheels. You may find inspiration in the manual included in the LEGO box. Further, connect a touch sensor using a standard sensor cable to one of the sensor inputs. Ideally events generated by external sources are detected by the interrupt service routines ISRs.
This allows to react immediately to signals from various sources. Unfortunately, most of the sensors on the NXT are working in a polling mode: They need to be asked for their state again and again, instead of getting active themselves when something interesting happens. Our workaround for this is to create a small, second task that and checks the sensors periodically about every 10ms.
If the state of the sensor changed, it generates the appropriate event for us. Integer; -- Event data declaration Signalled: This protect object can be used by different tasks to communicate between them.
For example, a task can block on receiving event:. In order to do this, declare and implement a task "EventdispatcherTask". It should call the appropriate API function to read the touch sensor and compare it to it's old state.
A static variable may be useful for that. If the state changed, it should release the corresponding event by using signal procedure of the Event protected object. Just as in part 1, put your code in an infinite loop with a delay in the end of the loop body. As suggested by the names of the events, the idea is that they should occur as soon as the user presses and releases the attached touch sensor.
In order for MotorcontrolTask to have priority over EventdispatcherTask, make sure to assign a lower priority to the latter. Otherwise, the infinite loop containing the sensor reading would just make the system completely busy and it could never react to the generated events.
Add further some nice status output on the LCD. This should complete your basic event-driven program. Compile and upload the program and try whether the car reacts to your commands. Attach a light sensor to your car that is attached somewhere in front of the wheel axis, close to the ground, pointing downwards.
Extend the program to also react to this light sensor. The car should stop not only when the touch sensor is released, but also when the light sensor detects that the car is very close to the edge of a table. You may need to play a little bit with the "Hello World! The car should only start moving again when the car is back on the table and the touch sensor is pressed again. The edge detection should happen in EventdispatcherTask and be communicated to MotorcontrolTask via the event protected object.
Use two new events for that purpose. Make sure you define and use all events properly. Further, the display should provide some useful information about the state of the car. Please hand in only the source of the full second program that includes the light sensor code. Make sure you include brief explanations and that your source is well-commented. Note that hand-ins without meaningful comments will be directly discarded.
Real-time schedulers usually schedule most of their tasks periodically. This usually fits the applications: Sensor data needs to be read periodically and reactions in control loops are also calculated periodically and depend on a constant sampling period. Another advantage over purely event-driven scheduling is that the system becomes much more predictable, since load bursts are avoided and very sophisticated techniques exist to analyze periodic schedules.
You will learn about response-time analysis later during the course. The target application in this part will make your car keep a constant distance to some given object in front of it. Additionally, the touch sensor is used to tell the car to move backwards a little bit in order to approach the object again. Note that this is a new program again, so for now, do not just extend the program from the event-driven assignment part.
Create a new program instead.