motor.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/time.h>
  6. extern int local_sdk_motor_get_position(float *step,float *angle);
  7. 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);
  8. extern void CommandResponse(int fd, const char *res);
  9. int MotorFd = 0;
  10. struct timeval MotorLastMovedTime = { 0, 0 };
  11. static void motor_move_done(float pan, float tilt) {
  12. if(MotorFd) {
  13. static char motorResBuf[256];
  14. sprintf(motorResBuf, "%f %f\n", pan, tilt);
  15. CommandResponse(MotorFd, motorResBuf);
  16. }
  17. MotorFd = 0;
  18. struct timeval tv;
  19. gettimeofday(&MotorLastMovedTime, NULL);
  20. }
  21. static void motor_move_canceled() {
  22. if(MotorFd) CommandResponse(MotorFd, "error");
  23. MotorFd = 0;
  24. gettimeofday(&MotorLastMovedTime, NULL);
  25. }
  26. char *MotorMove(int fd, char *tokenPtr) {
  27. char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  28. if(!p) {
  29. float pan; // 0-355
  30. float tilt; // 0-180
  31. int ret = local_sdk_motor_get_position(&pan, &tilt);
  32. static char motorResBuf[256];
  33. if(!ret) {
  34. sprintf(motorResBuf, "%f %f\n", pan, tilt);
  35. } else {
  36. sprintf(motorResBuf, "- -\n");
  37. }
  38. return motorResBuf;
  39. }
  40. float pan = atof(p); // 0-355
  41. if((pan < 0.0) || (pan > 355.0)) return "error";
  42. p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  43. if(!p) return "error";
  44. float tilt = atof(p); // 0-180
  45. if((tilt < 0.0) || (tilt > 180.0)) return "error";
  46. p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  47. int pri = 2; // 0: high - 3: low
  48. if(p) pri = atoi(p);
  49. if(pri < 0) pri = 0;
  50. if(pri > 3) pri = 3;
  51. if(MotorFd) return "error";
  52. MotorFd = fd;
  53. int speed = 9;
  54. int res = local_sdk_motor_move_abs_angle(pan, tilt, speed, &motor_move_done, &motor_move_canceled, pri);
  55. return NULL;
  56. }