Robot C Remote Control For this activity we
Robot. C Remote Control For this activity we will use the Squarebot
Learning Objectives: Focusing on Virtual World with Physical Examples • Understand Real-Time Joystick Mapping • Understand how to use timers • Understand how to incorporate buttons into controlling robot arms
Getting Started… Configuring the motors for Squarebot Only configure the motors for now.
Looking at the Joysticks on the Remote: Physical Robot Ch 1 Right = 127 Middle = 0 Left = -127 Ch 4 Right = 127 Middle = 0 Left = -127 Ch 3 Up = 127 Middle = 0 Down = -127 Ch 2 Up= 127 Middle = 0 Down = -127
Joystick Mapping: Physical <- Ch 4 -> Up - Ch 3 - Dn Joystick Mapping: Virtual x 1 y 1 <- Ch 1 -> Up - Ch 2 - Dn x 2 y 2 Note: If you copypaste these into your program, you will need to retype in the “”. //Place before task main() #pragma debugger. Windows(“joystick. Simple”); #include “Joystick. Driver. c”; Channel Left/Down Middle Right/Up vex. RT[Ch 1] -127… 0 … 127 vex. RT[Ch 2] -127… 0 … 127 vex. RT[Ch 3] -127… 0 … 127 vex. RT[Ch 4] -127… 0 … 127 //Place inside the loop prior to joystick. Command get. Joystick. Settings(joystick); Channel Left/Down Middle Right/Up joystick. joy 1_x 2 -127… 0 … 127 joystick. joy 1_y 1 -127… 0 … 127 joystick. joy 1_x 1 -127… 0 … 127
Accessing the Value for the Remote Commands Virtual Physical vex. RT[Channel. Number] //Place before task main() #pragma debugger. Windows(“joystick. Simple”); #include “Joystick. Driver. c”; //Place inside the loop prior to ‘joystick. ’ Command get. Joystick. Settings(joystick); joystick. joy#_axis#
Example Using the Remote Values to Drive the Motors Physical Robot These examples assume that the programmer labeled their motors left. Motor and right. Motor. Can also send the value directly to the motor port. motor[port 3] = vex. RT[Ch 3]; Virtual World
Online Time: Configure the motors and code the following Physical Robot Make sure the motors are configured: left. Motor, port 3, reversed right. Motor, port 2 Virtual World
Driving in the Virtual World • Compile and Download the Program • Select Virtual World (Utility – Huge Table is good for starters) • Open ‘Joystick Control –Basic ‘ Debugger Window’ • Robot-> Debugger Windows -> ‘Joystick Control – Basic’ • Refresh List if the remote does not show up.
Robot Creeping?
Robot Creeping • Y 1 and Y 2 values might not go exactly to ‘ 0’ when you release the buttons which can cause your robot to creep. • Can correct this in the code. • Pseudo Code • If the joystick reading is close to 0, say +/- 20 • Give a 0 power value to the motor • Else • Give the joystick reading to the motor
A Little Robot. C Math to Help Robot. C Function Description Example abs() Finds the absolute value of a number float x; x = abs(5 -10); pow() Calculates a power float x; x = pow(10, 3); //Calculates and returns 10^3 sqrt() Finds the square root of a number float x; x = sqrt(8);
Using a variable to make threshold changes easier Physical: Getting Rid of the Creep Using the abs command to simplify the condition. if (vex. RT[Ch 3] >(-threshold)) && (vex. RT[Ch 3] < (threshold)) Would give the same results. Executes this line of code when the above condition is true. Executes the commands in the ‘else’ when the above condition is false. Do the same for the right. Motor
Virtual Getting Rid of the Creep Add the pragma directive and include file. If you copy and paste from the Power. Point you will need to retype in the “”. Add the get. Joystick. Settings(joystick); command inside the while loop. Replaced vex. RT(Ch 3) with joystick. joy 1_y 1 Replaced vex. RT(Ch 2) with joystick. joy 1_y 2
More Control Options • To fight motors timing out, you can modify the drive code to lower the power sent to the motors. • Go half-power • Create a fancy equation that maps remote input to output. Had some math wizzes that used a 5 th degree polynomial to provide more control when going slow. • Can put together a bunch of ‘stepped’ if elses to give different power values for different ranges of input values.
Physical No Creep, Half Power = more control Half Power
Virtual No Creep Half Power: Online Time: Test it on the Utilities -> Huge Table Half Power
Robot. C Timers: Using an Internal Timer to Stop the Robot • Robot. C has 4 Timers (T 1, T 2, T 3, T 4) • Commands • clear. Timer(T 1); //Resets timer T 1 to 0. • time 1[T 1]; // Returns the amount of time in milliseconds since the last reset.
Physical Using Timer to limit how long you can drive Will let the user drive for 5 seconds (5000 milliseconds) Instead of while(true) which goes forever, this loop will only repeat when T 1 is less than 5000.
Virtual World With Timer Instead of while(true) which goes forever, this loop will only repeat when T 1 is less than 5000.
Online Time • Complete and test the drive code with the Anti-Creep code • Test to see if the code works in the Utility -> Huge Table • In the Virtual Worlds go to the Remote Control Tab • Bull in the Ring: Knocking out cans in < 10 sec • Robo Slalom I: Driving through a course. A good time to practice refining remote to controller mapping to make driving more accurate • Round up: How many laps can you complete in one minute? The code will need to stop you from driving at the one minute mark! (Use timers)
Buttons • Learning Objectives • Be able to use the buttons to control motors on your robot. • Complete challenges that incorporate buttons.
Joystick Buttons: Physical Buttons return a value of ‘ 1’ when pushed and ‘ 0’ when not pushed Button Description Example 5 U Top button on back left vex. RT[Btn 5 U] 5 D Bottom button, back left vex. RT[Btn 5 D] 6 U Top button, back right vex. RT[Btn 6 U] 6 D Bottom button, back right vex. RT[Btn 6 D] 7 U Button 7 up vex. RT[Btn 7 U] 7 D Button 7 down vex. RT[Btn 7 D] 7 R Button 7 right vex. RT[Btn 7 R] 7 L Button 7 left vex. RT[Btn 7 L] 8 U Button 8 up vex. RT[Btn 8 U] 8 D Button 8 down vex. RT[Btn 8 D] 8 R Button 8 right vex. RT[Btn 8 R] 8 L Button 8 left vex. RT[Btn 8 L]
Using the buttons to control the arm motor • First we need to go to Motors and Sensors setup to configure the arm and claw motor. • Clawbot 1) Name and set the claw and arm motors. 2) Reverse the Arm Motor for Virtual Clawbot. Might need to reverse for physical robot also. • Arm: Port 7 • Claw: Port 6 • Robot -> Motors and Sensors setup 3) Click Apply and OK when finished.
Looking at Arm Control using buttons: Pseudo-Code • If button 6 U is pushed • raise the arm (Send a signal of 127) • Else if button 6 D is pushed • Lower the arm (Send a signal of -127) • Else • Stop the arm (Send a signal of 0)
Looking at the Arm: Pseudo-Code to Code • If button 6 U is pushed • raise the arm (Send a signal of 127) • Else if button 6 D is pushed • Lower the arm (Send a signal of -127) • Else • Stop the arm (Send a signal of 0) Style Note: Indent between the {} to make the code easier to read.
Virtual World Buttons joy 1 Btn(6) joy 1 Btn(10) joy 1 Btn(9) joy 1 Btn(5) joy 1 Btn(8) joy 1 Btn(1) joy 1 Btn(7) joy 1 Btn(4) Mode joy 1 Btn(3) joy 1 Btn(2) joy 1 Btn(11) joy 1 Btn(12) joy 1_Top. Hat 7 0 1 6 -1 2 5 4 3
Joystick Buttons Virtual World joy 1_Top. Hat 7 0 1 6 -1 2 5 4 3 Buttons return a value of ‘ 1’ when pushed and ‘ 0’ when not pushed, except the Top. Hat. Button Description Example 1 Left joy 1 Btn(1) 2 Bottom joy 1 Btn(2) 3 Right joy 1 Btn(3) 4 Top joy 1 Btn(4) 5 Back, top left joy 1 Btn(5) 6 Back, top right joy 1 Btn(6) 7 Back, bottom left joy 1 Btn(7) 8 Back, bottom right joy 1 Btn(8) 9 Small button, top left joy 1 Btn(9) 10 Small button, top right joy 1 Btn(10) 11 Left joystick button joy 1 Btn(11) 12 Right joystick button joy 1 Btn(12) Top. Hat Returns values -1 (Not pushed) or 0, joystick. joy 1_Top. Hat 1, … 7 depending on which part is pushed.
Back to the Arm Movement Pseudo-Code but for Virtual Remote • If button 6 is pushed • raise the arm (Send a signal of 127) • Else if button 8 is pushed • Lower the arm (Send a signal of -127) • Else • Stop the arm (Send a signal of 0)
Arm Pseudo-Code to Code: Virtual World • If button 6 is pushed • raise the arm (Send a signal of 127) • Else if button 8 is pushed • Lower the arm (Send a signal of -127) • Else • Stop the arm (Send a signal of 0)
Where does this code go? Since you want the robot to continually check for the buttons being pressed, it needs to go inside the while(true) loop.
Online Time: Test Arm Movement • Implement the code for the arm movement and test it in the Virtual or Real World.
Any problems? • Arm floating down when button not pushed? • How can you combat this?
Claw Motor (For clawbot) Virtual • Pseudo Code • If the back, top, left button is pushed • Close the claw (127) • Else if the back-bottom-left button is pushed • Open the claw (-127) • Else • Leave the claw (0) Physical
What do you recall about…? #pragma debugger. Windows(“joystick. Simple”); #include “Joystick. Driver. c”;
Challenge using Buttons • Minefield Challenge: • Lift the bad mines (Orange Balls) and place them in the safety zones (blue boxes)
- Slides: 36