Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of 3pi_Line_Follow by
code.cpp@0:008c53db1931, 2018-03-20 (annotated)
- Committer:
- eencae
- Date:
- Tue Mar 20 16:28:07 2018 +0000
- Revision:
- 0:008c53db1931
- Child:
- 1:d8c15d5b8eac
Simple line follow code for the m3pi robot.; ; Written in an Arudino-like way - there is a repeat() function that is the only thing that needs to be modified.; ; Useful for coding beginners.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| eencae | 0:008c53db1931 | 1 | #include "main.h" |
| eencae | 0:008c53db1931 | 2 | |
| eencae | 0:008c53db1931 | 3 | void repeat() |
| eencae | 0:008c53db1931 | 4 | { |
| eencae | 0:008c53db1931 | 5 | float speed = 0.5; |
| eencae | 0:008c53db1931 | 6 | |
| eencae | 0:008c53db1931 | 7 | |
| eencae | 0:008c53db1931 | 8 | robot.scan(); |
| eencae | 0:008c53db1931 | 9 | |
| eencae | 0:008c53db1931 | 10 | // in range -1 to 1, no error when line is at 0.0 |
| eencae | 0:008c53db1931 | 11 | // number is negative when the line is to the left |
| eencae | 0:008c53db1931 | 12 | // number is positive when the line is to the right |
| eencae | 0:008c53db1931 | 13 | float position = robot.read_line(); |
| eencae | 0:008c53db1931 | 14 | |
| eencae | 0:008c53db1931 | 15 | robot.display_data(); |
| eencae | 0:008c53db1931 | 16 | |
| eencae | 0:008c53db1931 | 17 | |
| eencae | 0:008c53db1931 | 18 | |
| eencae | 0:008c53db1931 | 19 | float step = 0.4*position; |
| eencae | 0:008c53db1931 | 20 | // + when line to right |
| eencae | 0:008c53db1931 | 21 | // - for line to left |
| eencae | 0:008c53db1931 | 22 | |
| eencae | 0:008c53db1931 | 23 | |
| eencae | 0:008c53db1931 | 24 | if (position < 0.0) { // line to the left...need to turn left |
| eencae | 0:008c53db1931 | 25 | robot.left_motor(speed+step); |
| eencae | 0:008c53db1931 | 26 | robot.right_motor(speed); |
| eencae | 0:008c53db1931 | 27 | } else { // line to the right...need to turn right |
| eencae | 0:008c53db1931 | 28 | robot.left_motor(speed); |
| eencae | 0:008c53db1931 | 29 | robot.right_motor(speed-step); |
| eencae | 0:008c53db1931 | 30 | } |
| eencae | 0:008c53db1931 | 31 | |
| eencae | 0:008c53db1931 | 32 | } |
