beginner programming workshop simona doboli assistant professor computer science department hosftra...
TRANSCRIPT
Beginner Programming Beginner Programming Workshop Workshop
Simona DoboliSimona DoboliAssistant ProfessorAssistant Professor
Computer Science DepartmentComputer Science DepartmentHosftra UniversityHosftra University
Email: Email: [email protected]@hofstra.edu
November 17, 2007Hauppauge High School
SPBLI - FIRST
Mark McLeodMark McLeodAdvisorAdvisor
Hauppauge Team 358Hauppauge Team 358Northrop Grumman Corp.Northrop Grumman [email protected]@ngc.com
AgendaAgenda
FIRST Control SystemFIRST Control System MPLAB EnvironmentMPLAB Environment C BasicsC Basics FRC Default Code FRC Default Code
– Robot Driver ControlRobot Driver Control Demonstration of Basic OI Controls Demonstration of Basic OI Controls
– Robot Autonomous ControlRobot Autonomous Control Demonstration of Dead ReckoningDemonstration of Dead Reckoning
Hands-OnHands-On
FIRST Control SystemFIRST Control System
solenoid motor
FIRST Control ElementsFIRST Control Elements
HardwareHardware– Robot Controller (RC)Robot Controller (RC)
User Processor vs. Master ProcessorUser Processor vs. Master Processor PWM, Relay, Digital I/O, Analog, Misc.PWM, Relay, Digital I/O, Analog, Misc.
– Operator Interface (OI)Operator Interface (OI)– Tether cable / Programming cableTether cable / Programming cable– LaptopLaptop
ProgrammingProgramming– C LanguageC Language– MPLAB/mcc18 (write & compile your program)MPLAB/mcc18 (write & compile your program)– IFI_Loader (download to robot controller)IFI_Loader (download to robot controller)– Dashboard (optional)Dashboard (optional)– FIRST Default CodeFIRST Default Code
Robot Controller Robot Controller ArchitectureArchitecture User ProcessorUser Processor
– What we download toWhat we download to– Runs our codeRuns our code– Reads the digital/analog I/O directlyReads the digital/analog I/O directly
Master ProcessorMaster Processor– Controls Disable/Autonomous/Driver modesControls Disable/Autonomous/Driver modes– Disables OI inputs & most RC outputsDisables OI inputs & most RC outputs– Must hear from User periodically or shuts it downMust hear from User periodically or shuts it down– Master Code Software is updated each yearMaster Code Software is updated each year
Robot Controller I/ORobot Controller I/O
Banner Sensor
Touch Sensor
Switch
Gyroscope
CameraLimit Switch
Analog Rangefinder
Encoder
Potentiometer
Pressure Sensor
Servo
Solenoid Compressor
Analog
Digital
Motors
Relays
TeamLEDs
TTL
Getting StartedGetting Started
Create/Open an MPLAB ProjectCreate/Open an MPLAB Project Edit a FileEdit a File Build/CompileBuild/Compile Download Download
MPLAB
IFI_Loader
.hex file
Robot Controller
Operator Interface
Dashboard (optional)
C BasicsC Basics
Structure (#include, functions)Structure (#include, functions) IF, THEN, ELSEIF, THEN, ELSE WHILEWHILE FORFOR =, !=, ! >=, <=, ==, >, <, !=>=, <=, ==, >, <, != &&, ||&&, || OperatorsOperators Prototype declarationsPrototype declarations
C Basics – Structure C Basics – Structure ProgramProgram
#include <stdio.h> #include <stdio.h> // C header file – standard library function // C header file – standard library function // prototypes// prototypes
#include “myHeader.h” #include “myHeader.h” // User header file – user function prototypes// User header file – user function prototypes
int globalVar = 10; // definition global variable – can be used int globalVar = 10; // definition global variable – can be used anywhereanywhere
// in this file// in this filevoid main(void)void main(void){{
// What your program does// What your program does int result; // local variable – can be used only in main() int result; // local variable – can be used only in main()
functionfunction result = max(1, 10); // call function max: 1, 10 – actual parametersresult = max(1, 10); // call function max: 1, 10 – actual parameters}}
int max(int i, int j) // int i, int j = formal parametersint max(int i, int j) // int i, int j = formal parameters{{ // a function definition// a function definition}}
C Basics – Data TypesC Basics – Data Types
TypeType SizeSize MinMin MaxMax
charchar 8 bits8 bits -128-128 127127
unsigned charunsigned char 8 bits8 bits 00 255255
intint 16 16 bitsbits
-32,768-32,768 32,76732,767
unsigned intunsigned int 16 16 bitsbits
00 65, 53565, 535
short longshort long 24 24 bitsbits
-2^23-2^23 2^23-12^23-1
unsigned short unsigned short longlong
24 24 bitsbits
00 2^24-12^24-1
longlong 32 32 bitsbits
-2^31-2^31 2^31-12^31-1
unsigned longunsigned long 32 32 bitsbits
00 2^32-12^32-1
C Basics – A simple C Basics – A simple programprogram
#include <stdio.h>
#pragma config WDT = OFF
int max(int i, int j); // function declaration
void main(void){ int m, n, bigger; m = 10; n = 20; bigger = max(m,n); printf("m= %d, n = %d, bigger = %d”, m, n, bigger); while(1);}
//###############// Function definition//###############
int max(int i, int j){ int result; if (j > i)
result = i; else
result = j; return result;}
C Basics – if statementC Basics – if statement
if (touch == 1 if (touch == 1 &&&& light != 0 ) light != 0 )
goStraight = 1;goStraight = 1;
elseelse
goStraight = -1;goStraight = -1;
!!! NOTE: if (touch =1) vs. if (touch == !!! NOTE: if (touch =1) vs. if (touch == 1)1)
ASSIGNMENT CONDITIONASSIGNMENT CONDITION
AND Logic
OR Logic (||)
C Basics – while loopC Basics – while loop
while (touch == 0) while (touch == 0) { {
leftMotor = 5;leftMotor = 5;rightMotor = 5;rightMotor = 5;read(touch); // need to change touch read(touch); // need to change touch
insideinside // the loop// the loop
}}
// use { } if more than one statement in a // use { } if more than one statement in a looploop
C Basics - Exercise C Basics - Exercise
Write a program that computes the Write a program that computes the first n numbers in the Fibonnacci first n numbers in the Fibonnacci series: series:
0, 1, 1, 2, 3, 5, 8, 13. 0, 1, 1, 2, 3, 5, 8, 13.
n is an integer number between 0 and n is an integer number between 0 and 30.30.
C Basics - Algorithm C Basics - Algorithm
int first = 0, second = 1, third;int first = 0, second = 1, third;while (n > 0){while (n > 0){
third = first + second;third = first + second;output thirdoutput thirdfirst = second;first = second;second = third;second = third;n --;n --;
}}
FRC Default Code FlowFRC Default Code Flow
Main()Main()– User_Initialization()User_Initialization()– Do as long as we’re onDo as long as we’re on
New radio packet (slow loop)New radio packet (slow loop)– Process_Data_From_Master_uP(Process_Data_From_Master_uP(
)) Default_Routine()Default_Routine()
– As long as we’re in auto mode As long as we’re in auto mode do thisdo this
User_Autonomous_Code()User_Autonomous_Code() Fast loop stuffFast loop stuff
User Initialization
Radiopacket
?
Driver Routine
AutoMode
?
User Autonomous
NO
NO
YES
YES
FRC Default CodeFRC Default Code
Files to changeFiles to change– user_routines.c user_routines.c (to add switches & change (to add switches & change
controls)controls) Default_Routine()Default_Routine() User_Initialization()User_Initialization()
– user_routines_fast.cuser_routines_fast.c User_Autonomous()User_Autonomous() (to add autonomous (to add autonomous
movement)movement)
– user_routines.huser_routines.h (to share variables between files) (to share variables between files)
Input / OutputInput / Output
OI OI – InputsInputs
Joysticks x & y (0-254)Joysticks x & y (0-254) Buttons (0 or 1)Buttons (0 or 1) Unused inputsUnused inputs
– OutputsOutputs LEDs – lights or 3-digit readoutLEDs – lights or 3-digit readout
RC RC – OutputsOutputs
Digital I/O for sensors and devices (0 or 1)Digital I/O for sensors and devices (0 or 1) PWM for motors (0=reverse, 127=stop, 254=forward) PWM for motors (0=reverse, 127=stop, 254=forward) Don’t Don’t
use 13-16use 13-16 Relay for on/off, e.g., pneumatics (forward, reverse, stop)Relay for on/off, e.g., pneumatics (forward, reverse, stop)
– InputsInputs Analog for sensors, e.g., potentiometers (0 to 1023)Analog for sensors, e.g., potentiometers (0 to 1023)
– BothBoth TTL for devices, e.g., camera or LCD screenTTL for devices, e.g., camera or LCD screen
Sample OI ControlsSample OI Controls
(note: OI switches =0 when off and =1 when on)(note: OI switches =0 when off and =1 when on)
Sample uses:• Drive
• Control arm• Control turret
// motor controlled by switch
pwm03 = 127;if (p1_sw_trig == 1){ pwm03 = 254;}
// arm joystick
pwm03 = p3_y;
// 2-Joystick drive
pwm01 = p1_y;pwm02 = 255-p2_y;
// 1-Joystick drive
pwm01 = Limit_Mix(p1_y + p1_x - 127); pwm02 = Limit_Mix(p1_y - p1_x + 127);// Limit_Mix keeps pwm between 0 & 254
Sample OI Sample OI Input/OutputInput/Output Changing the OI LED/digital displayChanging the OI LED/digital display
// LED display on the OI
if (user_display_mode == 0) // OI lights are in use (select switch on OI){ Pwm1_green = 1; Pwm1_red = 1;}else // OI digital display is in use{ User_Mode_byte = 123;}
Sample uses:• Trim joysticks
• Display auto selected• Display backup battery voltage
Sample RC Digital Sample RC Digital InputInput Switches on the Robot Controller (RC Digital Inputs)Switches on the Robot Controller (RC Digital Inputs)(note: RC switches =1 when off and =0 when on)(note: RC switches =1 when off and =0 when on)
Sample uses:• Limit switches
• Autonomous selection• Field starting position
• Pressure sensor
// Autonomous selection
if (rc_dig_in01 == 1){ Autonomous_1();}else{ Autonomous_2();}
// Limit Switch on arm
pwm03 = p3_y;if ((rc_dig_in01 == 1) && (pwm03 > 127)){ pwm03 = 127;}
Sample RC Analog Sample RC Analog InputInput Analog Inputs on the RC (e.g., a potentiometer)Analog Inputs on the RC (e.g., a potentiometer)
// Use of arm position sensor (potentiometer) to limit arm movementint armpot;
pwm01 = p3_y;armpot = Get_Analog_Value(rc_ana_in03); // value will be 0-1023
if ( (armpot > 900) && (pwm01 > 127) ){ pwm01 = 127; // Neutral}else if (armpot < 100) && (pwm01 < 127) ){ pwm01 = 127; // Neutral}
Sample uses:• Position sensors
• Gyroscope• Analog rangefinder
Sample RC Relay Sample RC Relay OutputOutput Forward / Reverse / NeutralForward / Reverse / Neutral
relay8_fwd = 1;relay8_rev = 0;
relay8_fwd = 0;relay8_rev = 1;
relay8_fwd = 0;relay8_rev = 0;
// Single pneumatic solenoid (valve) relay8_fwd = p1_sw_trig; relay8_rev = 0;
// Double pneumatic solenoid (valve) relay8_fwd = p1_sw_trig; relay8_rev = !p1_sw_trig;
// Turn on Compressor when Pressure Switch on digital input 18 says so relay8_fwd = !rc_dig_in18; relay8_rev = 0;
Sample uses:• Pneumatic valves
• Compressor on/off• On/off motors
Robot Autonomous Robot Autonomous ControlControl Must be developed under identical Must be developed under identical
environment as in competitionenvironment as in competition Training the robot must be repeated over Training the robot must be repeated over
& over to get right& over to get right Develop code using old robots or Develop code using old robots or
prototypesprototypes Plan on multiple autonomous optionsPlan on multiple autonomous options Use as few distinct movements as possibleUse as few distinct movements as possible
Sample AutonomousSample Autonomous
// Drive forward and stop
Sample_Auto_1(){ static int counter=0;
if (counter<100) {
counter++;pwm01 = 200;pwm02 = 54; //motor is reversed
} else // Make sure it always stops {
pwm01 = 127;pwm02 = 127;
}}
May need function prototype:void Sample_Auto_1(void);
Common IssuesCommon Issues
Syntax and typosSyntax and typos Program & Data SpaceProgram & Data Space Code Too Slow trying to do too muchCode Too Slow trying to do too much Joystick trimJoystick trim Embedded math (variable overflow)Embedded math (variable overflow) Infinite or too large loopsInfinite or too large loops Variables keep values until you change Variables keep values until you change
themthem Unfortunately, it does exactly what Unfortunately, it does exactly what
you tell it to doyou tell it to do
Hands-OnHands-On
Vex RobotsVex Robots Timed AutonomousTimed Autonomous Sensors for Advanced StudentsSensors for Advanced Students
1.1. Vex w/encoder, turret, gyroVex w/encoder, turret, gyro2.2. Vex w/cameraVex w/camera3.3. Vex w/pot controlled armVex w/pot controlled arm4.4. Vex w/rangefinders (IR, sonic)Vex w/rangefinders (IR, sonic)5.5. Vex w/swerve driveVex w/swerve drive6.6. Edu w/bumper sensorEdu w/bumper sensor
ReferencesReferences
Programming Quick StartProgramming Quick Start FIRST RC Reference GuideFIRST RC Reference Guide FIRST OI Reference GuideFIRST OI Reference Guide www.chiefdelphi.com/forums www.chiefdelphi.com/forums
Programming forumProgramming forum
Reference BooksReference Books
C For Dummies, Volume OneC For Dummies, Volume One C For Dummies, Volume TwoC For Dummies, Volume Two C Programming - A Modern
Approach - K N King ““The C Programming Language” -The C Programming Language” -
Kernighan & RitchieKernighan & Ritchie ““The C Answer Book” - Kernighan & The C Answer Book” - Kernighan &
RitchieRitchie
Presentation slides at: Presentation slides at:
www.cs.hofstra.edu/~sdoboliwww.cs.hofstra.edu/~sdoboli
oror
Team358.orgTeam358.org
Questions/Help please email us. Questions/Help please email us.
[email protected]@hofstra.edu
[email protected]@ngc.com