duck.c 2.12 KB
Newer Older
jp7g21's avatar
jp7g21 committed
1
2
3
4
5
6
7
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include <stdlib.h>
#include "../libduck/libduck.h"

jp7g21's avatar
jp7g21 committed
8
#define SHAKE_ANGLE 90
jp7g21's avatar
jp7g21 committed
9
10
#define SHAKE_T 300

jp7g21's avatar
jp7g21 committed
11
void shake(int motor, int number, int min, int max, int delay)
jp7g21's avatar
jp7g21 committed
12
{
jp7g21's avatar
jp7g21 committed
13
14
15
16
17
18
19
20
21
22
23
     int delay1 = (delay * (-min)) / (max - min);
     int delay2 = delay - delay1;
     
     duck_set_position(motor, min);
     duck_delay(delay1);
     
     for (int j=0; j < number-1; j++) {
	  duck_set_position(motor, max);
	  duck_delay(delay);
	  duck_set_position(motor, min);
	  duck_delay(delay);
jp7g21's avatar
jp7g21 committed
24
     }
jp7g21's avatar
jp7g21 committed
25
26
27

     duck_set_position(motor, 0);
     duck_delay(delay2);
jp7g21's avatar
jp7g21 committed
28
29
30
31
32
33
34
35
}

int main(int argc, char **argv)
{
     int option;
     const char *fname = DEFAULT_DUCK_FNAME;
     char buf[100];
     
jp7g21's avatar
jp7g21 committed
36
     while ((option=getopt(argc, argv, "t:d")) != -1) {
jp7g21's avatar
jp7g21 committed
37
	  switch (option) {
jp7g21's avatar
jp7g21 committed
38
39
40
	  case 'd':
	       duck_debug_mode = 1;
	       break;
jp7g21's avatar
jp7g21 committed
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
	  case 't':
	       strncpy(buf, optarg, 100);
	       fname = buf;
	       break;
	  default:
	       printf("Usage: duck [-t TTYNAME] command\n");
	       exit(EXIT_FAILURE);
	       break;
	  }
     }

     open_duck(fname);
     configure_duck();
     
     for (int i=optind; argv[i]; i++) {
	  if (strcmp(argv[i], "set") == 0) {
	       if (argv[i+1] && argv[i+2]) {
		    int motor = atoi(argv[i+1]);
		    int angle = atoi(argv[i+2]);
		    duck_set_position(motor, angle);
		    i += 2;
	       }
	  } else if (strcmp(argv[i], "vel") == 0) {
	       if (argv[i+1] && argv[i+2]) {
		    int motor = atoi(argv[i+1]);
		    int deg_per_sec = atoi(argv[i+2]);
		    duck_set_velocity(motor, deg_per_sec);
		    i += 2;
	       }
	  } else if (strcmp(argv[i], "headshake") == 0) {
	       if (argv[i+1]) {
		    int numshakes = atoi(argv[i+1]);
jp7g21's avatar
jp7g21 committed
73
		    shake(MOTOR_3, numshakes, -90, 90, 250);
jp7g21's avatar
jp7g21 committed
74
75
76
77
78
		    i++;
	       }
	  } else if (strcmp(argv[i], "nod") == 0) {
	       if (argv[i+1]) {
		    int numnods = atoi(argv[i+1]);
jp7g21's avatar
jp7g21 committed
79
80
81
82
83
84
85
		    shake(MOTOR_2, numnods, -90, 0, 250);
		    i++;
	       }
	  } else if (strcmp(argv[i], "facepalm") == 0) {
	       if (argv[i+1]) {
		    int numnods = atoi(argv[i+1]);
		    shake(MOTOR_2, numnods, -90, 0, 250);
jp7g21's avatar
jp7g21 committed
86
87
88
89
90
91
92
		    i++;
	       }
	  }
     }
     return 0;
}