Introduction to Robots and the Mind - Proportional Controller - Bert Wachsmuth & Michael Vigorito Seton Hall University
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 }
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; }
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.
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 }
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); }
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 }]
Multiple decision example int dist = getDistance(); if (dist < 10) { avoidObstacle(); } else if (dist < 20) { slowdown(); } else { forward(); }
Common error with multiple if int dist = getDistance(); if (dist < 20) { slowdown(); } else is (dist < 10) { avoidObstacle(); } else { forward(); } Logical mistake!
Self-driving car algorithm
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
Quadratic equation Algorithm
Test and refine algorithm
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
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(); } }
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(); }
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; }
Refine algorithm