Introduction to RobotC
-
Upload
emjeney -
Category
Technology
-
view
2.698 -
download
0
description
Transcript of Introduction to RobotC
Programming in RobotC
Introduction to
Setting Menu Level
Open Up the Advanced Options
Connecting Bluetooth
Connecting Bluetooth
Connecting Bluetooth
1234
Connecting Bluetooth
Connecting Bluetooth
Sample Programs
RobotC: File -> Open Sample Program
Say Watt: http://github.com/SayWatt
First ProgramLet’s make the robot move!
task main() {motor[leftMotor] = 100;motor[rightMotor] = 100;
wait1Msec(2000);
motor[leftMotor] = 0;motor[rightMotor] = 0;
}
task main() {
Tells RobotC: Start Here!
motor[leftMotor] = 100;
motor[rightMotor] = 100;
Make leftMotor andrightMotor go forward
at full speed.
wait1Msec(2000);
Wait 2000 milliseconds.(2 seconds)
motor[leftMotor] = 0;
motor[rightMotor] = 0;
Make leftMotor andrightMotor stop.
}
Tells RobotC: We’re done.
Up Next: Touch Sensor
task main() {motor[leftMotor] = 100;motor[rightMotor] = 100;
while(SensorValue(touch) != 1) {wait1Msec(50);
}
motor[leftMotor] = 0;motor[rightMotor] = 0;
}
while(SensorValue(touch) != 1) {
“Keep looping through thisuntil the value of‘touch’ equals 1.”
Checks this
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.
Light Sensortask main() { motor[leftMotor] = 100; motor[rightMotor] = 100;
while(SensorValue(light) <= 45) { wait1Msec(50); }
motor[leftMotor] = 0; motor[rightMotor] = 0;}
Functions
Call Function Function Does JobFunction Returns
Value
Basic Functionsvoid startMotors() { motor[leftMotor] = 100; motor[rightMotor] = 100;}
void stopMotors() { motor[leftMotor] = 0; motor[rightMotor] = 0;}
Passing Valuesvoid setMotors(int speed) { motor[leftMotor] = speed; motor[rightMotor] = speed;}
Examples:setMotors(100);setMotors(0);
or
Light Sensor Function
bool checkLightSensor() { if(SensorValue(light) <= 45) { return true; }else { return false; }}
True / False
Line Following
Use the light sensorto follow a white
line.
Joysticks
3.00 Joystick Bug
Open the JoystickDriver.c
Change line 123 to:const int kMaxSizeOfMessage = 19;
Analog Sticks
joystick.joy1_y1;joystick.joy1_x1;
joystick.joy1_y2;…..
Buttons
joy1Btn(1);joy1Btn(2);
…
Returns a bool.
Basic Joystick Code
task main() { while(true) { getJoystickSettings(joystick);
motor[leftMotor] = joystick.joy1_y1; motor[rightMotor] = joystick.joy1_y2;
wait1Msec(20); }}
“Joystick Drift”
Fixing Driftint leftMotorValue = joystick.joy1_y1;int rightMotorValue = joystick.joy1_y2;
if(abs(leftMotorValue) < 20) { leftMotorValue = 0;}
if(abs(rightMotorValue) < 20) { rightMotorValue = 0;}
Wait For Start
Wait For StartSetup
waitForStart();
Teleop