forked from AlexMaxseiner/FTC-Team4592-2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutonomous.c
159 lines (152 loc) · 4.11 KB
/
Autonomous.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma config(Hubs, S1, HTMotor, none, none, none)
#pragma config(Sensor, S3, irseek, sensorI2CCustom)
#pragma config(Sensor, S1, , sensorI2CMuxController)
#pragma config(Sensor, S2, light, sensorLightActive)
#pragma config(Motor, mtr_S1_C1_1, left, tmotorTetrix, openLoop, encoder)
#pragma config(Motor, mtr_S1_C1_2, right, tmotorTetrix, openLoop, reversed, encoder)
/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Autonomous Mode Code Template
//
// This file contains a template for simplified creation of an autonomous program for an Tetrix robot
// competition.
//
// You need to customize two functions with code unique to your specific robot.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////
#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.
#include "blackMagic.h"
float numofrotations;
float targetencodervalue;
int count = 0;
const int IRDIST = 16;
bool atLine(float value);
void ClockWiseTurn(float degrees);
void forward(float distance);
void CounterClockWiseTurn(float degrees);
task countline();
task main()
{
forward(7);
//waitForStart(); // Wait for the beginning of autonomous phase.
/* while(true)
{
if(atLine(SensorValue[light]))
{
unsigned int d = HTIRS2readACDir(irseek);
if(3 < d && d < 7)
break;
wait1Msec(500);
}
else
{
motor[left] = 50;
motor[right] = 50;
}
}*/
//clockwise turn or counterclockwise turn in placeRing();
//PlaceRing();
while (true)
{}
}
task countline()
{
float value = 0;
while(true)
{
value = SensorValue[light];
if(atLine(value))
{
count++;
wait1Msec(750);
}
}
}
bool atLine(float value)
{
if(value > 45)
{
return true;
}
return false;
}
void forward(float distance) {
const float CHANGE = 2;
int totalTraveled = 0;
float RotationsNeeded = distance / 7.817;
float encoderTarget = RotationsNeeded * 1225;
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
float leftEncoder;
float rightEncoder;
float leftPower = 50;
float rightPower = 100;
while(abs(totalTraveled) < abs(encoderTarget))
{
leftEncoder = nMotorEncoder[leftDrive];
rightEncoder = nMotorEncoder[rightDrive];
if(leftEncoder > rightEncoder)
{
leftPower -= CHANGE;
rightPower += CHANGE;
}
else if(leftEncoder < rightEncoder)
{
leftPower += CHANGE;
rightPower -= CHANGE;
}
motor[leftDrive] = leftPower;
motor[rightDrive] = rightPower;
totalTraveled += (leftEncoder+rightEncoder)/2;
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
wait1Msec(200);
}
motor[leftDrive] = 0;
motor[rightDrive] = 0;
nMotorEncoder[rightDrive] = 0;
nMotorEncoder[leftDrive] = 0;
}
void CounterClockWiseTurn(float degrees)
{
const float PERDEGREE = 30/90.0;
const float LINKSPERSPROKET = 20;
numofrotations = (PERDEGREE * degrees )/LINKSPERSPROKET;
long leftEncoder = 0;
long rightEncoder = 0;
nMotorEncoder[left] = 0;
nMotorEncoder[right] = 0;
targetencodervalue = numofrotations * 1447;
motor[left] = 50;
motor[right] = -50;
while((abs(leftEncoder) < targetencodervalue) || (abs(rightEncoder) < targetencodervalue)){
leftEncoder = nMotorEncoder[left];
rightEncoder = nMotorEncoder[right];
if (abs(rightEncoder) >= targetencodervalue)
motor[right] = 0;
if (abs(leftEncoder) >= targetencodervalue)
motor[left] = 0;
}
}
void ClockWiseTurn(float degrees)
{
const float PERDEGREE = 30/90.0;
const float LINKSPERSPROKET = 20;
numofrotations = (PERDEGREE * degrees )/LINKSPERSPROKET;
long leftEncoder = 0;
long rightEncoder = 0;
nMotorEncoder[left] = 0;
nMotorEncoder[right] = 0;
targetencodervalue = numofrotations * 1447;
motor[left] = -50;
motor[right] = 50;
while((abs(leftEncoder) < targetencodervalue) || (abs(rightEncoder) < targetencodervalue))
{
leftEncoder = nMotorEncoder[left];
rightEncoder = nMotorEncoder[right];
if (abs(rightEncoder) >= targetencodervalue)
motor[right] = 0;
if (abs(leftEncoder) >= targetencodervalue)
motor[left] = 0;
}
}