-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmotor.cpp
195 lines (166 loc) · 5.39 KB
/
motor.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h> // clock function
#include <sys/times.h>
#include "motor.h"
#define DEFAULT_PORT "3490"
#define MAX_DUTY_CYCLE_VALUE 4096
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define SD_SEND SHUT_WR
#define SERVERNAME "192.168.10.106"
//-----------------------------------------------------------------------------
Motor::Motor(void)
//-----------------------------------------------------------------------------
{
ConnectSocket = INVALID_SOCKET;
setWantedFreq(16.00); // us = 16 Hz
}
//-----------------------------------------------------------------------------
void Motor::init(void)
//-----------------------------------------------------------------------------
{
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;
//ZeroMemory( &hints, sizeof(hints) );
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(SERVERNAME, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", errno);
exit(1);
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %d\n", errno);
exit(1);
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == -1) {
close(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
exit(1);
}
}
//-----------------------------------------------------------------------------
void Motor::setDutyCycle(double newDutyCycle)
//-----------------------------------------------------------------------------
{
const int DEFAULT_BUFLEN = 16;
char sendbuf[DEFAULT_BUFLEN];
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
unsigned int dutyCycleValue;
dutyCycle = newDutyCycle;
dutyCycleValue = (unsigned int)(dutyCycle/100.*MAX_DUTY_CYCLE_VALUE);
if (dutyCycleValue >= MAX_DUTY_CYCLE_VALUE) dutyCycleValue = MAX_DUTY_CYCLE_VALUE - 1;
sprintf(sendbuf, "%u", dutyCycleValue);
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", errno);
close(ConnectSocket);
exit(1);
}
// read response
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult == 1 ) {
int errorCode = recvbuf[0] - '0';
switch (errorCode) {
case 0:
break;
case 1:
printf("Error code 1: No valid integer\n");
exit(1);
case 2:
printf("Error code 2: PWM value out of range\n");
exit(1);
default:
printf("Error code %d: Unknown error code\n", errorCode);
exit(1);
}
}
else if (iResult > 1) {
printf("Received message has more than one byte (%d bytes)!\n", iResult);
exit(1);
}
else if ( iResult == 0 ) {
printf("Connection closed\n");
exit(1);
}
else {
printf("recv failed with error: %d\n", errno);
exit(1);
}
}
//-----------------------------------------------------------------------------
void Motor::control(unsigned int period)
//-----------------------------------------------------------------------------
{
static double step, newDutyCycle;
static clock_t tLast=0;
clock_t t;
int delta = period - wantedPeriod;
double deltaTimeSec;
struct tms buf;
// ensure a constant sampling frequency of 2 Hz
// the clock() function does not work in CYGWIN!
t = times(&buf);
deltaTimeSec = (float)(t - tLast)/ CLK_TCK;
//printf("\n DeltaTime = %5.2f\n", deltaTimeSec);
if(deltaTimeSec < 0.5) return;
tLast = t;
step = abs(delta) < 6000 ? 0.2 : 1.00;
//step = (double) abs(delta) / 12000.;
//if (step > 0.2) step = 0.2;
if (period < wantedPeriod)
newDutyCycle = dutyCycle > step ? dutyCycle-step : 0.;
else newDutyCycle = dutyCycle < 99.-step ? dutyCycle+step : 99.;
setDutyCycle(newDutyCycle);
}
// Destructor
//-----------------------------------------------------------------------------
Motor::~Motor(void)
//-----------------------------------------------------------------------------
{
if (ConnectSocket != INVALID_SOCKET) close(ConnectSocket);
}
#if 0
// tiny test program
int main(void)
{
Motor pwm;
unsigned int i;
for (i=100; i<4096; i++) {
pwm.setDutyCycle(i);
//usleep(200);
}
return 0;
}
#endif