Upload
duongkiet
View
227
Download
0
Embed Size (px)
Citation preview
Writing Code
• Plan
• Code
– In small pieces, tested at each step
• Refine
– Debugging: finding and fixing errors
– Refactoring: improving the performance of working code
– Extending: adding features to working code
The Teleop Template
• Handles communication between NXT, Samantha, and field control
• Provides the ability to read joysticks
• Your only job: read the joysticks and respond to them!
struct TJoystick
/* from JoystickDriver.c */{bool UserMode; // Autonomous or Telep-Operated modebool StopPgm; // Stop program
short joy1_x1; // -128 to +127short joy1_y1; // -128 to +127short joy1_x2; // -128 to +127short joy1_y2; // -128 to +127short joy1_Buttons; // Bit map for 12-buttonsshort joy1_TopHat; // value -1 = not pressed, otherwise 0 to 7 for selected "octant".
short joy2_x1; // -128 to +127short joy2_y1; // -128 to +127short joy2_x2; // -128 to +127short joy2_y2; // -128 to +127short joy2_Buttons; // Bit map for 12-buttonsshort joy2_TopHat; // value -1 = not pressed, otherwise 0 to 7 for selected "octant".
} TJoystick;
Drive Styles: Tank
• right joystick controls right wheel, left joystick controls left wheel.
task main(){
while (true){
getJoystickSettings(joystick);motor[LeftWheel] = joystick.joy1_y1;motor[RightWheel] = joystick.joy1_y2;wait1Msec(5);
}}
+
Drive Styles: Arcade
• Y-axis moves bot forward/back, X-axis turns bot.
task main(){
while (true){
getJoystickSettings(joystick);motor[LeftWheel]= joystick.joy1_y1 + joystick.joy1_x1/2;motor[RightWheel]= joystick.joy1_y1 - joystick.joy1_x1/2;wait1Msec(5);
}}
+
Teleop 1.0
task main(){initializeRobot();
waitForStart(); // wait for start of tele-op phase
while (true){
getJoystickSettings(joystick);
motor[LeftWheel] = joystick.joy1_y1;motor[RightWheel] = joystick.joy1_y2;
wait1Msec(5);
}}
Reading Buttons
if (joy1Btn(6)) {motor[BallCatcher] = 75;
}else if (joy1Btn(5)) {
motor[BallCatcher] = -75;}else {
motor[BallCatcher] = 0;}
Teleop 1.1
task main(){
initializeRobot();waitForStart(); // wait for start of tele-op phasewhile (true){
getJoystickSettings(joystick);motor[LeftWheel] = joystick.joy1_y1;motor[RightWheel] = joystick.joy1_y2;
if (joy1Btn(6)) {motor[BallCatcher] = 75;
}else if (joy1Btn(5)) {
motor[BallCatcher] = -75;}else {
motor[BallCatcher] = 0;}wait1Msec(5);
}}
Code Management
• Archive successful revisions
• Force backup at end of each session
• Before each competition (24 hr or more!)
– Wipe NXT
– Reinstall with clean software
– Test each program
Teleop 2.0
// Two-driver versiontask main(){
initializeRobot();waitForStart(); // wait for start of tele-op phasewhile (true){
getJoystickSettings(joystick);motor[LeftWheel] = joystick.joy1_y1;motor[RightWheel] = joystick.joy1_y2;
if (joy2Btn(6)) {motor[BallCatcher] = 75;
}else if (joy2Btn(5)) {
motor[BallCatcher] = -75;}else {
motor[BallCatcher] = 0;}wait1Msec(5);
}}