blob: ecbdf8b2f2ffff4ae749fe085061772067d98d81 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/atom_code/output/MotorOutput.h"
2
3#include <math.h>
4
5#include "aos/common/Configuration.h"
6#include "aos/aos_core.h"
7#include "aos/common/byteorder.h"
8#include "aos/common/control_loop/Timing.h"
9#include "aos/atom_code/messages/DriverStationDisplay.h"
10
11namespace aos {
12
13MotorOutput::MotorOutput() : sock(NetworkPort::kMotors,
14 configuration::GetIPAddress(configuration::NetworkDevice::kCRIO)) {}
15
16void MotorOutput::Run() {
17 while (true) {
18 time::PhasedLoopXMS(5, 1000);
19 RunIteration();
20
21 // doesn't matter if multiple instances end up running this loop at the same
22 // time because the queue handles the race conditions
23 while (true) {
24 const aos::DriverStationDisplay *line = aos::DriverStationDisplay::GetNext();
25 if (line != NULL) {
26 AddDSLine(line->line, line->data);
27 LOG(DEBUG, "got a line %hhd that said '%s'\n", line->line, line->data);
28 aos::DriverStationDisplay::Free(line);
29 } else {
30 break;
31 }
32 }
33
34 if (sock.SendHoldMsg() == -1) {
35 LOG(WARNING, "sending outputs failed\n");
36 continue;
37 } else {
38 LOG(DEBUG, "sent outputs\n");
39 }
40 }
41}
42
43int MotorOutput::AddMotor(char type, uint8_t num, float value) {
44 if (sock.hold_msg_len_ + 4 > sock.MAX_MSG) {
45 return -1;
46 }
47 sock.hold_msg_[sock.hold_msg_len_ ++] = type;
48 sock.hold_msg_[sock.hold_msg_len_ ++] = num;
49 to_network(&value, &sock.hold_msg_[sock.hold_msg_len_]);
50 sock.hold_msg_len_ += 4;
51 return 0;
52}
53int MotorOutput::AddSolenoid(uint8_t port, bool on) {
54 if (sock.hold_msg_len_ + 3 > sock.MAX_MSG) {
55 return -1;
56 }
57 sock.hold_msg_[sock.hold_msg_len_ ++] = 's';
58 sock.hold_msg_[sock.hold_msg_len_ ++] = port;
59 sock.hold_msg_[sock.hold_msg_len_ ++] = on ? 1 : 0;
60 return 0;
61}
62
63int MotorOutput::AddDSLine(uint8_t line, const char *data) {
64 size_t data_len = strlen(data); // doesn't include terminating NULL
65 if (sock.hold_msg_len_ + 3 + data_len > sock.MAX_MSG) {
66 return -1;
67 }
68
69 sock.hold_msg_[sock.hold_msg_len_ ++] = 'd';
70 sock.hold_msg_[sock.hold_msg_len_ ++] = line;
71 sock.hold_msg_[sock.hold_msg_len_ ++] = data_len;
72 memcpy(&sock.hold_msg_[sock.hold_msg_len_], data, data_len);
73 sock.hold_msg_len_ += data_len;
74 return 0;
75}
76
77} // namespace aos
78