Introduction to RobotC

32
Programming in RobotC Introduction to

description

An introduction to RobotC given by the Say Watt robotics team. The example files shown here are also posted online: http://github.com/SayWatt

Transcript of Introduction to RobotC

Page 1: Introduction to RobotC

Programming in RobotC

Introduction to

Page 2: Introduction to RobotC

Setting Menu Level

Open Up the Advanced Options

Page 3: Introduction to RobotC

Connecting Bluetooth

Page 4: Introduction to RobotC

Connecting Bluetooth

Page 5: Introduction to RobotC

Connecting Bluetooth

1234

Page 6: Introduction to RobotC

Connecting Bluetooth

Page 7: Introduction to RobotC

Connecting Bluetooth

Page 8: Introduction to RobotC

Sample Programs

RobotC: File -> Open Sample Program

Say Watt: http://github.com/SayWatt

Page 9: Introduction to RobotC

First ProgramLet’s make the robot move!

task main() {motor[leftMotor] = 100;motor[rightMotor] = 100;

wait1Msec(2000);

motor[leftMotor] = 0;motor[rightMotor] = 0;

}

Page 10: Introduction to RobotC

task main() {

Tells RobotC: Start Here!

Page 11: Introduction to RobotC

motor[leftMotor] = 100;

motor[rightMotor] = 100;

Make leftMotor andrightMotor go forward

at full speed.

Page 12: Introduction to RobotC

wait1Msec(2000);

Wait 2000 milliseconds.(2 seconds)

Page 13: Introduction to RobotC

motor[leftMotor] = 0;

motor[rightMotor] = 0;

Make leftMotor andrightMotor stop.

Page 14: Introduction to RobotC

}

Tells RobotC: We’re done.

Page 15: Introduction to RobotC

Up Next: Touch Sensor

task main() {motor[leftMotor] = 100;motor[rightMotor] = 100;

while(SensorValue(touch) != 1) {wait1Msec(50);

}

motor[leftMotor] = 0;motor[rightMotor] = 0;

}

Page 16: Introduction to RobotC

while(SensorValue(touch) != 1) {

“Keep looping through thisuntil the value of‘touch’ equals 1.”

Checks this

Page 17: Introduction to RobotC

What’s Next?

Try to make these changes:1. Move backwards for 2 sec.

after the button is pressed.

2. Try turning around and goingthe other way.

Page 18: Introduction to RobotC

Light Sensortask main() { motor[leftMotor] = 100; motor[rightMotor] = 100;

while(SensorValue(light) <= 45) { wait1Msec(50); }

motor[leftMotor] = 0; motor[rightMotor] = 0;}

Page 19: Introduction to RobotC

Functions

Call Function Function Does JobFunction Returns

Value

Page 20: Introduction to RobotC

Basic Functionsvoid startMotors() { motor[leftMotor] = 100; motor[rightMotor] = 100;}

void stopMotors() { motor[leftMotor] = 0; motor[rightMotor] = 0;}

Page 21: Introduction to RobotC

Passing Valuesvoid setMotors(int speed) { motor[leftMotor] = speed; motor[rightMotor] = speed;}

Examples:setMotors(100);setMotors(0);

or

Page 22: Introduction to RobotC

Light Sensor Function

bool checkLightSensor() { if(SensorValue(light) <= 45) { return true; }else { return false; }}

True / False

Page 23: Introduction to RobotC

Line Following

Use the light sensorto follow a white

line.

Page 24: Introduction to RobotC

Joysticks

Page 25: Introduction to RobotC

3.00 Joystick Bug

Open the JoystickDriver.c

Change line 123 to:const int kMaxSizeOfMessage = 19;

Page 26: Introduction to RobotC

Analog Sticks

joystick.joy1_y1;joystick.joy1_x1;

joystick.joy1_y2;…..

Page 27: Introduction to RobotC

Buttons

joy1Btn(1);joy1Btn(2);

Returns a bool.

Page 28: Introduction to RobotC

Basic Joystick Code

task main() { while(true) { getJoystickSettings(joystick);

motor[leftMotor] = joystick.joy1_y1; motor[rightMotor] = joystick.joy1_y2;

wait1Msec(20); }}

Page 29: Introduction to RobotC

“Joystick Drift”

Page 30: Introduction to RobotC

Fixing Driftint leftMotorValue = joystick.joy1_y1;int rightMotorValue = joystick.joy1_y2;

if(abs(leftMotorValue) < 20) { leftMotorValue = 0;}

if(abs(rightMotorValue) < 20) { rightMotorValue = 0;}

Page 31: Introduction to RobotC

Wait For Start

Page 32: Introduction to RobotC

Wait For StartSetup

waitForStart();

Teleop