Guide To Basic RobotC Programming - Seaquam...

23
By Charlie Friend, 2013 Example programs written with ROBOTC 3.54 Guide To Basic RobotC Programming V. 1.0

Transcript of Guide To Basic RobotC Programming - Seaquam...

Page 1: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

By Charlie Friend, 2013 Example programs written with ROBOTC 3.54

Guide To Basic RobotC Programming V. 1.0

Page 2: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

1

Table of Contents The Basics…………………………………………………………………………………………………2

o Plugs and Wires..............................................................................................................2 o The Stuff You’re Plugging In And How It Works………………………………..3

Your First Program....…………………………………………………………………………………4 o Getting Set Up…………………………………………………………………………………4 o Basic Movement……………………………………………………………………………...5 o Stopping...………………………………………………………………………………............6 o Turning…...........................................................................................................................6

Variables….......................................................................................................................................7 o Types of Variables.........................................................................................................7 o Declaring Variables…...................................................................................................8 o Manipulating Variables............................................................................................10 o Basic Trig/Function Calling…………………………………………………………...11

“If” Statements, “For” And “While” Loops.......................................................................12 o “If” Statements.............................................................................................................12 o “Else” Statements........................................................................................................12 o “And” and “Or” in “If” Statements........................................................................13 o “For” Loops……………………………………………………….………………………….14 o “While” Loops……………………………………………………………………………….14

Sensors………………………………………………………………………...…………………………15 o Limit and Bumper Switches…………………………………………………………..15 o Potentiometer………………………………………………………………………………15 o Optical Shaft Encoder……………………………………………………………………15 o Light Sensor………………………………………………………………………………….16 o Line Follower………………………………………………………………………………..16 o Ultrasonic Sensor………………………………………………………………………….16 o Yaw Rate Gyro………………………………………………………………………………17 o Analog Accelerometer…………………………………………………………………...17

Driver-Op Control……………………………………………………………………………………18 o Partner Joystick...........................................................................................................19

Drive System Controls.............................................................................................................20 o Tank-type Controls....................................................................................................20 o Arcade-Style Controls...............................................................................................21 o Mecanum Drive…………………………………………………………………………….22

Page 3: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

2

The Basics: Plugs and Wires In programming with VEX, it’s pretty important to know exactly what you’re

programming and where it’s plugged in on the microcontroller before you start. If you can, it’s better to label your motors and sensors with a piece of masking tape so you’re not trying to follow wires later.

1. Motor Ports: Motor ports are, quite simply, where motors are plugged in. If you’re using a two-wire motor, make sure you’re using a motor controller. Note: VEX decided that motor ports 1 and 10 are special, so motor controllers are not needed for those two ports.

2. Analog Ports: Analog ports are for sensors that simply return a voltage that is interpreted by the microcontroller and turned into a numerical value to be used in programming.

3. Digital Ports: Digital ports are for sensors that collect actual numerical data and then send that data to the microcontroller.

4. Speaker Port: Used to plug in the VEX speaker (see page ____)

Motor Ports

Analog Ports

Digital Ports

Speaker Port

Used with VEX LCD Screen

Page 4: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

3

The Basics Continued: The Stuff You’re Plugging In and How It Works

1. Motors: Motors make your robot move. They can be programmed to spin forwards and backwards at a particular percentage of their maximum power (and thus a particular rate) by being assigned a number from -127 to 127 (0 is stopped, -127 is reversed at full power and 127 is forwards at full power). Motors come in 2 distinct flavors: the 269 (shown to the left) and the 393 (provides 60% more power than a 269).

2. Sensors:

Sensors provide the programmer with the ability to have the robot “see” or “touch” something by returning numbers to the microcontroller. These numbers can be programmed so that the robot can react to certain situations without the help of the driver. Though these are generally used for autonomous in competition, they can also be used to automate tasks for the driver during the driver-control section of competition.

3. Servos: They look like motors, they smell like motors, they might even taste like motors (please don’t taste them), but servos are in fact different from motors and have a different purpose. Servos will allow the programmer to position a part of the robot without having to spin a motor and use sensors to figure out where the part is. However, they can only rotate 100 degrees.

Page 5: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

4

Your First Program

Getting Set Up: After starting ROBOTC, you’ll be shown a basic start page. Just go into the “File” menu, go to “New…” and select “New File”. You’ll be presented with

this:

Now before we actually tell the robot to do anything, we need to tell the robot which motors are plugged in and where. To do that, go to “Robot” “Motors and Sensors

Setup” and a window will pop up.

Page 6: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

5

In the configuration described above, the Front Left wheel is plugged into port 2, the Back Left in port 3, etc. (Note: some motors may have to be reversed, depending on

how they’re installed) After you’ve clicked OK, your code will be updated to look like this:

After you’ve got all this set up, you can start writing your program.

Basic Movement: Now it’s time for the fun part – actually making the robot do stuff.

To begin with, we’ll do a basic forward movement program:

This program will simply make the robot go forward at full speed indefinitely.

The code generated which you’ll later reference in your program – it’s best to just edit it through the window, though you can edit the text itself if you want to show off.

All motors: forward at full power

“//” causes anything in front of it to be ignored by the compiler. This is called “commenting”, and it’s REALLY important to use it for larger programs by taking note of what a particular portion of the code does. This makes it a lot easier for another programmer to see what you’re doing as well.

This is where the robot takes it’s instructions from.

Anything in between these two brackets will be done over and over again while the program is running.

Page 7: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

6

Stopping: OK, so you can make the robot go forward. But now, it’s going to need to stop (a robot that just goes forward will do you nothing but harm in competition). In order to do that, we’ll first need to tell the robot how long it needs to go forward for, and then set the motor powers to zero. The program will look something like

this:

*Note – this programming will keep cycling unless you create a variable to stop it. Use

a Boolean variable and an “if” statement to stop the program from cycling (see “Variables” section).

Turning: To make a robot go places and navigate, you’ll need to know how to make a turn autonomously. To make a turn, instead of all motors going forward with full

power you’re going to reverse the two motors on the side that you’re turning towards. A basic turning program will look like this:

Forward

STAHP

wait1Msec(2000) – freezes the computer for 2 seconds, then reads the next lines of code

Full power

Keep all settings – 2000 milliseconds (2 seconds)

All motors set to 0

Forward for 2 seconds

Forward for 2 seconds

Turn right for 1 second and stop

Page 8: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

7

Variables Types of Variables: So now that you know how to get the robot to move, the next

step is how to create more complex sequences of commands for the robot and manipulate the different values being used to control the robot. To do that, we use variables: different values that are remembered by the robot in the programming.

The types of variables you’ll use the most with ROBOTC are listed below:

Boolean (bool) – Boolean values are the most basic of variables, as they are either true or false. These work well for having a robot remember if it has done a task, or record if a bumper switch has been pressed or not. Examples: true, false

Integer (int) – Positive and negative values (as well as 0) without decimal points. These are used to assign motor powers and are returned from sensors. Examples: 8, 19, -5, 6, -13

Floating Point Decimal (float) – Positive and negative values with decimal places. These can be handy if you’re trying to do trig or just dividing. Examples: 1.07, 8.255, -2.34, 3.14159

String (string) – Words or phrases. These are not generally used in the logic of a program, but if you are using an LCD screen, they are used to display messages. Examples: “Distance to target: 14 inches”, “I like turtles.”

If you try to give a variable the wrong type of value, ROBOTC will have a minor

tantrum (meaning an error message). If you’re struggling with what type of variable you need, just think of the robot being asked about the variable.

Incorrect Variable Type Usage: Correct Variable Type Usage:

Page 9: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

8

Declaring Variables: Before you start giving variables values in your program, you have to tell the robot what types of variables you are using, and give those variables

names. Creating variable names may seem like a very basic task (and it ALMOST is), but there are some rules that apply to variable names that will cause a minor explosion of error messages in your program. These rules are:

1. A variable name cannot have spaces in it. It’ll just cause serious syntax issues when you’re coding anyway.

2. Variable names cannot have symbols in them. No question marks, hyphens, periods, exclamation marks, etc.

3. Variables must not start with numbers. They can end with numbers, but starting will cause mind-numbing confusion for ROBOTC.

4. There are certain words used to represent functions in ROBOTC. Do not name your variables after them. I could list them all, but there are far too many to know, so if you see an error message regarding the variable name and you’ve done everything else right, try changing the name a little bit.

Now that we know the rules of variable naming, we will create a variable to track our average motor power (what the use of this is, I’m not entirely sure). We will call the variable avgpower, and it will be an integer value (the same as the powers being assigned to the motors). There are 3 different types of variable declarations in ROBOTC: Global Variables: Global variables exist in every part of the code. They remain the same even after a cycle of code has been completed, as they are not defined in the main task itself. They are defined between the main task and the motors and sensors code. #pragma config(Motor, port2, FLWheel, tmotorVex269, openLoop)

#pragma config(Motor, port3, BLWheel, tmotorVex269, openLoop)

#pragma config(Motor, port4, FRWheel, tmotorVex269, openLoop)

#pragma config(Motor, port5, BRWheel, tmotorVex269, openLoop)

//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

int avgpower;

task main() { }

Space.

Type of variable

Name of variable

Page 10: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

9

Local Variables: Local variables are used only temporarily, and are only remembered by the robot during the process it is defined in (between two curvy brackets). They can be used in “if” statements (which you’ll learn about in the next section) if the variable is only needed when a certain credential is met. #pragma config(Motor, port2, FLWheel, tmotorVex269, openLoop)

#pragma config(Motor, port3, BLWheel, tmotorVex269, openLoop)

#pragma config(Motor, port4, FRWheel, tmotorVex269, openLoop)

#pragma config(Motor, port5, BRWheel, tmotorVex269, openLoop)

//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main() { int avgpower; }

Constants: Constants are numbers remembered by the computer that cannot be changed. They are used as a shortcut for programmers, so that the programmer does not have to constantly (heh, bad puns) keep writing down the same number over and over again. The programmer can also change the constant quickly instead of going into the program and replacing every single instance of the same number. #pragma config(Motor, port2, FLWheel, tmotorVex269, openLoop)

#pragma config(Motor, port3, BLWheel, tmotorVex269, openLoop)

#pragma config(Motor, port4, FRWheel, tmotorVex269, openLoop)

#pragma config(Motor, port5, BRWheel, tmotorVex269, openLoop)

//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#define AVG_POWER 80;

task main() {

}

A variable, if declared between two curvy brackets, will be disposed of at the end of the process. In the case of task main(), the process simply restarts and the variable is reset.

Instead of defining what type of variable, “#define” will create a constant.

To give a constant a value, simply type the value next to the name of a constant.

Page 11: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

10

Manipulating Variables: Being able to assign variables is about as simple as declaring them. Different variables in ROBOTC (and any other programming language) can be added, divided, multiplied, etc. allowing for all sorts of fun

solutions to programming problems. To demonstrate this, we’ll go back to our useless “average power” variable from the last section. Now of course, to calculate the average motor power for this robot the

equation will be:

𝑆𝑢𝑚 𝑜𝑓 𝑎𝑙𝑙 𝑚𝑜𝑡𝑜𝑟 𝑝𝑜𝑤𝑒𝑟𝑠

𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑚𝑜𝑡𝑜𝑟𝑠

Easy, right?

Now, to calculate the avgpower variable, the code will look like this (we’ll be making

it a global variable): #pragma config(Motor, port2, FLWheel, tmotorVex269, openLoop) #pragma config(Motor, port3, BLWheel, tmotorVex269, openLoop) #pragma config(Motor, port4, FRWheel, tmotorVex269, openLoop) #pragma config(Motor, port5, BRWheel, tmotorVex269, openLoop) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// int avgpower; task main() { avgpower = (motor[BLWheel] + motor[BRWheel] + motor[FLWheel] + motor[FRWheel]) / 4 ; }

So in that you can see how you might calculate a variable in ROBOTC. Of course, all of these numbers are calculated using BEDMAS, so it’s just basic math. To multiply

divide, add, subtract, etc. are all done using operators. The most commonly used mathematical operators in ROBOTC are used below:

1. Addition (+) – About as basic as it gets. Adds two numbers together. 2. Subtraction (-) – Again, unbelievably simple. Subtracts two numbers. 3. Multiplication (*) - *Yawns* Multiplies two numbers. 4. Division (/) – Divides two numbers 5. Powers (^) – Used the same as any other operator. Instead of 22, you would

write (2^2).

Variable declaration

Sum of all motor powers Total number of motors

Page 12: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

11

Basic Trigonometry/Function Calling: Trigonometry may not be something that you might think you would need to use in the programming aspect of robotics, but it

can provide some good solutions to otherwise difficult problems.

The functions that ROBOTC uses for trig are generally in radians, but there are sine, cosine and tangent functions that can be used with degrees. To get the sine ratio of a particular number of degrees (in this case we’ll say 64°), you can simply use this

line of code: { sinofangle = Sindegrees(64); }

So now, we have a sine ratio that does ABSOLUTELY NOTHING. Hooray. In actual competition though, these can be handy for the detection of elevated

obstacles. Consider this scenario:

Your robot has an ultrasonic sensor mounted to the robot at a 56° angle in order to

detect the trough. The ultrasonic sensor is returning a value of 2176mm. Using these two values, we can figure out how far we are from the obstacle using this

simple algorithm: task main() { distancetoobstacle = SensorValue[ultrasonic] * cosDegrees(56); } Though you won’t be using it a lot, trig in a programming can help you out a lot with

navigation in a competition environment.

2176mm

Distance to target

56°

Page 13: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

12

“If” Statements, “For” and “While” Loops “If” Statements: After declaring variables and manipulating them in ROBOTC, there really isn’t a whole lot you can do with them, aside from putting a string value on an LCD screen or assigning a motor power. “If” statements provide an integral part in the programming of just about any robot you’ll ever build with VEX, as they allow

the robot to only execute a certain portion of code if a certain credential is met.

For this program, the robot will only move forward if the “forward” variable is true.

bool forward;

task main()

{

forward = true; if (forward == true) {

motor[FLWheel] = 127; motor[BLWheel] = 127; motor[BRWheel] = 127; motor[FRWheel] = 127; }

}

“Else” Statements: “Else” statements allow you to account for all the possible

outcomes that you didn’t account for in your “if” statements.

Let’s say you had an integer variable in your program, “distance”. If the value of that integer was greater than 2, you would want the robot to go forward. If the value

were less than -2, you would want the robot to go backward. Anywhere in between, the robot is close enough and can stop. However, it’d be pretty painful to make a

separate “if” statement for the values of -1, 0 and 1. This is where an “else” statement comes in.

int distance; task main() { distance = 8; if (distance >= 2) { motor[FLWheel] = 127; motor[FRWheel] = 127; motor[BLWheel] = 127; motor[BRWheel] = 127; }

Variable is true

Only if the variable “forward” is true will the code between the brackets be executed. If we had set the variable to false, the code would have been ignored and the robot would not move.

The robot moves forward if the distance is greater than 2.

Page 14: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

13

else if (distance <= 2) { motor[FLWheel] = -127; motor[FRWheel] = -127; motor[BLWheel] = -127; motor[BRWheel] = -127; } else { motor[FLWheel] = 0; motor[FRWheel] = 0; motor[BLWheel] = 0; motor[BRWheel] = 0; } } “And” and “Or” in “If” Statements: Often when dealing with a full-on programming for a robot of any sort, you tend to be dealing with multiple variables, sensor values,

etc. feeding into the same function.

Here’s a simple program where two variables are needed in order to make the robot go forward:

bool forward1; bool forward2; task main() forward1 = true; forward2 = true; if (forward1 == true && forward2 == true) { motor[FLWheel] = 127; motor[FRWheel] = 127; motor[BLWheel] = 127; motor[BRWheel] = 127; } }

It also might be necessary to have the robot move if one of the two variables are true. The “if” statement for that would look like this:

if (forward1 == true || forward2 == true)

The robot moves backward if the distance is less than 2.

In any other scenario, the robot will stop. In this case, the only other values the integer could be are 0, 1 and -1.

“and” Both variables must be true for the robot to go forward.

“or”

Page 15: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

14

“For” Loops: “For” loops are a way of making your program shorter and less confusing when your robot has to do the same thing over and over again.

task main() { int i; for (i = 0; i <= 5; i++) { //Do stuff here. } } In this case, the robot will actually repeat the code 6 times, because the loop counter

started at 0.

“While” Loops: “While” loops basically function the same as “for” loops, but except counting they simply continue looping until a certain condition is met. In the

program below, the code under the loop will not be read until the variable “go” is true.

bool go; task main() { go = false; while (go == false) { //Do stuff here. } }

Starting number Counting to five

Counting up

Code is read

At this point, the loop counter is increased by 1. If the counter equals 5, the software breaks out of the loop. If not, it returns to the top of the loop.

Loop counter

The code will be read until this value changes. In this case, nothing will modify the variable and the program will never break out of the loop.

Page 16: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

15

Sensors Sensors return values to the microcontroller, they can be used exactly like variables.

The values can be accessed using one line of code:

SensorValue[nameofyoursensor]

Depending on the sensor you’re using, different values will be returned.

Limit and Bumper Switch Sensor type: digital Variable type returned: Boolean Limit and bumper switches are the most basic sensors you’ll use programming a VEX robot. If the switch is pressed down, the sensor will return true. If the switch is not pressed down, the switch will return false.

Potentiometer

Sensor type: analog Variable type returned: integer Potentiometers measure 250° of rotation in a shaft that is put through the sensor. The integer values can sometimes be a little tricky to figure out, so it’s generally better to figure them out using live values. On one end, the sensor usually returns about 1000, and on the other around 3000.

Optical Shaft Encoder

Sensor type: digital Variable type returned: integer Optical shaft encoders measure rotation in “counts” (juan, two, tree). In one rotation of an OSE, there are 90 counts, meaning 4° of rotation for every count. Optical shaft encoders can be used on drive systems because they will not stop rotating at any point.

Page 17: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

16

Light Sensor Sensor type: sensor Variable type returned: integer Light sensors return an integer value showing how bright the area the sensor is pointing at is. You could use it to have a robot move forward if a certain area is bright enough, execute a programming when the lights get turned on, etc.

Line Tracker Sensor type: analog Variable type returned: integer Line followers are effectively light sensors, but are sold by VEX in packs of 3 in order to (guess what?) follow lines. A line follower will return a number between 0 and 255, 0 being the brightest and 255 being the darkest. Using 3 line followers will allow you to track whether your robot is to the left or

right of a line by telling you how much light the line followers are sensing. For example, if you were following a black line, your middle line follower would ideally return 255 and your left and right line followers would return 0. Ultrasonic Sensor

Sensor type: digital Variable type returned: integer Ultrasonic sensors use “echolocation” to find the distance between the robot and an object. You can choose whether you want the sensor to return distances in cm, mm or inches in the motors and sensors setup window.

L

M

R Bright Bright

Dark Robot is on track

Page 18: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

17

Yaw Rate Gyro Sensor type: analog Variable type returned: integer Yaw rate gyros measure rotation in tenths of a degree (30° = 300). These are super handy when you need to make precise turns in an autonomous programming, as you don’t have to rely on shaft encoders to figure out how far your robot has turned.

Analog Accelerometer

Sensor type: analog Variable type returned: floating point decimal The analog accelerometer is used to measure acceleration in the form of G-force. The sensor measures this on 3 axes. Given that 1.0G is equivalent to the force of gravity on an object, the z axis (up and down, provided the sensor is mounted flat) will register a constant 1.0G when the robot is at rest. The accelerometer’s sensitivity can be adjusted using a jumper.

Jumpers Used Sensitivity

None ±1.5g

1 only ±2.0g

2 only ±4.0g

1 and 2 ±6.0g

Page 19: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

18

Driver-Op Control The VEXNet controller is actually really easy to program with, as it returns values just like a sensor would. These values are accessed using the VexRT function and

the button or axis’ particular name.

motor[wheel] = VexRT[nameofaxisgoeshere];

Axes: Axes return a number from 127 to -127, 127 being full forward and -127 being full back (kinda like motors…huh). The numbers for these are labeled on the

controller, and you access their values using vexRT[Ch(channel#)].

If you were to assign axis 2 to your front left drive motor, this would be the code:

motor[FLWheel] = vexRT[Ch2];

Buttons: Buttons return boolean values, accessed by the function vexRT[btn(button number)]. In a competition program, if you wanted a function

to be triggered when a button is pressed just use an “if” statement. If you were using button 8L, the “if” statement would look like this:

if (vexRT[btn8L] == true) { //your function goes here }

Axes

Buttons

Page 20: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

19

Partner Joystick: Partner joysticks can be used in competition when two drivers are needed to control a robot. They are connected via a tether cable to a VEXNet

controller and look virtually identical to one – expect there’s no USB port.

See? Same deal.

In programming with a partner joystick, the partner joystick is referred to as

“Transmitter 2”. To access the values, it’s as simple as adding “Xmtr2” to the end of the motor and button names in your vexRT command.

Let’s try controlling the front left drive motor with the partner joystick, just like we

did in the last section:

motor[FLWheel] = vexRT[Ch2Xmtr2];

It’s that simple.

The only difference between a partner joystick and normal joystick in programming

Page 21: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

20

Drive System Controls It may seem relatively minimal in the grand scheme of designing, building the robot

for competition, testing and writing autonomous functions, but the controls your driver uses to move the robot around in competition can really make or break a

match for you. If your driver isn’t comfortable with how your robot drives, he or she is not going to be able to react quickly and might end up in a bad situation that way.

In this portion of writing your programming, it’s really important to consult with

your driver and ask him or her how the robot should respond to his/her commands. Ultimately, if you want to maximize your points in competition being able to move

the robot has to be a reflex for the driver.

Because of this, you’ll probably end up improving your own controller configuration, but these standard configurations are a good place to base your

programming off of.

Tank-Type Controls: Tank-type controls are probably the most basic way you can control your robot. The left joystick (channel 3) controls the left side of the robot

and the right joystick (channel 2) controls the right side of the robot.

The code for this is just as basic: task main() { while (true) { motor[FLWheel] = vexRT[Ch3]; motor[BLWheel] = vexRT[Ch3]; motor[FRWheel] = vexRT[Ch2]; motor[BRWheel] = vexRT[Ch2]; } }

Every motor is directly assigned to one axis

Page 22: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

21

Arcade-Style Controls: Arcade-style controls can be good to use because they use only one joystick to control the motion of the robot. This opens up the other joystick

for controlling a lifting mechanism, etc.

Full forward on one joystick will make the robot go forward at full power. The difficulty with arcade style is causing the motors on one side of the robot to

decrease power as you move the joystick in that direction. By the time that you have to joystick all the way to the left, the left motors should be in full reverse and

the right motors should still be going full power forward.

For the code, we’re going to need to write something just a little more complicated:

task main() { while(true) { motor[FLWheel] = vexRT[Ch3] + vexRT[Ch4]; motor[BLWheel] = vexRT[Ch3] + vexRT[Ch4]; motor[FRWheel] = vexRT[Ch3] - vexRT[Ch4]; motor[BRWheel] = vexRT[Ch3] - vexRT[Ch4]; } }

The motor values are calculated from the two axes, so that when the joystick is full right the robot is turning right, etc.

Page 23: Guide To Basic RobotC Programming - Seaquam …seaquamrobotics.wikispaces.com/file/view/Programming+guide.pdf · Guide To Basic RobotC Programming ... Motor Ports: Motor ports are

22

Mecanum Drive: Mecanum wheels are used on VEX robots to eliminate the need for a strafe wheel.

Mecanum wheel values are calculated using a particular algorithm. Frankly, I have no idea how or why this algorithm works, and I don’t think very many people do – but the fact of the matter is that it does. It assigns both axes of the left joystick for

forward motion and strafing, and the left-right axis of the right joystick for sideways movement. Of course, you can substitute these axes in the code to customize the

controls.

The algorithm is as follows: motor[FRWheel] = vexRT[Ch3] - vexRT[Ch1] - vexRT[Ch4]; motor[BRWheel] = vexRT[Ch3] - vexRT[Ch1] + vexRT[Ch4]; motor[FLWheel] = vexRT[Ch3] + vexRT[Ch1] + vexRT[Ch4]; motor[BLWheel] = vexRT[Ch3] + vexRT[Ch1] - vexRT[Ch4];

Forward/backward axis Rotation axis Strafe axis