diff options
author | 2025-01-04 14:52:55 +0800 | |
---|---|---|
committer | 2025-01-04 14:52:55 +0800 | |
commit | ab091cb4661aed9b9da997e58a8b5ec1f5853951 (patch) | |
tree | 0abdf7f99b5fc069536d975b39e1bcb8cf8e77b9 | |
parent | 6c06f515f18f5913a027ba2c84092c4861e8cf45 (diff) | |
download | WheelCtrl-ab091cb4661aed9b9da997e58a8b5ec1f5853951.tar.gz WheelCtrl-ab091cb4661aed9b9da997e58a8b5ec1f5853951.zip |
In the old commit, CMakeLists is set to find liblcm in
${CMAKE_SOURCE_ROOT}/libs, and there are two dirs: aarch64 and x86.
When I compile it for x86-64 computer using the cmake on host machine,
it will automatically find lib under ${CMAKE_SOURCE_ROOT}/lib/x86, and
when compiling for arm using cmake/compiler provided by ohos, it will
also find under ${CMAKE_SOURCE_ROOT}/lib/aarch64. Sounds good.
But, this is just a feature of special version for cmake/make/binutils,
causing error on other linux distributions. The path should be given,
but tools wouldn't find libs there recursively, which means path should
be spercific.
So in this commit, I use runme.sh to compile. It will work depending on
the argument(named arch) given by user, and pass it as a macro named
OHOS_ARCH to cmake, which is used in linking path of CMakeLists.
Besides, libpthread.so is not always default linked. So let's write it
in CMakeLists.
Last but not least, keeping source code clean is our duty. There's a
.clang-format under ${CMAKE_SOURCE_ROOT} to help. Use it.
Moving for the better!
-rw-r--r-- | .clang-format | 121 | ||||
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rwxr-xr-x[-rw-r--r--] | lib/arm64-v8a/liblcm.so (renamed from lib/aarch64/liblcm.so) | bin | 95736 -> 95736 bytes | |||
-rwxr-xr-x | lib/x86_64/liblcm.so (renamed from lib/x86/liblcm.so) | bin | 144320 -> 144320 bytes | |||
-rwxr-xr-x | runme.sh | 34 | ||||
-rw-r--r-- | serial/CMakeLists.txt | 2 | ||||
-rw-r--r-- | serial/serial.c | 31 | ||||
-rw-r--r-- | serial/serial.h | 6 | ||||
-rw-r--r-- | serial/wheel.c | 7 | ||||
-rw-r--r-- | udp2lcm/CMakeLists.txt | 2 | ||||
-rw-r--r-- | udp2lcm/udp.c | 30 | ||||
-rw-r--r-- | udp2lcm/udp2lcm.c | 48 | ||||
-rw-r--r-- | udp2lcm/udp2lcm.h | 6 |
14 files changed, 264 insertions, 30 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a1054a5 --- /dev/null +++ b/.clang-format | |||
@@ -0,0 +1,121 @@ | |||
1 | --- | ||
2 | Language: Cpp | ||
3 | # BasedOnStyle: LLVM | ||
4 | AccessModifierOffset: -2 | ||
5 | AlignAfterOpenBracket: Align | ||
6 | AlignConsecutiveAssignments: false | ||
7 | AlignConsecutiveDeclarations: false | ||
8 | AlignEscapedNewlines: Right | ||
9 | AlignOperands: true | ||
10 | AlignTrailingComments: true | ||
11 | AllowAllParametersOfDeclarationOnNextLine: true | ||
12 | AllowShortBlocksOnASingleLine: false | ||
13 | AllowShortCaseLabelsOnASingleLine: false | ||
14 | AllowShortFunctionsOnASingleLine: All | ||
15 | AllowShortIfStatementsOnASingleLine: false | ||
16 | AllowShortLoopsOnASingleLine: false | ||
17 | AlwaysBreakAfterDefinitionReturnType: None | ||
18 | AlwaysBreakAfterReturnType: None | ||
19 | AlwaysBreakBeforeMultilineStrings: false | ||
20 | AlwaysBreakTemplateDeclarations: MultiLine | ||
21 | BinPackArguments: true | ||
22 | BinPackParameters: true | ||
23 | BraceWrapping: | ||
24 | AfterClass: false | ||
25 | AfterControlStatement: false | ||
26 | AfterEnum: false | ||
27 | AfterFunction: false | ||
28 | AfterNamespace: false | ||
29 | AfterObjCDeclaration: false | ||
30 | AfterStruct: false | ||
31 | AfterUnion: false | ||
32 | AfterExternBlock: false | ||
33 | BeforeCatch: false | ||
34 | BeforeElse: false | ||
35 | IndentBraces: false | ||
36 | SplitEmptyFunction: true | ||
37 | SplitEmptyRecord: true | ||
38 | SplitEmptyNamespace: true | ||
39 | BreakBeforeBinaryOperators: None | ||
40 | BreakBeforeBraces: Attach | ||
41 | BreakBeforeInheritanceComma: false | ||
42 | BreakInheritanceList: BeforeColon | ||
43 | BreakBeforeTernaryOperators: true | ||
44 | BreakConstructorInitializersBeforeComma: false | ||
45 | BreakConstructorInitializers: BeforeColon | ||
46 | BreakAfterJavaFieldAnnotations: false | ||
47 | BreakStringLiterals: true | ||
48 | ColumnLimit: 80 | ||
49 | CommentPragmas: '^ IWYU pragma:' | ||
50 | CompactNamespaces: false | ||
51 | ConstructorInitializerAllOnOneLineOrOnePerLine: false | ||
52 | ConstructorInitializerIndentWidth: 4 | ||
53 | ContinuationIndentWidth: 4 | ||
54 | Cpp11BracedListStyle: true | ||
55 | DerivePointerAlignment: false | ||
56 | DisableFormat: false | ||
57 | ExperimentalAutoDetectBinPacking: false | ||
58 | FixNamespaceComments: true | ||
59 | ForEachMacros: | ||
60 | - foreach | ||
61 | - Q_FOREACH | ||
62 | - BOOST_FOREACH | ||
63 | IncludeBlocks: Preserve | ||
64 | IncludeCategories: | ||
65 | - Regex: '^"(llvm|llvm-c|clang|clang-c)/' | ||
66 | Priority: 2 | ||
67 | - Regex: '^(<|"(gtest|gmock|isl|json)/)' | ||
68 | Priority: 3 | ||
69 | - Regex: '.*' | ||
70 | Priority: 1 | ||
71 | IncludeIsMainRegex: '(Test)?$' | ||
72 | IndentCaseLabels: false | ||
73 | IndentPPDirectives: None | ||
74 | IndentWidth: 4 | ||
75 | IndentWrappedFunctionNames: false | ||
76 | JavaScriptQuotes: Leave | ||
77 | JavaScriptWrapImports: true | ||
78 | KeepEmptyLinesAtTheStartOfBlocks: true | ||
79 | MacroBlockBegin: '' | ||
80 | MacroBlockEnd: '' | ||
81 | MaxEmptyLinesToKeep: 1 | ||
82 | NamespaceIndentation: None | ||
83 | ObjCBinPackProtocolList: Auto | ||
84 | ObjCBlockIndentWidth: 2 | ||
85 | ObjCSpaceAfterProperty: false | ||
86 | ObjCSpaceBeforeProtocolList: true | ||
87 | PenaltyBreakAssignment: 2 | ||
88 | PenaltyBreakBeforeFirstCallParameter: 19 | ||
89 | PenaltyBreakComment: 300 | ||
90 | PenaltyBreakFirstLessLess: 120 | ||
91 | PenaltyBreakString: 1000 | ||
92 | PenaltyBreakTemplateDeclaration: 10 | ||
93 | PenaltyExcessCharacter: 1000000 | ||
94 | PenaltyReturnTypeOnItsOwnLine: 60 | ||
95 | PointerAlignment: Right | ||
96 | ReflowComments: true | ||
97 | SortIncludes: true | ||
98 | SortUsingDeclarations: true | ||
99 | SpaceAfterCStyleCast: false | ||
100 | SpaceAfterTemplateKeyword: true | ||
101 | SpaceBeforeAssignmentOperators: true | ||
102 | SpaceBeforeCpp11BracedList: false | ||
103 | SpaceBeforeCtorInitializerColon: true | ||
104 | SpaceBeforeInheritanceColon: true | ||
105 | SpaceBeforeParens: ControlStatements | ||
106 | SpaceBeforeRangeBasedForLoopColon: true | ||
107 | SpaceInEmptyParentheses: false | ||
108 | SpacesBeforeTrailingComments: 1 | ||
109 | SpacesInAngles: false | ||
110 | SpacesInContainerLiterals: true | ||
111 | SpacesInCStyleCastParentheses: false | ||
112 | SpacesInParentheses: false | ||
113 | SpacesInSquareBrackets: false | ||
114 | Standard: Cpp11 | ||
115 | StatementMacros: | ||
116 | - Q_UNUSED | ||
117 | - QT_REQUIRE_VERSION | ||
118 | TabWidth: 8 | ||
119 | UseTab: Never | ||
120 | ... | ||
121 | |||
@@ -1,3 +1,4 @@ | |||
1 | build/* | 1 | build/* |
2 | bin/* | 2 | bin/* |
3 | .vscode/* \ No newline at end of file | 3 | .vscode/* |
4 | compile_commands.json | ||
diff --git a/CMakeLists.txt b/CMakeLists.txt index c487036..c01ccfc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -9,10 +9,10 @@ include_directories( | |||
9 | ) | 9 | ) |
10 | # 添加库文件路径 | 10 | # 添加库文件路径 |
11 | link_directories( | 11 | link_directories( |
12 | ${CMAKE_SOURCE_DIR}/lib | 12 | ${CMAKE_SOURCE_DIR}/lib/${OHOS_ARCH} |
13 | ) | 13 | ) |
14 | # 设置输出路径 | 14 | # 设置输出路径 |
15 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) | 15 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) |
16 | 16 | ||
17 | add_subdirectory(serial) | 17 | add_subdirectory(serial) |
18 | add_subdirectory(udp2lcm) \ No newline at end of file | 18 | add_subdirectory(udp2lcm) |
diff --git a/lib/aarch64/liblcm.so b/lib/arm64-v8a/liblcm.so index 98bb954..98bb954 100644..100755 --- a/lib/aarch64/liblcm.so +++ b/lib/arm64-v8a/liblcm.so | |||
Binary files differ | |||
diff --git a/lib/x86/liblcm.so b/lib/x86_64/liblcm.so index cb18db8..cb18db8 100755 --- a/lib/x86/liblcm.so +++ b/lib/x86_64/liblcm.so | |||
Binary files differ | |||
diff --git a/runme.sh b/runme.sh new file mode 100755 index 0000000..947d50a --- /dev/null +++ b/runme.sh | |||
@@ -0,0 +1,34 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | ########################################################################## | ||
4 | # File Name : compile.sh | ||
5 | # Encoding : utf-8 | ||
6 | # Author : We-unite | ||
7 | # Email : weunite1848@gmail.com | ||
8 | # Created Time : 2024-02-29 15:19:15 | ||
9 | ########################################################################## | ||
10 | |||
11 | set -e | ||
12 | |||
13 | if [ $UID -eq 0 ]; then | ||
14 | echo "Please do not run this script as root" | ||
15 | exit 1 | ||
16 | fi | ||
17 | |||
18 | if [ $# -ne 1 ]; then | ||
19 | echo "ERROR: $0 <arm64-v8a|x86_64>" | ||
20 | exit 1 | ||
21 | fi | ||
22 | |||
23 | arch=$1 | ||
24 | link=static # or shared | ||
25 | native_path=/home/player/app/native | ||
26 | |||
27 | if [ $arch == "arm64-v8a" ]; then | ||
28 | export PATH=$native_path/build-tools/cmake/bin:$PATH | ||
29 | cmake -B build -D OHOS_STL=c++_$link -D OHOS_ARCH=$arch -D OHOS_PLATFORM=OHOS -D CMAKE_TOOLCHAIN_FILE=$(find $native_path -name ohos.toolchain.cmake) | ||
30 | else | ||
31 | cmake -B build -D OHOS_ARCH=$arch | ||
32 | fi | ||
33 | |||
34 | cmake --build build | ||
diff --git a/serial/CMakeLists.txt b/serial/CMakeLists.txt index 5ded98c..c0bc05a 100644 --- a/serial/CMakeLists.txt +++ b/serial/CMakeLists.txt | |||
@@ -8,4 +8,4 @@ aux_source_directory(${CMAKE_SOURCE_DIR}/lcmtype DIR_SRCS) | |||
8 | add_executable(serial ${DIR_SRCS}) | 8 | add_executable(serial ${DIR_SRCS}) |
9 | 9 | ||
10 | # 添加链接库 | 10 | # 添加链接库 |
11 | target_link_libraries(serial lcm) \ No newline at end of file | 11 | target_link_libraries(serial lcm pthread) |
diff --git a/serial/serial.c b/serial/serial.c index a2f253a..6163703 100644 --- a/serial/serial.c +++ b/serial/serial.c | |||
@@ -1,7 +1,12 @@ | |||
1 | #include "serial.h" | 1 | #include "serial.h" |
2 | #include <math.h> | ||
2 | 3 | ||
3 | lcm_t *lcm; | 4 | lcm_t *lcm; |
4 | 5 | ||
6 | // TODO 屏蔽停止指令 | ||
7 | bool stopFlag = false; | ||
8 | //////////////////////// | ||
9 | |||
5 | int curStatus = -1, curSpeed = 0; // 这里的速度指百分比速度 | 10 | int curStatus = -1, curSpeed = 0; // 这里的速度指百分比速度 |
6 | double curOmega = 0.0; | 11 | double curOmega = 0.0; |
7 | // 临界区数据 | 12 | // 临界区数据 |
@@ -72,6 +77,23 @@ void parseCmd(const lcm_recv_buf_t *rbuf, const char *channel, | |||
72 | curOmega = (double)-DEFAULT_SPEED * FULLSPEED / 100 / RADIUS; | 77 | curOmega = (double)-DEFAULT_SPEED * FULLSPEED / 100 / RADIUS; |
73 | curSpeed = 0; | 78 | curSpeed = 0; |
74 | break; | 79 | break; |
80 | // TODO 屏蔽停止指令 | ||
81 | case 4: | ||
82 | wheelSend(0x00, 0x00, 0x00, 0x00); | ||
83 | curOmega = 0; | ||
84 | curSpeed = 0; | ||
85 | stopFlag = true; | ||
86 | printf("Stop for 2 seconds\n"); | ||
87 | break; | ||
88 | // 取消屏蔽 | ||
89 | case 5: | ||
90 | wheelSend(0x00, 0x00, 0x00, 0x00); | ||
91 | curOmega = 0; | ||
92 | curSpeed = 0; | ||
93 | stopFlag = false; | ||
94 | printf("Stop but can recieve cmd\n"); | ||
95 | break; | ||
96 | //////////////////////////////////// | ||
75 | case 0: | 97 | case 0: |
76 | default: | 98 | default: |
77 | wheelSend(0x00, 0x00, 0x00, 0x00); | 99 | wheelSend(0x00, 0x00, 0x00, 0x00); |
@@ -85,6 +107,11 @@ void parseCmd(const lcm_recv_buf_t *rbuf, const char *channel, | |||
85 | 107 | ||
86 | void parsePath(const lcm_recv_buf_t *rbuf, const char *channel, | 108 | void parsePath(const lcm_recv_buf_t *rbuf, const char *channel, |
87 | const path_t *msg, void *userdata) { | 109 | const path_t *msg, void *userdata) { |
110 | // TODO | ||
111 | if (stopFlag) { | ||
112 | return; | ||
113 | } | ||
114 | ///////////////// | ||
88 | int16_t length = msg->length; | 115 | int16_t length = msg->length; |
89 | double v, w; | 116 | double v, w; |
90 | int8_t vWheels[2]; | 117 | int8_t vWheels[2]; |
@@ -127,7 +154,7 @@ void setCurPose(const lcm_recv_buf_t *rbuf, const char *channel, | |||
127 | pthread_mutex_unlock(&curPoseMutex); | 154 | pthread_mutex_unlock(&curPoseMutex); |
128 | } | 155 | } |
129 | 156 | ||
130 | void sendCurPose() { | 157 | void *sendCurPose(void *args) { |
131 | pose_t pose; | 158 | pose_t pose; |
132 | while (true) { | 159 | while (true) { |
133 | renewCurPose(); | 160 | renewCurPose(); |
@@ -152,4 +179,4 @@ void renewCurPose() { | |||
152 | curPose[2] += curOmega * dt; | 179 | curPose[2] += curOmega * dt; |
153 | lastTime = clock(); | 180 | lastTime = clock(); |
154 | pthread_mutex_unlock(&curPoseMutex); | 181 | pthread_mutex_unlock(&curPoseMutex); |
155 | } \ No newline at end of file | 182 | } |
diff --git a/serial/serial.h b/serial/serial.h index 3563557..2fbf870 100644 --- a/serial/serial.h +++ b/serial/serial.h | |||
@@ -19,7 +19,7 @@ | |||
19 | // TODO: 两个待测数据 | 19 | // TODO: 两个待测数据 |
20 | #define RADIUS 0.13 // 两轮之间的距离的一半,米 | 20 | #define RADIUS 0.13 // 两轮之间的距离的一半,米 |
21 | #define FULLSPEED 0.60 // 轮子全速,m/s | 21 | #define FULLSPEED 0.60 // 轮子全速,m/s |
22 | #define fabs(x) ((x) > 0 ? (x) : -(x)) | 22 | // #define fabs(x) ((x) > 0 ? (x) : -(x)) |
23 | typedef unsigned char byte; | 23 | typedef unsigned char byte; |
24 | 24 | ||
25 | int identify_device(const char *port); | 25 | int identify_device(const char *port); |
@@ -34,6 +34,6 @@ void parsePath(const lcm_recv_buf_t *rbuf, const char *channel, | |||
34 | void setCurPose(const lcm_recv_buf_t *rbuf, const char *channel, | 34 | void setCurPose(const lcm_recv_buf_t *rbuf, const char *channel, |
35 | const pose_t *msg, void *userdata); | 35 | const pose_t *msg, void *userdata); |
36 | void renewCurPose(); | 36 | void renewCurPose(); |
37 | void sendCurPose(); | 37 | void *sendCurPose(void *args); |
38 | 38 | ||
39 | #endif \ No newline at end of file | 39 | #endif |
diff --git a/serial/wheel.c b/serial/wheel.c index 8155734..c470234 100644 --- a/serial/wheel.c +++ b/serial/wheel.c | |||
@@ -5,7 +5,8 @@ char *portname; // 串口设备名 | |||
5 | struct termios tty; | 5 | struct termios tty; |
6 | 6 | ||
7 | bool whellInit() { | 7 | bool whellInit() { |
8 | portname = findUSBDev("wheel"); | 8 | // portname = findUSBDev("wheel"); |
9 | portname = "/dev/ttyUSB1"; // "/dev/ttyUSB1 | ||
9 | if (portname == NULL) { | 10 | if (portname == NULL) { |
10 | fprintf(stderr, "Error: Failed to find wheel serial port\n"); | 11 | fprintf(stderr, "Error: Failed to find wheel serial port\n"); |
11 | return false; | 12 | return false; |
@@ -55,6 +56,10 @@ bool wheelSend(byte a, byte a_v, byte b, byte b_v) { | |||
55 | unsigned char data[7] = {0x53, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00}; | 56 | unsigned char data[7] = {0x53, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00}; |
56 | byte checksum = 0; | 57 | byte checksum = 0; |
57 | data[2] = a; | 58 | data[2] = a; |
59 | // TODO: 速度转换 | ||
60 | int temp = ((int)a_v + 2); | ||
61 | a_v = (byte)temp; | ||
62 | //////////////////////// | ||
58 | data[3] = a_v; | 63 | data[3] = a_v; |
59 | data[4] = b; | 64 | data[4] = b; |
60 | data[5] = b_v; | 65 | data[5] = b_v; |
diff --git a/udp2lcm/CMakeLists.txt b/udp2lcm/CMakeLists.txt index aaf54db..206a8a8 100644 --- a/udp2lcm/CMakeLists.txt +++ b/udp2lcm/CMakeLists.txt | |||
@@ -7,4 +7,4 @@ aux_source_directory(${CMAKE_SOURCE_DIR}/lcmtype DIR_SRCS) | |||
7 | 7 | ||
8 | # 生成可执行文件 | 8 | # 生成可执行文件 |
9 | add_executable(udp2lcm ${DIR_SRCS}) | 9 | add_executable(udp2lcm ${DIR_SRCS}) |
10 | target_link_libraries(udp2lcm lcm) \ No newline at end of file | 10 | target_link_libraries(udp2lcm lcm pthread) |
diff --git a/udp2lcm/udp.c b/udp2lcm/udp.c index 6c4bd4a..97fb53e 100644 --- a/udp2lcm/udp.c +++ b/udp2lcm/udp.c | |||
@@ -1,6 +1,6 @@ | |||
1 | #include "udp2lcm.h" | 1 | #include "udp2lcm.h" |
2 | 2 | ||
3 | void udpRecvHandler() { | 3 | void *udpRecvHandler(void *args) { |
4 | int socketfd = -1; | 4 | int socketfd = -1; |
5 | char buffer[MAX_BUFFER_SIZE]; | 5 | char buffer[MAX_BUFFER_SIZE]; |
6 | struct sockaddr_in serverAddr; | 6 | struct sockaddr_in serverAddr; |
@@ -13,7 +13,7 @@ void udpRecvHandler() { | |||
13 | printf("udpRecvHandler\n"); | 13 | printf("udpRecvHandler\n"); |
14 | if ((socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { | 14 | if ((socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { |
15 | fprintf(stderr, "Error: Failed to create socket\n"); | 15 | fprintf(stderr, "Error: Failed to create socket\n"); |
16 | return; | 16 | return NULL; |
17 | } | 17 | } |
18 | memset((char *)&serverAddr, 0, sizeof(serverAddr)); | 18 | memset((char *)&serverAddr, 0, sizeof(serverAddr)); |
19 | memset(buffer, 0, sizeof(buffer)); | 19 | memset(buffer, 0, sizeof(buffer)); |
@@ -25,7 +25,7 @@ void udpRecvHandler() { | |||
25 | -1) { | 25 | -1) { |
26 | fprintf(stderr, "Error: Bind failed\n"); | 26 | fprintf(stderr, "Error: Bind failed\n"); |
27 | close(socketfd); | 27 | close(socketfd); |
28 | return; | 28 | return NULL; |
29 | } | 29 | } |
30 | 30 | ||
31 | bytesReceived = recvfrom(socketfd, buffer, MAX_BUFFER_SIZE, 0, | 31 | bytesReceived = recvfrom(socketfd, buffer, MAX_BUFFER_SIZE, 0, |
@@ -33,7 +33,7 @@ void udpRecvHandler() { | |||
33 | if (bytesReceived == -1) { | 33 | if (bytesReceived == -1) { |
34 | fprintf(stderr, "Error: Failed to receive data\n"); | 34 | fprintf(stderr, "Error: Failed to receive data\n"); |
35 | close(socketfd); | 35 | close(socketfd); |
36 | return; | 36 | return NULL; |
37 | } | 37 | } |
38 | printf("Start message from %s: %s\n", inet_ntoa(clientAddr.sin_addr), | 38 | printf("Start message from %s: %s\n", inet_ntoa(clientAddr.sin_addr), |
39 | buffer); | 39 | buffer); |
@@ -57,13 +57,13 @@ void udpRecvHandler() { | |||
57 | robotCtrlData.iparams[0] = 1; | 57 | robotCtrlData.iparams[0] = 1; |
58 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | 58 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); |
59 | freeRobotCtrl(&robotCtrlData); | 59 | freeRobotCtrl(&robotCtrlData); |
60 | // 随即下达30号命令,开始建图 | 60 | // // 随即下达30号命令,开始建图 |
61 | robotCtrlInit(&robotCtrlData, 0, 30, 0, 1, 1, 0, 0); | 61 | // robotCtrlInit(&robotCtrlData, 0, 30, 0, 1, 1, 0, 0); |
62 | robotCtrlData.dparams[0] = 0.05; | 62 | // robotCtrlData.dparams[0] = 0.05; |
63 | robotCtrlData.niparams = 1; | 63 | // robotCtrlData.niparams = 1; |
64 | robotCtrlData.iparams[0] = 1; | 64 | // robotCtrlData.iparams[0] = 1; |
65 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | 65 | // robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); |
66 | freeRobotCtrl(&robotCtrlData); | 66 | // freeRobotCtrl(&robotCtrlData); |
67 | 67 | ||
68 | /* | 68 | /* |
69 | * 开始接收来自手机端的命令 | 69 | * 开始接收来自手机端的命令 |
@@ -99,13 +99,13 @@ void udpRecvHandler() { | |||
99 | } | 99 | } |
100 | } | 100 | } |
101 | 101 | ||
102 | void udpSendHandler() { | 102 | void *udpSendHandler(void *args) { |
103 | // 创建UDP套接字 | 103 | // 创建UDP套接字 |
104 | int sockfd = -1; | 104 | int sockfd = -1; |
105 | struct sockaddr_in serverAddr; | 105 | struct sockaddr_in serverAddr; |
106 | if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { | 106 | if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { |
107 | fprintf(stderr, "Error: Failed to create socket\n"); | 107 | fprintf(stderr, "Error: Failed to create socket\n"); |
108 | return; | 108 | return NULL; |
109 | } | 109 | } |
110 | 110 | ||
111 | memset((char *)&serverAddr, 0, sizeof(serverAddr)); | 111 | memset((char *)&serverAddr, 0, sizeof(serverAddr)); |
@@ -114,7 +114,7 @@ void udpSendHandler() { | |||
114 | if (inet_aton(clientIP, &serverAddr.sin_addr) == 0) { | 114 | if (inet_aton(clientIP, &serverAddr.sin_addr) == 0) { |
115 | fprintf(stderr, "Error: Failed to convert IP address\n"); | 115 | fprintf(stderr, "Error: Failed to convert IP address\n"); |
116 | close(sockfd); | 116 | close(sockfd); |
117 | return; | 117 | return NULL; |
118 | } | 118 | } |
119 | 119 | ||
120 | while (true) { | 120 | while (true) { |
@@ -124,4 +124,4 @@ void udpSendHandler() { | |||
124 | pthread_mutex_unlock(&heartBeatMutex); | 124 | pthread_mutex_unlock(&heartBeatMutex); |
125 | sleep(1); | 125 | sleep(1); |
126 | } | 126 | } |
127 | } \ No newline at end of file | 127 | } |
diff --git a/udp2lcm/udp2lcm.c b/udp2lcm/udp2lcm.c index 36100da..5514b24 100644 --- a/udp2lcm/udp2lcm.c +++ b/udp2lcm/udp2lcm.c | |||
@@ -71,7 +71,13 @@ void parseCmd(const char *buffer, int bytesReceived) { | |||
71 | if (buffer[0] == 0) { | 71 | if (buffer[0] == 0) { |
72 | // 手机端用来与udp2lcm服务器建立连接的初始化消息 | 72 | // 手机端用来与udp2lcm服务器建立连接的初始化消息 |
73 | // 也是手机端的心跳 | 73 | // 也是手机端的心跳 |
74 | ; | 74 | // 下达30号命令,开始建图 |
75 | robotCtrlInit(&robotCtrlData, 0, 30, 0, 1, 1, 0, 0); | ||
76 | robotCtrlData.dparams[0] = 0.05; | ||
77 | robotCtrlData.niparams = 1; | ||
78 | robotCtrlData.iparams[0] = 1; | ||
79 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | ||
80 | freeRobotCtrl(&robotCtrlData); | ||
75 | } else if (buffer[0] == 1) { | 81 | } else if (buffer[0] == 1) { |
76 | // 建图过程中,手机端遥控小车进行移动,命令下发至轮控模块 | 82 | // 建图过程中,手机端遥控小车进行移动,命令下发至轮控模块 |
77 | path.cmd = buffer[1]; | 83 | path.cmd = buffer[1]; |
@@ -125,7 +131,47 @@ void parseCmd(const char *buffer, int bytesReceived) { | |||
125 | } else if (buffer[0] == 4) { | 131 | } else if (buffer[0] == 4) { |
126 | // 取消导航,删除上一个目标点 | 132 | // 取消导航,删除上一个目标点 |
127 | robotCtrlInit(&robotCtrlData, 0, 23, 0, 0, 0, 0, 0); | 133 | robotCtrlInit(&robotCtrlData, 0, 23, 0, 0, 0, 0, 0); |
134 | // TODO 第一次停止指令 | ||
135 | printf("Cancel Navigation!\n"); | ||
136 | path.cmd = 4; | ||
137 | path_ctrl_t_publish(lcm, "wheel_ctrl", &path); | ||
138 | ///////////////////////////// | ||
139 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | ||
140 | // TODO 再次下达停止指令 | ||
141 | sleep(2); | ||
142 | path.cmd = 5; | ||
143 | printf("Stop but can recieve cmd\n"); | ||
144 | path_ctrl_t_publish(lcm, "wheel_ctrl", &path); | ||
145 | ///////////////////////// | ||
146 | } else if (buffer[0] == 5) { | ||
147 | /* | ||
148 | 加载地图,需要注意的是所加载的地图是由电脑控制端下发的 | ||
149 | 该部分由建图部分修改 | ||
150 | */ | ||
151 | path.cmd = 0; | ||
152 | path.speed = 0; | ||
153 | path_ctrl_t_publish(lcm, "wheel_ctrl", &path); | ||
154 | |||
155 | printf("Load Map!\n"); | ||
156 | // robotCtrlInit(&robotCtrlData, 0, 32, 0, 0, 0, 0, 0); | ||
157 | // robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | ||
158 | // freeRobotCtrl(&robotCtrlData); | ||
159 | |||
160 | // sleep(2); | ||
161 | |||
162 | robotCtrlInit(&robotCtrlData, 0, 10, 0, 7, 0, 0, 0); | ||
163 | pthread_mutex_lock(&heartBeatMutex); | ||
164 | int16_t x, y, sita; | ||
165 | x = swapEndian(*(int16_t *)&heartBeat[3]); | ||
166 | y = swapEndian(*(int16_t *)&heartBeat[5]); | ||
167 | sita = swapEndian(*(int16_t *)&heartBeat[7]); | ||
168 | printf("NOW point: x: %d, y: %d, sita: %d\n", x, y, sita); | ||
169 | robotCtrlData.dparams[4] = 0; | ||
170 | robotCtrlData.dparams[5] = 0; | ||
171 | robotCtrlData.dparams[6] = 0; | ||
172 | pthread_mutex_unlock(&heartBeatMutex); | ||
128 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | 173 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); |
174 | freeRobotCtrl(&robotCtrlData); | ||
129 | } else { | 175 | } else { |
130 | fprintf(stderr, "Error: Invalid command: "); | 176 | fprintf(stderr, "Error: Invalid command: "); |
131 | for (int i = 0; i < 9; i++) { | 177 | for (int i = 0; i < 9; i++) { |
diff --git a/udp2lcm/udp2lcm.h b/udp2lcm/udp2lcm.h index 1ab7cf3..e78bc73 100644 --- a/udp2lcm/udp2lcm.h +++ b/udp2lcm/udp2lcm.h | |||
@@ -33,8 +33,8 @@ extern char clientIP[20]; | |||
33 | extern lcm_t *lcm; | 33 | extern lcm_t *lcm; |
34 | 34 | ||
35 | bool LCMInit(); | 35 | bool LCMInit(); |
36 | void udpSendHandler(); | 36 | void *udpSendHandler(void *args); |
37 | void udpRecvHandler(); | 37 | void *udpRecvHandler(void *args); |
38 | void parseCmd(const char *buffer, int bytesReceived); | 38 | void parseCmd(const char *buffer, int bytesReceived); |
39 | void robotCtrlInit(robot_control_t *robotCtrlData, int64_t utime, | 39 | void robotCtrlInit(robot_control_t *robotCtrlData, int64_t utime, |
40 | int8_t commandid, int8_t robotid, int8_t ndparams, | 40 | int8_t commandid, int8_t robotid, int8_t ndparams, |
@@ -44,4 +44,4 @@ void poseHandler(const lcm_recv_buf_t *rbuf, const char *channel, | |||
44 | const pose_t *msg, void *userdata); | 44 | const pose_t *msg, void *userdata); |
45 | int16_t swapEndian(int16_t val); | 45 | int16_t swapEndian(int16_t val); |
46 | 46 | ||
47 | #endif \ No newline at end of file | 47 | #endif |