Skip to content

Commit f447aee

Browse files
committed
can over tcp version 1;
1 parent 7c37fa1 commit f447aee

File tree

4 files changed

+162
-4
lines changed

4 files changed

+162
-4
lines changed

include/barrett/bus/can_over_tcp_socket.h

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class CANOverTCPSocket : public CommunicationsBus
3333
*/
3434
CANOverTCPSocket();
3535
CANOverTCPSocket(int port) throw(std::runtime_error);
36+
CANOverTCPSocket(const char *ip, const char *port) throw(std::runtime_error);
3637
~CANOverTCPSocket();
3738
/** getMutex() method gets and locks interthread data exchange assuring nothing critical is happening in either thread.
3839
*/
@@ -61,6 +62,9 @@ class CANOverTCPSocket : public CommunicationsBus
6162

6263
mutable unsigned char packetData[64];
6364

65+
const char *ip_;
66+
const char *port_;
67+
6468
private:
6569
DISALLOW_COPY_AND_ASSIGN(CANOverTCPSocket);
6670
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Copyright 2019-2119 Wingkou <[email protected]>
3+
*/
4+
5+
/**
6+
* @file can_over_tcp_main_function.h
7+
* @date 10/22/2019
8+
* @author Wingkou
9+
*/
10+
11+
#ifndef CAN_OVER_TCP_MAIN_FUNCTION_H_
12+
#define CAN_OVER_TCP_MAIN_FUNCTION_H_
13+
14+
#include <barrett/exception.h>
15+
#include <barrett/products/product_manager.h>
16+
#include <barrett/bus/bus_manager.h>
17+
#include <barrett/systems/wam.h>
18+
19+
#include "can_over_tcp_socket.h"
20+
21+
#ifdef BARRETT_SMF_VALIDATE_ARGS
22+
bool validate_args(int argc, char **argv);
23+
#endif
24+
25+
#ifdef BARRETT_SMF_CONFIGURE_PM
26+
bool configure_pm(int argc, char **argv, ::barrett::ProductManager &pm);
27+
#endif
28+
29+
#ifndef BARRETT_SMF_NO_DECLARE
30+
template <size_t DOF>
31+
int wam_main(int argc, char **argv, ::barrett::ProductManager &pm, ::barrett::systems::Wam<DOF> &wam);
32+
#endif
33+
34+
#ifndef BARRETT_SMF_DONT_WAIT_FOR_SHIFT_ACTIVATE
35+
#define BARRETT_SMF_WAIT_FOR_SHIFT_ACTIVATE true
36+
#else
37+
#define BARRETT_SMF_WAIT_FOR_SHIFT_ACTIVATE false
38+
#endif
39+
40+
#ifndef BARRETT_SMF_DONT_PROMPT_ON_ZEROING
41+
#define BARRETT_SMF_PROMPT_ON_ZEROING true
42+
#else
43+
#define BARRETT_SMF_PROMPT_ON_ZEROING false
44+
#endif
45+
46+
#ifndef BARRETT_SMF_WAM_CONFIG_PATH
47+
#define BARRETT_SMF_WAM_CONFIG_PATH NULL
48+
#endif
49+
50+
int main(int argc, char **argv)
51+
{
52+
if (argc < 3)
53+
{
54+
std::cerr << "Usage: your_program_name <host> <port> [other options]\n";
55+
return 1;
56+
}
57+
58+
// Give us pretty stack-traces when things die
59+
::barrett::installExceptionHandler();
60+
61+
// #ifdef BARRETT_SMF_VALIDATE_ARGS
62+
// if (!validate_args(argc, argv))
63+
// {
64+
// return 1;
65+
// }
66+
// #endif
67+
68+
::barrett::ProductManager pm(NULL, new ::barrett::bus::BusManager(new ::barrett::bus::CANOverTCPSocket(argv[1], argv[2])));
69+
// #ifdef BARRETT_SMF_CONFIGURE_PM
70+
// if ( !configure_pm(argc, argv, pm) ) {
71+
// return 1;
72+
// }
73+
// #endif
74+
75+
pm.waitForWam(BARRETT_SMF_PROMPT_ON_ZEROING);
76+
pm.wakeAllPucks();
77+
// TODO(JH): Rehab Update implement and test
78+
if (pm.foundWam3())
79+
{
80+
return wam_main(argc, argv, pm, *pm.getWam3(BARRETT_SMF_WAIT_FOR_SHIFT_ACTIVATE, BARRETT_SMF_WAM_CONFIG_PATH));
81+
}
82+
else if (pm.foundWam4())
83+
{
84+
return wam_main(argc, argv, pm, *pm.getWam4(BARRETT_SMF_WAIT_FOR_SHIFT_ACTIVATE, BARRETT_SMF_WAM_CONFIG_PATH));
85+
}
86+
else if (pm.foundWam7())
87+
{
88+
return wam_main(argc, argv, pm, *pm.getWam7(BARRETT_SMF_WAIT_FOR_SHIFT_ACTIVATE, BARRETT_SMF_WAM_CONFIG_PATH));
89+
}
90+
else
91+
{
92+
printf(">>> ERROR: No WAM was found. Perhaps you have found a bug in ProductManager::waitForWam().\n");
93+
return 1;
94+
}
95+
}
96+
97+
#endif /* BARRETT_STANDARD_MAIN_FUNCTION_H_ */

src/bus/can_over_tcp_server.cpp

+32-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
#include <barrett/os.h>
12-
#include <barrett/bus/can_over_tcp_server.h>
12+
#include "can_over_tcp_server.h"
1313

1414
namespace barrett
1515
{
@@ -78,6 +78,7 @@ class session
7878
}
7979
else
8080
{
81+
std::cout << "handle_read_header died" << std::endl;
8182
delete this;
8283
}
8384
}
@@ -103,9 +104,18 @@ class session
103104
// TODO
104105
}
105106

107+
// printf("<<bus id:%d len:%d data:", busId, len);
108+
// for (size_t i = 0; i < len; i++)
109+
// {
110+
// printf("%X", (unsigned char)data_[i]);
111+
// }
112+
// putchar('\n');
113+
106114
// msg : ret(4)
107115
int32_t ret = cansocket_->send(busId, reinterpret_cast<const unsigned char *>(data_), len);
108116

117+
// printf(">>ret:%d\n", ret);
118+
109119
memcpy(data_, &ret, 4);
110120
boost::asio::write(socket_, boost::asio::buffer(data_, 4));
111121
}
@@ -123,11 +133,25 @@ class session
123133

124134
int blocking = readInt32(reinterpret_cast<const unsigned char *>(data_));
125135

136+
// printf("<<blocking:%d\n", blocking);
137+
126138
// msg : ret(4) + busId(4) + len(4) + data
127139
int busId;
128140
size_t len;
129141
int32_t ret = cansocket_->receiveRaw(busId, reinterpret_cast<unsigned char *>(data_ + 12), len, blocking == 1);
130142

143+
if (ret != 0)
144+
{
145+
len = 0;
146+
}
147+
148+
// printf(">>ret:%d bus id:%d len:%d data:", ret, busId, (int)len);
149+
// for (size_t i = 0; i < len; i++)
150+
// {
151+
// printf("%X", (unsigned char)data_[i + 12]);
152+
// }
153+
// putchar('\n');
154+
131155
int32_t dbusId = busId;
132156
int32_t dlen = len;
133157
memcpy(data_, &ret, 4);
@@ -152,6 +176,8 @@ CANOverTCPServer::CANOverTCPServer(boost::asio::io_service &io_service, short po
152176
{
153177
cansocket.open(canPort);
154178

179+
std::cout << "CAN Socket is " << (cansocket.isOpen() ? "opened" : "closed") << std::endl;
180+
155181
session *new_session = new session(io_service_, &cansocket);
156182
acceptor_.async_accept(new_session->socket(),
157183
boost::bind(&CANOverTCPServer::handle_accept, this, new_session,
@@ -164,13 +190,18 @@ void CANOverTCPServer::handle_accept(session *new_session,
164190
if (!error)
165191
{
166192
new_session->start();
193+
194+
std::cout << "session started" << std::endl;
195+
167196
new_session = new session(io_service_, &cansocket);
168197
acceptor_.async_accept(new_session->socket(),
169198
boost::bind(&CANOverTCPServer::handle_accept, this, new_session,
170199
boost::asio::placeholders::error));
171200
}
172201
else
173202
{
203+
std::cout << "session died" << std::endl;
204+
174205
delete new_session;
175206
}
176207
}

src/bus/can_over_tcp_socket.cpp

+29-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <barrett/os.h>
1818
#include <barrett/thread/real_time_mutex.h>
1919
#include <barrett/products/puck.h>
20-
#include <barrett/bus/can_over_tcp_socket.h>
20+
#include "can_over_tcp_socket.h"
2121

2222
namespace barrett
2323
{
@@ -28,6 +28,11 @@ CANOverTCPSocket::CANOverTCPSocket() : mutex()
2828
{
2929
}
3030

31+
CANOverTCPSocket::CANOverTCPSocket(const char *ip, const char *port) throw(std::runtime_error) : ip_(ip), port_(port)
32+
{
33+
open(0);
34+
}
35+
3136
CANOverTCPSocket::CANOverTCPSocket(int port) throw(std::runtime_error) : mutex()
3237
{
3338
open(port);
@@ -47,8 +52,10 @@ void CANOverTCPSocket::open(int port) throw(std::logic_error, std::runtime_error
4752

4853
logMessage("CANOverTCPSocket::open(%d) using CAN Over TCP driver") % port;
4954

55+
std::cout << "using CAN Over TCP driver" << std::endl;
56+
5057
tcp::resolver resolver(io_service);
51-
tcp::resolver::query query(tcp::v4(), "localhost", "12345");
58+
tcp::resolver::query query(tcp::v4(), ip_, port_);
5259
tcp::resolver::iterator iterator = resolver.resolve(query);
5360

5461
socket = new tcp::socket(io_service);
@@ -84,20 +91,32 @@ int CANOverTCPSocket::send(int busId, const unsigned char *data, size_t len) con
8491
memcpy(packetData + 12, data, len);
8592
boost::asio::write(*socket, boost::asio::buffer(packetData, len + 12));
8693

94+
// printf("<<bus id:%d len:%d data:", busId, (int)len);
95+
// for (size_t i = 0; i < len; i++)
96+
// {
97+
// printf("%X", (unsigned char)data[i]);
98+
// }
99+
// putchar('\n');
100+
87101
// msg : ret(4)
88102
const size_t reply_length = 4;
89103
char reply[reply_length];
90104
size_t actual_reply_length = boost::asio::read(*socket, boost::asio::buffer(reply, reply_length));
91105

92106
assert(actual_reply_length == reply_length);
93107

94-
return *reinterpret_cast<int32_t *>(reply);
108+
int ret = *reinterpret_cast<int32_t *>(reply);
109+
// printf(">>ret:%d\n", ret);
110+
111+
return ret;
95112
}
96113

97114
int CANOverTCPSocket::receiveRaw(int &busId, unsigned char *data, size_t &len, bool blocking) const
98115
{
99116
BARRETT_SCOPED_LOCK(mutex);
100117

118+
// printf("<<blocking:%d\n", blocking);
119+
101120
// msg : type(4) + blocking(4)
102121
int32_t type = 1;
103122
int32_t dblocking = blocking ? 1 : 0;
@@ -120,6 +139,13 @@ int CANOverTCPSocket::receiveRaw(int &busId, unsigned char *data, size_t &len, b
120139

121140
assert(actual_data_length == len);
122141

142+
// printf(">>ret:%d bus id:%d len:%d data:", ret, busId, (int)len);
143+
// for (size_t i = 0; i < len; i++)
144+
// {
145+
// printf("%X", (unsigned char)data[i]);
146+
// }
147+
// putchar('\n');
148+
123149
return ret;
124150
}
125151

0 commit comments

Comments
 (0)