Download presentation
Presentation is loading. Please wait.
1
Introduction to Robots and the Mind - Methods -
Bert Wachsmuth & Michael Vigorito Seton Hall University
2
Last time Learned about fields and methods
What a robot “has”: fields What a robot “does”: methods Working with “fixed” Components Sound.beep(); or Sound.playNote(freq, duration); or Button.ENTER.waitForPressAndRelease(); Working with (regulated) motors Define reference to a motor as a field via public static EV3LargeRegulatedMotor nameForMotor = new EV3LargeRegulatedMotor(MotorPort.A); Use motor methods via nameForMotor.methodName(opt input)
3
Methods A method defines a well-defined subtask that can be called upon to solve a portion of the bigger task. Every program (task) needs to be divided up into multiple methods (subtasks), each of which is simple and flexible and tested for correctness The methods are combined, usually in the main method, to solve the overall task Every method has a header that defines its name, input and output types body that defines what the method does comments that describe the method in plain English
4
Example: Problem: we don’t know exactly when a program starts.
Solution: Create a method that indicates that everything is ready to go, but then waits for the user to press a button. // Method to beep and wait until user presses ENTER public static void wait() { System.out.println(“Press ENTER”); Sound.beep(); Button.ENTER.waitForPressAndRelease(); } public static void main(String[] args) wait(); // rest of the program starts here
5
Syntax to define a method
There are two types of methods: those that don’t return anything and those that do: // method that does not return anything public static void methodName(optional input) { … } or // method that does return a specific value public static returnType methodName(optional input) { … return …
6
Java Types Methods can return a value of a specified type, and they can have an input list of variables of certain types. Java build-in types are: int (or short) defines an integer double (or float) defines a decimal char single character boolean either true or false String list of characters
7
Examples Method that triples its input value and writes result on LCD screen Method that triples its input value and returns the result Method that engages both motors for n full rotations at a specified speed Method that computes the area of a right triangle with a given base and height. Method that computes both the area and the circumference of a circle with radius r Method that drives a differential drive robot forward x cm Method that rotates a differential drive robot by x degrees
8
Methods for Differential Drive
Want to define a method that drives robot x cm. Start with arbitrary conversion factor 7.3 (or anything else): public static void drive(int distance) { int rotDegrees = (int)(7.3*distance); leftMotor.rotate(rotDegrees, true); rightMotor.rotate(rotDegrees); } Call this method in the main method: drive(10); Adjust the conversion factor through trial and error until your robot really drives 10 cm
9
Model for Rotation in Place
Need: length of axis L radius of wheels r Could compute angle t to rotate wheels to achieve a turn by angle T
10
Methods for Differential Drive
Want to define a method that turns robot x degrees. Start with arbitrary conversion factor 5.4 (or anything else): public static void turn(double angle) { int rotDegrees = (int)(5.4*angle); leftMotor.rotate(rotDegrees, true); rightMotor.rotate(-rotDegrees); } Call this method in the main method: turn(180); Adjust the conversion factor through trial and error until your robot really turns 180 degrees
11
Second Challenge Our program to solve 2nd challenge has:
Two fields of type ‘Motor’ Three methods: public static void drive(int distance) public static void turn(int angle) public static void main(String[] args) drive(50); turn(90);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.