ELEC1601-无代写
时间:2023-09-11
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 1/10
Exercise 3 using Physical Equipment
Sample video
One of your 2022 tutors kindly recorded an example
(https://canvas.sydney.edu.au/media_objects_iframe/m-69yaBFritYCJ97EApHVtpmPncv8mdpQs?
type=video?type=video) of how your Shield-bot might perform in the maze in the final exercise this
week.
Quick Links
The Shield-bot Sensor Shield (https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-
using-physical-equipment#part0)
Part 1 - Calibrating an IR Sensor (https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-
using-physical-equipment#part1)
Part 2: Robot Movement with the Continuous Rotation Servo
(https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment#part2)
Part 3 - Robot Sensing and movement
(https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment#part3)

The Shield-bot Sensor Shield
In the simulation exercise, you first learnt about the light sensor, and then how it could be used as
an input to control the servo motors. The model your design was based on was that as a wall
moved closer, the sensor would receive more light.
To enable the Shield-Bot to navigate, you will instead use an infra-red (IR) LED as a "headlight"
and pair it with an IR receiver to detect the light being reflected off walls and objects. Similar to the
visible light sensor, as a wall moves closer, the IR receiver will detect more reflected IR light.
Some additional details if you are interested: IR light is an invisible light whose wavelength spans
800 – 1000 nanometres (nm). The wavelength of visible light ranges from 400 nm (blue light) to
700 nm (red). IR is commonly used to transmit data such as in TV remote control and wireless
keyboard/mouse. Just like visible light, IR light also bounces off walls and almost any solid
material. Hence it can be used to detect walls or solid obstacles when an IR receiver is placed
facing the same direction as the IR source.
To make things easier for you, we have designed a sensor shield for the Shield-bots that already
has three pairs of IR LEDs (sources) and IR receivers (sensors) installed. This is mounted on top
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 2/10
of your Shield-bot over the top of the small breadboard so that it is connected directly to the
Arduino. This means that you do not need to construct any physical circuits during the lab this
week and can focus on developing code that you will be using later in your Project. If you want
some background information about how these LEDs and receivers are configured, you can find
the original Shield-bot assembly guide here (https://learn.parallax.com/tutorials/robot/shield-
bot/robotics-board-education-shield-arduino/chapter-7-navigating-infrared-10) .
The below image shows a representation of the sensor shield. The green LED will light up if your
sensors have power. The three grey components are the IR receivers which will detect IR light
being reflected off objects in front of your robot and off walls on either side. The IR LEDs are
mounted on the underside of the board, directly below the IR receivers. The red LEDs are free for
you to use however you wish, but we recommend that you use them as a visual indicator that your
robot has detected an obstacle with the corresponding IR LED/receiver.
For the first exercises, you will only be using the centre, or mid IR LED/receiver pair. You can see
from the table printed on the board in the image above that the IR LED is connected to digital Pin
6, the IR receiver/sensor is connected to digital Pin 7, and the red LED is connected to analog Pin
A1. The schematic below shows how these components are connected to the Arduino pins.
Part 1: Calibrating an IR Sensor (This is loosely related to
Simulation Exercise 3 Part 1
(https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-in-
simulation#part1) )
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 3/10
Some source material can be found here: Object Detection Test Code
(https://learn.parallax.com/tutorials/robot/shield-bot/robotics-board-education-shield-
arduino/chapter-7-navigating-infrared-9) Detection Range Adjustments
(http://learn.parallax.com/node/305)
1.1  Detection
Infrared detection takes three steps:
A. Flash the IR LED on/off at 38 kHz using the tone()
(https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/) command.
B. Delay for a millisecond or more to give the IR receiver time to send a low signal in
response to sensing 38 kHz IR light reflecting off an object.
C. Check the state of the IR receiver using digitalRead(). Note that digitalRead() will return 0 if
IR light is detected (object within range) or 1 if no IR light is detected.
D. Light the red LED if the IR receiver returned a low signal (IR detected).
You will note that the receiver response is digital, not analog. This means that you only know
whether IR light was detected or not. You don't know how strong the reflected light is. This is
important on our robot because it means that you know whether an object is within detection
range, but you don't know exactly how close it is.
Here is an example of the three steps applied to the centre/mid IR LED (pin 6) and IR receiver (pin
7).
tone(6, 38000);      // Flash IR LED at 38 kHz
  delay(1);            // Wait 1 ms
  noTone(6); // Stop the LED flashing after delay time has passed
int ir = digitalRead(7); // IR receiver -> ir variable
The tone function generates a square wave that repeats its high/low cycle 38,000 times per
second. Only one tone can be generated at a time. The noTone()
(https://www.arduino.cc/reference/en/language/functions/advanced-io/notone/) command must be
used to stop the generation of the square wave before tone() can be used on a different pin.
To test the LED/receiver pair, use the following code:
const int irLedPin=***, irReceiverPin=***; // Select these to match the IR LED/receiver pai
r that you are using
const int redLedPin = **; // Select this to match the red LED next to the
IR receiver you are using
void setup()                                 // Built-in initialization block
{
  pinMode(irReceiverPin, INPUT);   // IR receiver pin is an input
pinMode(irLedPin, OUTPUT); // IR LED pin is an ouput
  pinMode(redLedPin, OUTPUT); // Red LED pin is an output
  Serial.begin(9600);                       // Set data rate to 9600 bps
}  
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 4/10
 
void loop()                                  // Main loop auto-repeats
{
  int irVal = irDetect(38000);       // Check for object
  Serial.println(irVal);                     // Display 1/0 no detect/detect
if (irVal == 0)                   // Optional - display detection by setting red L
ED high
{
digitalWrite(redLedPin, HIGH);
  }
delay(500);                                // 0.5 second delay - just long enough to see th
e LED blink
digitalWrite(redLedPin, LOW);
}
// IR Object Detection Function
int irDetect(long frequency)
{
  tone(irLedPin, frequency);              // Turn on the IR LED square wave
  delay(1);                                 // Wait 1 ms
noTone(irLedPin); // Turn off the IR LED
 int ir = digitalRead(irReceiverPin); // IR receiver -> ir variable
  delay(1);                                 // Down time before recheck
  return ir;                                // Return 0 detect, 1 no detect
}

Test the frequency detection:
1. Write code that uses the middle sensor to detect objects every 500 ms.
2. Place a white target directly in front of the robot. You could use a section of the maze wall
(best) or a white sheet of paper. How far away can you sense the object?
3. Angle the robot to the target instead of placing it directly in front of the robot. Does the
detection distance change? If so, by how much?
1.2  Obtaining distance and not just detect/not-detect
The power of IR light diminishes over distance, so the detection of IR light reflected off a wall
relies on (1) its emission power and (2) the sensitivity of the receiver. A higher emission power will
allow the IR light to travel farther distance and still have enough power to be detected by the
receivers. Similarly, a more sensitive receiver will be able to detect a weaker reflection, increasing
the detection range for a fixed emission power.
We could explore the emission power and distance relationship by changing the value of the
resistor that is located between LED's anode and the digital pin - effectively changing the
brightness of the IR "headlight" - but this would involve swapping resistors which is impractical
while the robot is operating. Instead, we will take advantage of a property of the IR receiver to
adjust the detection distance using code.
Referring to the figure below for a typical IR sensor (TSOP34338
(https://canvas.sydney.edu.au/courses/52848/files/33289890?wrap=1)
(https://canvas.sydney.edu.au/courses/52848/files/33289890/download?download_frd=1) ), you will
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 5/10
see that it is most responsive at its design frequency of 38 kHz but there is a sharp drop-off in
receiver responsivity at frequencies either side of 38 kHz. For example, if you used a 0.9 * 38 = 34
kHz frequency on the IR LED, you will see that the sensor is only about 40% as responsive. In
other words, it won't "see" as far. The most sensitive frequency (38 kHz) will detect the objects
that are the farthest away, while the further from 38 kHz the IR LED’s signal rate is, the closer the
IR receiver has to be to an object to see that IR signal’s reflection. The exact values vary between
sensors, so the figure below may not be exactly the same as the sensors on your robot (most
likely GP1UX51QS).
Rough distance sensing with IR can be achieved by using a frequency sweep technique to take
advantage of this varying sensitivity.
Test the Frequency Sweep:
1. Several frequencies in the range 38 kHz to 45 kHz have been listed in the table below.
2. Place your white target directly in front of the Shield-bot, starting with the closest distance
listed in the table below. If you don't have a ruler, an AA size battery is 5cm long.
3. Start with the most sensitive frequency first (i.e. 38 kHz) and test each of your frequencies
from most sensitive to least sensitive. Record in the table below whether the traget was
detected. Remember that a digitalRead() value of 0 means that the target was detected.
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 6/10
Target
distance
38
kHz
(most)
39
kHz
40
kHz
41
kHz
42
kHz
(least)
5 cm
15 cm
25 cm
Depending on which frequency makes the reflected infrared no longer visible to the IR detector,
your sketch can infer a rough distance. The more frequencies you add, the finer your range
resolution can become.
1.3  Automatic distance indication
The next image shows an example of how the Shield-bot can automatically test for distance using
frequency. Note this diagram is not to scale: the detection range only covers a short distance and
begins a few centimetres away from the robot.
In this example, an object is in Zone 3. That means that the object can be detected when the IR
LED transmits at 38 kHz and 39 kHz, but it cannot be detected when the IR LED transmits at 40
kHz, 41 kHz, or 42 kHz. If you were to move the object into Zone 2, then the object would be
detected when 38 kHz, 39 kHz, and 40 kHz are transmitted, but not with 41 kHz or 42 kHz.
The irDistance function from the next sketch performs distance measurement using the technique
shown above. Code in the main loop calls irDistance and passes it values for a pair of IR LED
and receiver pins.
// IR distance measurement function
int irDistance(int irLedPin, int irReceivePin)
{
   int distance = 0;
   for(long f = 38000; f <= 42000; f += 1000)
   {
  distance += irDetect(irLedPin, irReceiverPin, f);
   }
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 7/10
return distance;
}
The irDetect function returns a 1 if it doesn’t see an object, or a zero if it does. The expression
distance += irDetect(irLedPin, irReceiverPin, f) counts the number of times the object is not
detected. After the for loop is done, that distance variable stores the number of times the IR
sensor did not see an object. The more times it doesn’t see an object, the further away it must be.
So, the distance value the function returns represents the zone number in the image above.
1.4  Calibrating your sensor to a known distance
We know from experience that no two IR LEDs have exactly the same brightness and no two IR
receivers have exactly the same sensitivity. This might be due to slight variation in the resistors
used on the IR LEDs (causing the LEDs to have slightly different brightness) or might be due to
manufacturing differences in the receivers.
This means that it is highly likely that the three LED/receiver pairs on your robot will detect at
slightly different distances, and is a good example of how using physical hardware can generate
issues that need to be taken into account when compared to using simulation.
The goal of this exercise is for all three sensors to have the same detection distance, and you will
do this by modifying their frequencies.
1. Set your white target in front of your robot at a the distance that you want to detect at. You
could use the width of the lid on your robot box as a reference distance. The exact distance is
not important as long as you can repeat it.
2. Modify your tone frequency such that the sensor is at the edge of detecting/not detecting. You
can do this automatically too with a frequency sweep, starting from least sensitive and working
towards 38 kHz, and stopping as soon as the target is detected. You might want to choose
increments of less than 1 kHz for this exercise.
3. Take note of the frequency that corresponds to your target detection distance.
4. Repeat with the other two sensors at the exact same target-robot distance.
You now have a piece of code that you can use to quickly calibrate different LED/sensor pairs. In
other words, you now know that if you set your LED frequency to the frequency that you found in
Step 3, you will detect objects at a distance equivalent to the reference distance that you set in
Step 1.
Part 2: Robot Movement with the Continuous Rotation
Servo (This is related to Simulation Exercise 3 Part 2
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 8/10
(https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-in-
simulation#part2) )
Like physical IR LEDs and IR receivers, the physical servo motors also show some variability
when compared to the ideal behaviour that is modelled in the simulation environment.
In Simulation Exercise 3 Part 2, you explored the servo response when it received
writeMicroseconds values between 1,300 and 1,700. You should have noticed that the servo
was stationary (or centred) when it received a value of 1,500. We know from experience that this
is not always the case with the physical robots.
The goal of this exercise is to learn how the servo motors on your robot respond, so that you can
calibrate them and compensate for any variation.
1. The servo motors (wheels) connect to the top of the robot next to the Sensor shield. The
headers are labelled 13, 12, 11, and 10 which correspond to the digital pins that they are
connected to.
Check that the right servo motor is connected to Pin 12 and the left servo motor to Pin 13.
Do not use Pin 10 or Pin 11 because these pins are being used by the left IR LED/sensor
pair!
Check that the servo connections are correctly oriented, with the white wires closest to the
edge of the robot.
2. Find your TinkerCAD implementation for Simulation Exercise 3 Part 2
3. Copy your paste your code to the Arduino Editor
4. Check all your movements work!!
5. Calibrate your movements - adjust your PWM settings and timings/delays to get the correct
turning motions.
6. Identify the PWM setting for each servo/wheel that corresponds to no movement. You could
write a small piece of code that sweeps through a range of PWM values and displays them on
the Serial Monitor. You can then make a note of what value the servo stops moving. Note - The
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 9/10
values will most likely be different for each servo. There may even be a "dead band" where
there is a small range of frequencies where no movement occurs.
If you are unable to identify a PWM value where the servo completely stops moving, please let
your demonstrator know.
Part 3: Robot Sensing and movement (This is related
to Simulation Exercise 3 Part 3
(https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-in-
simulation#part3) )
In this part, we will consider how a robot will detect an opening in the wall on either side of it that
leads to a new passage using two IR sensors - one facing right and one facing left - to detect light
being reflected off the side walls of the maze.
Referring to the table printed on the board in the image at the top of this page, you will see that
the Arduino pins that correspond to the left and right IR LED/sensor pairs and their associated red
LEDs are:
Left Right
IR
LED
Pin
10
Pin 2
IR
sensor
Pin
11
Pin 3
Red
LED
Pin
A2
Pin
A0
1. Find your TinkerCAD implementation for Simulation Exercise 3 Part 3
2. Update your code to use the IR LED/sensor approach instead of the light sensor.
3. Add a 5 second delay to your code before any movement starts.
4. Check that your code works. Use sections of the maze wall (best) or white sheets of paper to
simulate side walls. Does your robot travel straight and turn as expected?
5. Adjust your code as necessary. This code will become a foundation for your group Project. If
you take the time to get it working now, it will give you more time to focus on other challenges
in the project lab sessions.
2023/9/11 18:25 Exercise 3 using Physical Equipment: ELEC1601 Introduction to Computer Systems
https://canvas.sydney.edu.au/courses/52848/pages/exercise-3-using-physical-equipment?module_item_id=2053582 10/10
Copyright © The University of Sydney. Unless otherwise indicated, 3rd party material has been reproduced and communicated to you
by or on behalf of the University of Sydney in accordance with section 113P of the Copyright Act 1968 (Act). The material in this
communication may be subject to copyright under the Act. Any further reproduction or communication of this material by you may be the
subject of copyright protection under the Act. Do not remove this notice.
Live streamed classes in this unit may be recorded to enable students to review the content. If you have concerns about this, please
visit our student guide (https://canvas.sydney.edu.au/courses/4901/pages/zoom) and contact the unit coordinator.
Activities to do after the lab session
All remaining team members should write a lab report. Submit the report before the start of your
next lab session.


essay、essay代写