summaryrefslogtreecommitdiffstats
path: root/kernel/code/thing.c
blob: d9b5eac00cb511190c257c09ad0522aee0381394 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <linux/cp210x.h>

/*
 * bit 0: 1	left  leg NPN base
 * bit 1: 2	left  leg PNP base
 * bit 2: 4	right leg PNP base
 * bit 3: 8	right leg NPN base
 */

#define LEFT_NPN_OFF	0
#define LEFT_NPN_ON	1
#define LEFT_PNP_OFF	2
#define LEFT_PNP_ON	0

#define LEFT_LOW	(LEFT_NPN_ON | LEFT_PNP_OFF)
#define LEFT_HI		(LEFT_NPN_OFF | LEFT_PNP_ON)
#define LEFT_OFF	(LEFT_NPN_OFF | LEFT_PNP_OFF)

#define RIGHT_NPN_OFF	0
#define RIGHT_NPN_ON	8
#define RIGHT_PNP_OFF	4
#define RIGHT_PNP_ON	0


#define RIGHT_LOW	(RIGHT_NPN_ON | RIGHT_PNP_OFF)
#define RIGHT_HI	(RIGHT_NPN_OFF | RIGHT_PNP_ON)
#define RIGHT_OFF	(RIGHT_NPN_OFF | RIGHT_PNP_OFF)

#define FORWARDS	(RIGHT_LOW | LEFT_HI)
#define BACKWARDS	(RIGHT_HI | LEFT_LOW)
#define OFF		(RIGHT_OFF| LEFT_OFF)

#define TIMEOUT 	750000         /*75ms */
#define RUN_IN 		50
#define RELEASE_TENSION	10

int
drive_motor (int fd, int dir)
{
  int i;

  if (dir > 0)
    {
      printf (" Motor forwards\n");
      i = FORWARDS;
    }
  else if (dir < 0)
    {
      printf (" Motor backwards\n");
      i = BACKWARDS;
    }
  else
    {
      printf (" Motor off\n");
      i = OFF;
    }


  return ioctl (fd, CPIOC_GPIOSET, &i);
}

int
sensor_led_power (int fd, int on)
{
  int i = TIOCM_DTR;

  printf (" Rotation sensor power %s\n", on ? "on" : "off");

  return ioctl (fd, on ? TIOCMBIS : TIOCMBIC, &i);
}


static int
count_steps (int fd, int count)
{
  struct timeval tv, tv_changed, tv_diff;
  int cts, old_cts = -1;
  int counted = 0;

  printf (" Counting %d steps", count);

  gettimeofday (&tv_changed, NULL);

  while (count)
    {
      ioctl (fd, TIOCMGET, &cts);
      cts &= TIOCM_CTS;

      if (cts != old_cts)
        {
          if (cts)
            {
              counted++;
              count--;
              printf (".");
              fflush (stdout);
            }
          gettimeofday (&tv_changed, NULL);
          old_cts = cts;
        }
      gettimeofday (&tv, NULL);
      timersub (&tv, &tv_changed, &tv_diff);

      if (tv_diff.tv_sec || (tv_diff.tv_usec > TIMEOUT))
        break;

    }

  printf ("%d steps\n", counted);
  return counted;
}




static int
drive_motor_home (int fd)
{
  int ret;
  sensor_led_power (fd, 1);
  drive_motor (fd, -1);
  ret = count_steps (fd, -1);
  drive_motor (fd, 0);
  ret -= count_steps (fd, -1);
  sensor_led_power (fd, 0);
  return ret;
}

static int
drive_motor_count (int fd, int dir, int steps)
{
  int ret;
  sensor_led_power (fd, 1);
  drive_motor (fd, dir);
  ret = count_steps (fd, steps);
  drive_motor (fd, 0);
  ret += count_steps (fd, -1);
  sensor_led_power (fd, 0);
  return ret;
}

int
main (int argc, char *argv[])
{
  int overshoot, guard, pos, wanted, delta;

  int fd = open (argv[1], O_RDWR);

  if (fd < 0)
    return -1;


  pos = drive_motor_home (fd);

  printf ("Motor was %d steps from home\n", pos);


  if (argc == 2)
    {
      pos = drive_motor_count (fd, 1, RELEASE_TENSION);

      printf ("Motor now %d steps from home\n", pos);
      return 0;
    }

  pos = drive_motor_count (fd, 1, RUN_IN);

  overshoot = pos - RUN_IN;     /*overshot */

  wanted = atoi (argv[2]);

  delta = wanted - pos;
  printf ("Now at %d want %d, delta %d\n", pos, wanted, delta);

  guard = overshoot * 3;

  if ((delta > -guard) && (delta < guard))
    {
      printf ("Too close\n");
      pos += drive_motor_count (fd, 1, RUN_IN);
      delta = wanted - pos;
      printf ("Now at %d want %d, delta %d\n", pos, wanted, delta);
    }


  if (delta < 0)
    {
      pos -= drive_motor_count (fd, -1, (-delta) - overshoot);
    }
  else if (delta > 0)
    {
      pos += drive_motor_count (fd, 1, delta - overshoot);
    }

  printf ("Wanted %d steps, got %d steps\n", wanted, pos);

  return 0;
}