-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyRobot.cpp
More file actions
143 lines (118 loc) · 3.76 KB
/
MyRobot.cpp
File metadata and controls
143 lines (118 loc) · 3.76 KB
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
#include "WPILib.h"
/**
* This is our current robot code, including networking
*/
class RobotDemo : public SimpleRobot
{
RobotDrive myRobot; // robot drive system
Joystick stick; // only joystick
Encoder digEncoder;
AnalogChannel ultra;
public:
RobotDemo(void):
myRobot(8, 1, 9, 2),
stick(1),
digEncoder(13, 14),
ultra(1, 1)
{
myRobot.SetExpiration(0.1);
myRobot.SetInvertedMotor(myRobot.kFrontRightMotor, true);
//myRobot.SetInvertedMotor(myRobot.kFrontLeftMotor, true);
myRobot.SetInvertedMotor(myRobot.kRearRightMotor, true);
//myRobot.SetInvertedMotor(myRobot.kRearLeftMotor, true);
}
/**
* Drive left & right motors for 2 seconds then stop
*/
void Autonomous(void)
{
myRobot.SetSafetyEnabled(false);
}
/*
Runs the motors with arcade steering.
*/
void OperatorControl(void)
{
NetTest();
return;
myRobot.SetSafetyEnabled(true);
digEncoder.Start();
const double ppsTOrpm = 60.0/250.0; //Convert from Pos per Second to Rotations per Minute by multiplication
// (See the second number on the back of the encoder to replace 250 for different encoders)
const float VoltsToIn = 41.0; // Convert from volts to cm by multiplication (volts from ultrasonic).
// This value worked for distances between 1' and 10'.
while (IsOperatorControl())
{
if (stick.GetRawButton(4)) {
myRobot.MecanumDrive_Cartesian(stick.GetX(), stick.GetY(), -1);
}
else if (stick.GetRawButton(5))
{
myRobot.MecanumDrive_Cartesian(stick.GetX(), stick.GetY(), 1);
}
else
{
myRobot.MecanumDrive_Cartesian(stick.GetX(), stick.GetY(), 0);
}
myRobot.MecanumDrive_Cartesian(stick.GetX(), stick.GetY(), 0);
SmartDashboard::PutNumber("Digital Encoder RPM", abs(digEncoder.GetRate()*ppsTOrpm));
SmartDashboard::PutNumber("Ultrasonic Distance inch", (double) ultra.GetAverageVoltage()*VoltsPerInch);
SmartDashboard::PutNumber("Ultrasonic Voltage", (double) ultra.GetAverageVoltage());
Wait(0.1);
}
digEncoder.Stop();
}
void NetTest() {
// Network testing
// learned from github.com/jacob9706/HighTekerz2012/
// and Beej's Networking Guide
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;
int sockAddrSize;
int sockFd;
SmartDashboard::PutString("Data Recvd", "initializing");
sockAddrSize = sizeof(struct sockaddr_in);
bzero((char *) &serverAddr, sockAddrSize);
serverAddr.sin_family = AF_INET;
serverAddr.sin_len = (u_char) sockAddrSize;
serverAddr.sin_port = htons(1140);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if ((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) == ERROR) {
perror("Socket");
SmartDashboard::PutString("Data Recvd", "socket error");
return;
}
if (bind(sockFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) {
perror("bind");
SmartDashboard::PutString("Data Recvd", "bind error");
return;
}
SmartDashboard::PutString("Data Recvd", "waiting...");
int bytesRecvd = 0;
char buf[256];
while (IsOperatorControl()) {
memset(buf, 0, sizeof(buf));
if ((bytesRecvd = recvfrom(sockFd, buf, 255, 0, (struct sockaddr *) &clientAddr, &sockAddrSize)) == ERROR) {
perror("recv");
SmartDashboard::PutString("Data Recvd", "recv");
break;
}
SmartDashboard::PutString("Data Recvd", buf);
}
SmartDashboard::PutString("Data Recvd", "Done");
close(sockFd);
}
/**
* Runs during test mode
*/
void Test() {
}
float abs(float num){
if (num < 0) {
return (-num);
} else {
return (num);
}
}
};
START_ROBOT_CLASS(RobotDemo);