libnxter  0.1
CaliberateMotors.nxc
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  CaliberateMotors.nxc
4  Copyright (C) 2008 Naba Kumar <naba@gnome.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
25 /* Power assignments */
26 #define MOTOR_POWER_FAST 75
27 #define MOTOR_POWER_MODERATE 75
28 #define MOTOR_POWER_SLOW 30
29 
30 /* Motor assignments */
31 #define MOTOR_TILT OUT_C
32 #define MOTOR_STRIDE OUT_B
33 
34 /* Sensor assignments */
35 #define TOUCH_SENSOR IN_1
36 #define LIGHT_SENSOR IN_3
37 
38 /* Light sensor threshold below which proximity is confirmed */
39 #define LIGHT_SENSOR_PROXIMITY_THRESHOLD 40
40 
41 /* Tolerance degrees for cycle caliberation. Cycle count will be trimmed */
42 #define CALIBERATION_ERROR_TOLERANCE 90
43 
44 int GetSensorProximity (int sensor)
45 {
46  if (sensor == TOUCH_SENSOR)
47  return Sensor(TOUCH_SENSOR);
48  if (Sensor(LIGHT_SENSOR) < LIGHT_SENSOR_PROXIMITY_THRESHOLD)
49  return 1;
50  else
51  return 0;
52 }
53 
54 int CaliberateMotorArc(int motor, int power, int sensor,
55  int postAlignmentAngle)
56 {
57  if (!GetSensorProximity(sensor))
58  {
59  OnFwd(motor, power);
60  while (!GetSensorProximity(sensor)) Wait(20);
61  Off(motor);
62  }
63  RotateMotor(motor, power, postAlignmentAngle);
64 }
65 
75 int CaliberateMotor(int motor, int sensor, int postAlignmentPercentage)
76 {
77  int extremeTachoMin;
78  int extremeTachoMax;
79  int cycleCountMin;
80  int cycleCountMax;
81  int cycleCount;
82  int alignDegrees;
83 
84  /* Start tilt motor and monitor touch sensor for rythm cycle */
85  OnFwd(motor, MOTOR_POWER_FAST);
86 
87  /* First let the sensor get out of active state */
88  while (GetSensorProximity(sensor));
89 
90  /* Wait for sensor to activate */
91  while (!GetSensorProximity(sensor));
92 
93  /* Start both extreme count and cycle count */
94  extremeTachoMin = GetOutput(motor, TachoCount);
95  cycleCountMin = extremeTachoMin;
96 
97  /* Wait for sensor to deactivate */
98  while (GetSensorProximity(sensor));
99 
100  /* end count */
101  extremeTachoMax = GetOutput(motor, TachoCount);
102 
103  /* Wait for sensor to activate again */
104  while (!GetSensorProximity(sensor));
105 
106  /* End cycle count */
107  cycleCountMax = GetOutput(motor, TachoCount);
108 
109  /* Stop the motor */
110  Off(motor);
111 
112  /* Correct any error within cycle count */
113  cycleCount = cycleCountMax - cycleCountMin;
114  int errorCycleCount = cycleCount % CALIBERATION_ERROR_TOLERANCE;
115  if (errorCycleCount < CALIBERATION_ERROR_TOLERANCE/2)
116  cycleCount -= errorCycleCount;
117  else
118  cycleCount += CALIBERATION_ERROR_TOLERANCE - errorCycleCount;
119 
120  /* Align the moter to neutral */
121  /* Move the tacho limit to neutralAlignment + 1/2 extreme sensor period */
122  alignDegrees = (cycleCount * postAlignmentPercentage)/100 +
123  (extremeTachoMax - extremeTachoMin)/2;
124  RotateMotor(motor, MOTOR_POWER_FAST, alignDegrees);
125 
126  return cycleCount;
127 }
128 
129 #if 0
130 task main ()
131 {
132  int tiltCycleCount, strideCycleCount;
133  int currentPositionTilt = 0, currentPositionStride = 0;
134 
135  SetSensorTouch(TOUCH_SENSOR);
136  SetSensorLight(LIGHT_SENSOR);
137 
138  tiltCycleCount = CaliberateMotor(MOTOR_TILT, TOUCH_SENSOR, 0);
139  TextOut(0, LCD_LINE1, NumToStr(tiltCycleCount), true);
140 
141  strideCycleCount = CaliberateMotor(MOTOR_STRIDE, LIGHT_SENSOR, 0);
142  TextOut(0, LCD_LINE2, NumToStr(strideCycleCount), false);
143 
144  RotateMotor(MOTOR_STRIDE, MOTOR_POWER_FAST, strideCycleCount/8);
145  RotateMotor(MOTOR_TILT, MOTOR_POWER_FAST, tiltCycleCount/4);
146 }
147 #endif
148 
int CaliberateMotor(int motor, int sensor, int postAlignmentPercentage)
Caliberates a rotary motor setup that has a sensor attached to detect a cyclic position.