code – mplab – ifi loader

23
Code – Mplab – IFI Code – Mplab – IFI loader loader Nick and Kyle Nick and Kyle

Upload: heriberto-noboa

Post on 02-Jan-2016

27 views

Category:

Documents


1 download

DESCRIPTION

Code – Mplab – IFI loader. Nick and Kyle. Background. Original C oriented Designed to be lightweight yet robust Very small subset of C. Mplab. Available free from www.microchip.com Default code editor for the TU Storm robots. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Code – Mplab – IFI loader

Code – Mplab – IFI Code – Mplab – IFI loaderloader

Nick and KyleNick and Kyle

Page 2: Code – Mplab – IFI loader

BackgroundBackground

Original C orientedOriginal C oriented

Designed to be lightweight yet robustDesigned to be lightweight yet robust

Very small subset of CVery small subset of C

Page 3: Code – Mplab – IFI loader

MplabMplab

Available free from Available free from www.microchip.comwww.microchip.com

Default code editor for the TU Storm Default code editor for the TU Storm robots.robots.

Supports all the language functions Supports all the language functions needed for both the TU Storm and needed for both the TU Storm and Competition robots.Competition robots.

Default installation, everything you need Default installation, everything you need

(we hope)(we hope)

Page 4: Code – Mplab – IFI loader

ScreenshotsScreenshots

Page 5: Code – Mplab – IFI loader
Page 6: Code – Mplab – IFI loader

How to codeHow to code

Various places to put things. Various places to put things.

Constants go at the top. Constants go at the top. int Speed = 10;int Speed = 10; int LftBWThreshold = 1000;int LftBWThreshold = 1000;

IntilizationIntilization Done once only, and never again.Done once only, and never again. For the FIRST competition robots, we put the For the FIRST competition robots, we put the

camera startup code in here.camera startup code in here.

Page 7: Code – Mplab – IFI loader

Two other code placesTwo other code places

Autonomous code Autonomous code Code that words only when the autonomous Code that words only when the autonomous

code is enabled. No user input. code is enabled. No user input.

Default_Routine()Default_Routine() The entirety of the robot code for the TUStorm The entirety of the robot code for the TUStorm

robots, appears here. In competition robots, robots, appears here. In competition robots, all code that’s done via the all code that’s done via the

user > joystick/buttons > controller > robotuser > joystick/buttons > controller > robot

and back goes here. Run once a program cycleand back goes here. Run once a program cycle

Page 8: Code – Mplab – IFI loader

Every program loop!Every program loop!

The execution of User_Routines is done The execution of User_Routines is done once a program cycle (19 ms)once a program cycle (19 ms)

All code has to complete in that cycle All code has to complete in that cycle (difficult to make it not do so, but can be (difficult to make it not do so, but can be done)done)

Restarts at the top of user routines and Restarts at the top of user routines and executes it again, every cycle.executes it again, every cycle.

Page 9: Code – Mplab – IFI loader

What weapons do you have?What weapons do you have?

Remember very VERY basic version of CRemember very VERY basic version of Cif ( this = that ) { do this }if ( this = that ) { do this }else { do this } else { do this }

switch(int) switch(int) case 0: { do this } case 0: { do this }

case 1: { do this } case 1: { do this } default: { do this } default: { do this }

Page 10: Code – Mplab – IFI loader

More weaponsMore weapons

printf(“some text”); printf(“some text”); printf(“some more text\n”);printf(“some more text\n”);Printf(“even more text”);Printf(“even more text”);

some textsome more textsome textsome more texteven more texteven more text

int x = 0; int y = 1; int z = 3;int x = 0; int y = 1; int z = 3;printf(“data %d %d %d\n”, x, y, z) printf(“data %d %d %d\n”, x, y, z)

data 1 2 3data 1 2 3

Page 11: Code – Mplab – IFI loader

How to make this int turn that wheelHow to make this int turn that wheel

At the top, in the constants, you define At the top, in the constants, you define what all your pwms do. what all your pwms do. RightMotor pwm07;RightMotor pwm07; LeftMotor pwm08;LeftMotor pwm08;to use in your default routines.to use in your default routines.int RightMotor = 127; (idle)int RightMotor = 127; (idle)int RightMotor = 150; (forward)int RightMotor = 150; (forward)int RightMotor = 80; (reverse)int RightMotor = 80; (reverse)Reversed for the left motor.Reversed for the left motor.

Page 12: Code – Mplab – IFI loader

All defined constantsAll defined constants// Arm// Arm#define Wrist pwm01#define Wrist pwm01#define Elbow pwm02#define Elbow pwm02#define Shoulder pwm03#define Shoulder pwm03

// Pan/Tilt// Pan/Tilt#define Pan pwm05#define Pan pwm05#define Tilt pwm06#define Tilt pwm06

// Drive// Drive#define RightMotor pwm07#define RightMotor pwm07#define LeftMotor pwm08#define LeftMotor pwm08

// Digital Inputs// Digital Inputs#define TopSw !rc_dig_in06#define TopSw !rc_dig_in06#define MidSw !rc_dig_in07#define MidSw !rc_dig_in07#define BotSw !rc_dig_in08#define BotSw !rc_dig_in08#define Banner rc_dig_in09#define Banner rc_dig_in09Adjust = (int)Adjust = (int)(Get_Analog_Value(rc_ana_in05)) / (Get_Analog_Value(rc_ana_in05)) / 4;4;

Contols the Wrist, Elbow and Shoulder of the robot arm

Pan and Tilt of the Camera

Left and Right drive motors

Three switches on the site, and the banner sensor underneath

Adjust – converts the knob into an int based signal.

Page 13: Code – Mplab – IFI loader

So now that we have that?So now that we have that?

So what cant you do? No while loops, no So what cant you do? No while loops, no for loops, no do/while loops. Basically you for loops, no do/while loops. Basically you have:have: If, else, caseIf, else, case ints, floatsints, floats And that’s itAnd that’s it You can write your own functions, for You can write your own functions, for

reusability.reusability.

Page 14: Code – Mplab – IFI loader

if ((TopSw == ON) && (MidSw == ON) && (BotSw == ON)) {if ((TopSw == ON) && (MidSw == ON) && (BotSw == ON)) { //#8 //#8 //Puts the robot back into stable position//Puts the robot back into stable position printf("Running Program #8, return robot to stable position\n" );printf("Running Program #8, return robot to stable position\n" ); RightMotor = RightIdle;RightMotor = RightIdle; LeftMotor = LeftIdle;LeftMotor = LeftIdle; Wrist = WristIdle;Wrist = WristIdle; Shoulder = ShoulderIdle;Shoulder = ShoulderIdle; Elbow = ElbowIdle;Elbow = ElbowIdle; Pan = PanIdle;Pan = PanIdle; Tilt = TiltIdle;Tilt = TiltIdle; Flag = 0;Flag = 0; }}

Page 15: Code – Mplab – IFI loader

That’s not very complicatedThat’s not very complicated

Really, from those basic commands you Really, from those basic commands you make the entire rest of the robot work.make the entire rest of the robot work.How do I make the robot do something for How do I make the robot do something for so long then stop?so long then stop?Declare a counter at the top. Declare a counter at the top. int couter = 0;int couter = 0;

Then.Then.If ( counter >= (somenumber) ) { do this } If ( counter >= (somenumber) ) { do this } else { counter++; } else { counter++; }

Page 16: Code – Mplab – IFI loader

NestingNesting

Nest to infinity (well the max limit of your Nest to infinity (well the max limit of your robots controller)robots controller)if ( this ) { do this } if ( this ) { do this } else if { do this } else if { do this } else { do this } else { do this }

or even uglieror even uglierif ( this ) { if (this) { do this } else { do this! } if ( this ) { if (this) { do this } else { do this! } else { do this } else { do this }

Page 17: Code – Mplab – IFI loader

What accessories do I have?What accessories do I have?

Two black/white sensorsTwo black/white sensorsWall follower (distance sensors)Wall follower (distance sensors)Banner SensorBanner SensorRight and Left Drive motorsRight and Left Drive motors3 Toggle switches3 Toggle switches1 Adjust knob1 Adjust knobOne Arm (wrist, elbow, shoulder)One Arm (wrist, elbow, shoulder)1 Camera1 CameraPan and Tilt on CameraPan and Tilt on CameraTwo direction/distance countersTwo direction/distance counters

Page 18: Code – Mplab – IFI loader

Now for some quirksNow for some quirks

Robot power != constant.Robot power != constant. A robot at full charge, will read certain valuesA robot at full charge, will read certain values A robot at half charge, will read those same A robot at half charge, will read those same

things as different valuesthings as different values Solution fresh charge on all cases.Solution fresh charge on all cases.

His robot works differently than mineHis robot works differently than mine They are all made the same, but all have They are all made the same, but all have

slightly different constants. Your constants for slightly different constants. Your constants for your robot are not the same as they would be your robot are not the same as they would be on another robot. on another robot.

Page 19: Code – Mplab – IFI loader

Morale of the storyMorale of the story

All hardcoded constants = badAll hardcoded constants = badRightMotor = 128;RightMotor = 128;

A better solution (recommendation)A better solution (recommendation)int rightidle = 128; (at the top) int rightidle = 128; (at the top)

RightMotor = rightidle; (in your code)RightMotor = rightidle; (in your code)

That way, if it drifts, you change one constant That way, if it drifts, you change one constant instead of several.instead of several.

Page 20: Code – Mplab – IFI loader

A sampleA sampleif ((TopSw == ON) && (MidSw == OFF) && (BotSw == ON)) {if ((TopSw == ON) && (MidSw == OFF) && (BotSw == ON)) {

printf("Running Program #6, wall following program\n" );printf("Running Program #6, wall following program\n" );//if (DistL > LLeftDstThreshold) { //if (DistL > LLeftDstThreshold) {

//RightMotor = RightIdle - 20;//RightMotor = RightIdle - 20; //LeftMotor = LeftIdle - 20;//LeftMotor = LeftIdle - 20;

//} //} //else//elseif (DistF > FrontDstThreshold) {if (DistF > FrontDstThreshold) { printf("DistF is Greater than Front Threashold\n" ); printf("DistF is Greater than Front Threashold\n" ); RightMotor = RightIdle - 15;RightMotor = RightIdle - 15; LeftMotor = LeftIdle - 15;LeftMotor = LeftIdle - 15;}}else if (DistL < LLeftDstThreshold) { else if (DistL < LLeftDstThreshold) { printf("DistL is Less than Lower Threashold\n" );printf("DistL is Less than Lower Threashold\n" ); RightMotor = RightIdle + 15;RightMotor = RightIdle + 15; LeftMotor = LeftIdle + 15;LeftMotor = LeftIdle + 15;

} } else if (DistL > ULeftDstThreshold) { else if (DistL > ULeftDstThreshold) { printf("DistL is Greater than Upper Threashold\n" );printf("DistL is Greater than Upper Threashold\n" ); RightMotor = RightIdle - 15;RightMotor = RightIdle - 15; LeftMotor = LeftIdle - 15;LeftMotor = LeftIdle - 15;}} else { else { printf("DistL is within toleration\tDistF is below threashold\n" );printf("DistL is within toleration\tDistF is below threashold\n" );

RightMotor = RightIdle + 15;RightMotor = RightIdle + 15; LeftMotor = LeftIdle - 15;LeftMotor = LeftIdle - 15;

}}}}

Page 21: Code – Mplab – IFI loader

IFI - LoaderIFI - Loader

Page 22: Code – Mplab – IFI loader

SafetySafety

Just a few reminders, these robots can Just a few reminders, these robots can breakbreak Whenever moving a motor too a new position, Whenever moving a motor too a new position,

slamming the motor full throttle can damage itslamming the motor full throttle can damage itExample: Wrist = 127; (default)Example: Wrist = 127; (default)

Wrist = 255; Wrist = 255;

A better way to do that, is to increase the wrist A better way to do that, is to increase the wrist gradually over time. Use a counter, do it over a gradually over time. Use a counter, do it over a series of loopsseries of loops

Page 23: Code – Mplab – IFI loader

Questions?Questions?

http://http://kevin.org/frckevin.org/frc//

http://www.usfirst.org/robotics/C_help.htmhttp://www.usfirst.org/robotics/C_help.htm

http://www.ifirobotics.com/docs/oi-ref-http://www.ifirobotics.com/docs/oi-ref-guide-11-21-05.pdfguide-11-21-05.pdf

http://www.ifirobotics.com/edu-rc.shtmlhttp://www.ifirobotics.com/edu-rc.shtml