-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.c
129 lines (111 loc) · 2.86 KB
/
main.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
/*
* Copyright (C) 2009 Miika-Petteri Matikainen, Tuomas Miettinen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include "can.h"
#include "elmo.h"
/**
* CAN device interface name.
*/
#define CAN_INTERFACE "can0"
/**
* Motor controller CANOpen node ID.
*/
#define CANOPEN_ID 127
/**
* Prints position and force readings from the motor controller.
* Note: this function will never exit.
*/
void print_info(TCan *can)
{
int f;
float a;
while (1) {
getPosition(can, &f);
getForce(can, &a);
printf("position = %d\tforce = %f\n", f, a);
usleep(500 * 1000);
}
}
/**
* Drive the motor with speed 30000 to position 4000000.
*/
void test_position(TCan *can)
{
setSpeed(can, 30000);
setPosition(can, 500000);
print_info(can);
}
/**
* Drive the motor using force feedback with 0.6 amperes.
*/
void test_force(TCan *can)
{
setForce(can, 0.6);
print_info(can);
}
/**
* This will test some motor commands. It will test moving the motor to a
* specified position with a specified speed. After that, it will also test
* driving the motor with a specified force.
*/
void test(TCan *can)
{
/**
* Set the motor speed limits.
*/
setLimits(can, -320000, 320000, -320000, 320000);
/**
* Uncomment test_position(can) or test_force(can) to test position
* control or force control.
*/
test_position(can);
//test_force(can);
stop(can);
}
/**
* A simple test program consisting of the mandatory commands to be given
* before the motor controller can accept any useful commands and the
* function calls to be given afterwards. The useful commands are put in
* the test() function. To be able to run the useful commands one has to
* remember to use first start.sh and run the useful commands in it.
*/
int main()
{
printf("CAN test begins\n");
TCan *can = TCanConstruct(CAN_INTERFACE);
if (!can) {
printf("Could not construct can\n");
return EXIT_FAILURE;
}
if (TCanOpen(can, CANOPEN_ID) < 0) {
printf("CanOpen failed\n");
return EXIT_FAILURE;
}
if (sendEchoMessage(can) < 0) {
printf("Echo failed\n");
return EXIT_FAILURE;
}
test(can);
if (TCanClose(can) < 0) {
printf("CanClose failed\n");
return EXIT_FAILURE;
}
TCanDestruct(can);
printf("CAN test end\n");
return EXIT_SUCCESS;
}