Title: ADVANCED BRAIN Training
1ADVANCEDBRAIN Training
Presented by
Matt Lapolla matthew_at_mattandsuz.com
2Topics for this class
- Why program your BRAIN?
- Vocabulary
- Useful References
- BRAIN Software Rev 3.pdf
- IR Communications
- More functionality than just 4 channels
- Selection Stick
- State Machines
- Readjusting the driving joystick
- Debugging
- LEDs
- Serial Communication
- Example
3Why program your BRAIN?
- Make Things Simpler for the driver and designers
- Automate operations
- Use the BRAIN to its maximum potential
- Have better control of your robot
4Why program your BRAIN
- Dont Over complicate the controls
- Your drivers have understand the controls
- Cant put stickers, tape, etc on transmitter
5Vocabulary
- String vs. Number
- Integer Number
- Real Number
- Proportional
6Vocabulary
- How you see the number 13
- 13
- How the computer sees the number 13
- 1101 lt- this is a number
- How the computer displays the number 13
- Character(49) and Character(51)
- 1 and 3
- 13 lt-this is a string of 2 characters
7Vocabulary
- Integers 1, 54, 742, 34532
- Real 1.233, .3532, 33453.22. 1.00
8Vocabulary
- Proportional is the same as variable
- Think of a dimmer switch in your house
- Non-Proportional is On or Off
- Think of a normal light switch in your house
9IR Communications
- The IR transmitter (on your robot)
- Is optional But can help the points
- Sends commands to the game field
- Uses a modified servo port
- You will not be able to verify until mall day or
game day. - ALWAYS bring your most current program with you
to mall day and game day! - Laptop
- Thumb drive
- CD
10IR Communications
- To use the IR transmitter you need to change the
way the servo port operates. - The code looks like this
- Declare Variables and Initialize our port
- unsigned short now
- ...
- int main( void )
- InitBrain() // initialize the BRAIN
- nowgetClock(0) // Give 1/5 sec
- while(getClock(now)lt10) // delay for CP
- setServoRange(0,180) //if 0 is IR port
11IR Communications
- Now we need to use our IR commands
- Commands are numbers sent to our IR port
- Read the rules on page 21
- To send the command 300 to the IR port
- setServo(0,300) // if 0 is our IR port
- To send the command 900 to the IR port
- setServo(0,900) // if 0 is our IR port
12More functionality than 4
- More functionality than just 4 channels?
13Selection stick
- The Transmitter has 4 proportional channels
14Selection stick
15Selection stick
16Selection stick
17Selection stick
18Selection stick
- So you should only be able to control 4 things
with our transmitter. - Is it possible to control 5 things with
proportional channels?
19Selection stick
- YES
- Instead of using channel 2 as a proportional
control of a motor or servo, we use it as a
selection switch. - It will select what the right joystick (channel 0
and 1) control.
20Selection stick
- For example If the left joystick is up, then the
right joystick controls the drive motors
21Selection stick
- For example If the left joystick is DOWN, then
the right joystick controls the robotic arm
22Selection stick
- So now we have 5 proportional controls with only
4 channels - 4 on the right stick (depending on the up/down
position of the left stick) - And one more left stick side to side.
- A total of 5!
23Selection stick
- The code for this looks something like this
- if(getRcValue(2,DEADBAND,125)lt512)
- // Do this if the stick is in the Upper half
- else
- // Do this if the stick is in the Lower half
Left Stick Up and down
24Selection stick
- If we use the entire left stick (channels 2 and
3) - We can easily create 9 sensitive areas.
25Selection stick
- Separate the left stick in to a 3x3 grid
26Selection stick
- The vertical and horizontal limits of the
joystick are 0 to 1023
0
1023
0
1023
27Selection stick
- If we integer divide the vertical and horizontal
limits of the joystick by 342 we will get a range
of 0, 1, and 2
0
0
1
2
28Selection stick
- Now we combine the row and column into one
number. 00, 01, 02, 10, 11, 12, 20, 21 or 22 - Row10Column
0
0 (00)
1 (01)
2 (02)
11
10
12
21
20
22
29Selection stick
- The code to make 9 sensitive areas looks like
this
30 //Get and determine Vertical cell of Left
joystick Row getRcValue(2,8,125)/342
//determine Horizontal cell of left Joystick.
Column getRcValue(3,8,125)/342 Cell
Row10 Column // Combine the cell //
Determine what to do in each cell of the left
joystick switch (Cell) case 0 // Left
Joystick in the Upper Left corner // Put
your code here break
case 1 // Left Joystick in the Upper Center
Corner // Put your code here break
case 2 // Left Joystick in the upper Right
corner // Put your code here
break case 10 // Left Joystick in the
Center Left // Put your code here
break case 11 // Left Joystick
in the Center Center // Put your code here
break case 12 //
Left Joystick in the Center Right // Put
your code here break
case 20 // Left Joystick in the Lower Left
// Put your code here break
case 21 // Left Joystick in the Lower Center
// Put your code here break
case 22 // Left Joystick in the Lower Center
// Put your code here
break
31Selection stick
- With this arrangement we could have 18
proportional channels or..
32Selection stick
- Any combination of Proportional channels and
other functions
33State Machines
- What other functions?
- Non-proportional channels
- For Example Open the hand, close the hand
Open Hand
Close Hand
34State Machines
- What other functions?
- State Machines
Close Hand then Lower Arm then Tip
Bucket then Etc...
35State machines
- State Machines execute a series of actions
- They can be time based
- Turn on Motor 0, wait 3 seconds
- Then, Set servo 3 to 800 and wait for 4 seconds
- Then...
- Or they can use some feed back (input)
- Turn on motor 0 until switch 1 is pushed
- Then turn on motor 1 until switch 2 is pushed
36State machines
- A timed state machine code looks like this
37//To turn on your sequence Seq1 0 //Set
the first sequence number DelaySeq1 0
//Enable the timer . . . //To turn off your
sequence DelaySeq1 65535 //This is the highest
clock value . . . //See if time for next
sequence if(getClock(LastClockSeq1) gt
DelaySeq1) LastClockSeq1 getClock(0)
//Reset Timer for next sequence switch(Seq1)
//Choose next sequence case 0
// Put your code here
DelaySeq1 150 // set delay 50 1 second
Seq1 1 // Set Next Sequence to
execute break
case 1 // Put your code here
DelaySeq1 150 // set delay 50 1 second
Seq1 2 // Set Next Sequence
break case 2
// Put your code here DelaySeq1
150 // set delay 50 1 second Seq1
3 // Set Next Sequence
break case 3
// Put your code here DelaySeq1 250
// set delay 50 1 second Seq1 4
// Set Next Sequence break
case 4 // Put your
code here DelaySeq1 DelayOff //
This stops the timer Seq1 0
// Reset Sequence Number break
38State machines
- A feedback state machine code looks like this
39// To turn on the state machine then // Start
Sequence ArmSeq 1// Enable Sequence Seq2
0 // Pick an initial condition
movement -----------------------------------------
------------------------ // To turn off the
sequence ArmSeq 0 // Stop Sequence ----------
--------------------------------------------------
----- //Check to see if the Arm Sequence is
being used. If so then.... if (ArmSeq 1)
// Check to see if at any limit switches.
If so switch direction if(getSwitch(0)1)
Seq2 0 if(getSwitch(1)1)
Seq2 1 // Select the
direction based on Sequence switch(Seq2)
case 0 ArmMotor 430 break
case 1 ArmMotor 594 break
40Readjusting the drive joystick
- When is forward not forward?
- When you dont use your BRAIN
41Readjusting the drive joystick
- Normally to drive forward you need the left motor
and right motor at full power.
42Readjusting the drive joystick
- But this is not very intuitive.
- There is a better way!
43Readjusting the drive joystick
- By creating a simple function that you call, it
will adjust your steering for you. - You pass it the steer value (usually channel 0)
- And the Speed Value (usually channel 1)
- It will return to you the adjusted motor values
for the right and left motors
44Readjusting the drive joystick
- void remapDrive(unsigned int steerValue, unsigned
int speedValue, - unsigned int rightMotor, unsigned int
leftMotor) -
- int xInput, yInput // declare some
signed integers - int rightValue,leftValue
-
- xInputsteerValue-RCIDLEVAL // grab the
input values - yInputspeedValue-RCIDLEVAL // and make into
delta -
- leftValueyInputxInput // combine the
input to create - rightValueyInput-xInput // output delta
values - // bound the delta values (remember RCIDLEVAL is
half RCMAXVAL) - if (leftValuegtRCIDLEVAL) leftValueRCIDLEVAL
// becomes RCMAXVAL - if (leftValuelt-RCIDLEVAL) leftValue-RCIDLEVAL
// becomes zero - if (rightValuegtRCIDLEVAL) rightValueRCIDLEVAL
- if (rightValuelt-RCIDLEVAL) rightValue-RCIDLEV
AL -
- // output the values and include the idle offset
45Debugging
- What happens when my program does not operate
correctly?
46Debugging
- This is called Debugging.
- The BRAIN has 2 useful debugging tools.
- The first are 8 LEDs that you can turn on with
your program. - I will use this to help me determine where the
program is going.
47Debugging
- Here is a sample code for turning LEDs on
- setSingleLED(0) // Turn on 1st LED
- setSingleLED(1) // Turn on 2nd LED
- setSingleLED(2) // Turn on 3rd LED
- setSingleLED(3) // Turn on 4th LED
- setSingleLED(4) // Turn on 5th LED
- setSingleLED(5) // Turn on 6th LED
- setSingleLED(6) // Turn on 7th LED
- setSingleLED(7) // Turn on 8th LED
- writeLED(255) // Turn on all LEDs
48Debugging
- Here is a sample code for turning LEDs off
- clearSingleLED(0) //Turn off 1st LED
- clearSingleLED(1) //Turn off 2nd LED
- clearSingleLED(2) //Turn off 3rd LED
- clearSingleLED(3) //Turn off 4th LED
- clearSingleLED(4) //Turn off 5th LED
- clearSingleLED(5) //Turn off 6th LED
- clearSingleLED(6) //Turn off 7th LED
- clearSingleLED(7) //Turn off 8th LED
- writeLED(0) //Turn off all LEDs
49Debugging USB
- There is also a more advance debugging tool.
- The BRAIN can display information on a PC.
- You simply build up a string that you want to
send to the PC then call the outHostsz procedure.
50Debugging USB
- // Declare 2 strings for your USB message
- //buf1 is a temporary string for converting
numbers - char buf110
- // outbuf is string to be transmitted to the host
PC - char outbuf70 // max of 70 characters, or
change num - // just after the main while(1) loop
- // Clear out the display string before each loop
- outbuf0 '\0'
- // To add a message with out variables
- strcat(outbuf,My Message") // Add words to
message - // To add a variable Cell to the message
- itoa(Cell,buf1) // Convert the var Cell to
text - strcat(outbuf,buf1) // Add new text to buffer
51Debugging USB
- //Lastly you need to send out your completed
message - strcat(outbuf,"\r\n") // add carriage return
line feed - outHostsz(outbuf) // send outbuf to the USB
port - You will need a program on the PC to receive your
message. - Have IAR workbench installed on the PC (included
on DVD) - Use Realterm (included on your DVD)
- Settings
- Display Tab
- ansi
- Port Tab
- BAUD is 9600
- Port is (that varies from computer to computer,
Try the largest number you have listed) - OPEN button is pressed in
- Pins Tab
- DTR Press Clear Button
- RTS Press Clear Button
52Example
- Here is an example program that I came up with.
- The program in its entirety will not be helpful
to you, but some of the examples used may help
you get started planning your own program.
53Example
- The start to every program you need to decide
what you want it to do. - So let me outline what my program does.
- The Right Joystick controls the drive motors
- Note I dont have any drive motors hooked up
- The Right Joystick controls the arm.
- The left joystick was separated in to 9 sensitive
areas
54Example
- If the left stick is in the upper 3rd
- Then the right stick is used for drive wheels
I control the drive wheels
55Example
- If the left joystick is in the Center 3rd
- Then the right joystick up and down, controls the
arm
I control the arm
56Example
- If the left stick is in the Bottom 3rd
- Then the right up and down controls the arm with
parabolic_scale
I control the arm with parabolic_scale
57Example
- If the stick is in the top right then
- Raise Red, Yellow, Green and Blue Flags
- Example of a switch
58Example
- If the stick is in the top left then
- Lower Red, Yellow, Green and Blue Flags
- Example of a Switch
59Example
- Start a timed state machine
- First raise the Red flag then wait 3 seconds
- Then raise the Yellow flag then wait 3 seconds
- Then raise the Green flag then wait 3 seconds
- Then raise the Blue flag then wait 5 seconds
- Then lower all flags
- Example of a timed state machine
- If the stick is in the center left then
60Example
- Slowly rotate arm until limit switch
- Change direction until limit switch
- Repeat
- Example of a State Machine with feedback
- If the stick is in the Lower Left then
61Example
- Stop the arm State Machine
- If the stick is in the Lower Right then
62Look at the code
63What did we miss?