Download presentation
Presentation is loading. Please wait.
Published byJanel Welch Modified over 8 years ago
1
Introduction to Robots and the Mind - Proportional Controller - Bert Wachsmuth & Michael Vigorito Seton Hall University
2
Last Time: Loops while loops while (condition_is_true) { // do this } for loops: for (type var=init_value; condition_is_true; change_var) { // do this }
3
Loop examples while (Button.ENTER.isUp()) { System.out.println(getDistance()); Delay.msDelay(1000); } int count = 0; while (count < 4) { drive(60); turn(90); count = count + 1; }
4
Common loop problems int count = 0; while (count < 100) { System.out.println(“Do not cheat in class”); } while (Button.ENTER.isUp()); { System.out.println(“D=“ + getDistance()); } Tricky mistakes that are not flagged by the compiler. Run-time error as opposed to compile-time error.
5
Last Time: Conditionals Simple if statement: if (condition_is_true) { // do this } if statement with alternative: if (condition_is_true) { // do this } else { // do that }
6
Conditional examples if (getDistance() < 25) { rightMotor.stop(); leftMotor.stop(); } if (getDistance() < 25) { rightMotor.setSpeed(200); leftMotor.setSpeed(200); } else { rightMotor.setSpeed(800); leftMotor.setSpeed(800); }
7
Multiple if statement if (condition1_is_true) { // do code 1 } else if (condition2_is_true) { // do code 2 }... else if (conditionN_is_true) { // do code block N } [ else { // do optional code block }]
8
Multiple decision example int dist = getDistance(); if (dist < 10) { avoidObstacle(); } else if (dist < 20) { slowdown(); } else { forward(); }
9
Common error with multiple if int dist = getDistance(); if (dist < 20) { slowdown(); } else is (dist < 10) { avoidObstacle(); } else { forward(); } Logical mistake!
10
Self-driving car algorithm
11
Algorithm vs Formula A math formula represents a relationship between expressions whereas An algorithm describes steps to solve a task, i.e. an algorithm is goal-oriented
12
Quadratic equation Algorithm
13
Test and refine algorithm
14
Self-driving car algorithm An algorithm is a sequence of well-defined steps that collectively solve a task Self driving car algorithm 1: 1. Drive forward but maintain distance Self driving car algorithm 2: 1. Drive forward 2. Maintain distance
15
Refine algorithm 1. Drive forward –If you are too close to car in front, stop –If you are too far away from the car in front, go while (Button.ENTER.isUp()) { double distance = getDistance(); if (distance < 0.1) { motorLeft.stop(true); motorRight.stop(); } else { motorLeft.forward(); motorRight.forward(); } }
16
Refine algorithm Drive forward –If you are vey close to lead car, set speed to zero –If you are a medium distance from lead car, set speed to ‘slow’ –If you are far away from lead car, set speed to ‘fast’ while (Button.ESCAPE.isUp()) { double distance = getDistance(); double speed = getSpeed(distance); motorLeft.setSpeed((int)speed); motorRight.setSpeed((int)speed); motorLeft.forward(); motorRight.forward(); }
17
Refine Algorithm public static double getSpeed(double distance) { if (distance < 0.05) { return 0.0; } else if (distance < 0.15) { return 300.0; } else { return 600.0; }
18
Refine algorithm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.