forked from mnakada/atomcam_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor.c
79 lines (65 loc) · 2.12 KB
/
motor.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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
extern int local_sdk_motor_get_position(float *step,float *angle);
extern int local_sdk_motor_move_abs_angle(float pan, float tilt, int speed, void (*done)(float a, float b), void (*canceled)(void), int mode);
extern int IMP_ISP_Tuning_GetISPHflip(int *pmode);
extern int IMP_ISP_Tuning_GetISPVflip(int *pmode);
extern void CommandResponse(int fd, const char *res);
int MotorFd = 0;
struct timeval MotorLastMovedTime = { 0, 0 };
static void motor_move_done(float pan, float tilt) {
if(MotorFd) {
static char motorResBuf[256];
sprintf(motorResBuf, "%f %f\n", pan, tilt);
CommandResponse(MotorFd, motorResBuf);
}
MotorFd = 0;
struct timeval tv;
gettimeofday(&MotorLastMovedTime, NULL);
}
static void motor_move_canceled() {
if(MotorFd) CommandResponse(MotorFd, "error");
MotorFd = 0;
gettimeofday(&MotorLastMovedTime, NULL);
}
char *MotorMove(int fd, char *tokenPtr) {
int vflip, hflip;
IMP_ISP_Tuning_GetISPHflip(&hflip);
IMP_ISP_Tuning_GetISPVflip(&vflip);
char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) {
float pan; // 0-355
float tilt; // 0-180
int ret = local_sdk_motor_get_position(&pan, &tilt);
static char motorResBuf[256];
if(!ret) {
if(hflip) pan = 355.0 - pan;
if(vflip) tilt = 180.0 - tilt;
sprintf(motorResBuf, "%f %f %d %d\n", pan, tilt, hflip, vflip);
} else {
return "error";
}
return motorResBuf;
}
float pan = atof(p); // 0-355
if((pan < 0.0) || (pan > 355.0)) return "error";
p = strtok_r(NULL, " \t\r\n", &tokenPtr);
if(!p) return "error";
float tilt = atof(p); // 0-180
if((tilt < 0.0) || (tilt > 180.0)) return "error";
p = strtok_r(NULL, " \t\r\n", &tokenPtr);
int pri = 2; // 0: high - 3: low
if(p) pri = atoi(p);
if(pri < 0) pri = 0;
if(pri > 3) pri = 3;
if(MotorFd) return "error";
MotorFd = fd;
if(hflip) pan = 355.0 - pan;
if(vflip) tilt = 180.0 - tilt;
int speed = 9;
int res = local_sdk_motor_move_abs_angle(pan, tilt, speed, &motor_move_done, &motor_move_canceled, pri);
return NULL;
}