libnxter  0.1
Sonar.nxc
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  Sonar.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 */
20 
21 #ifndef _SONAR_H_
22 #define _SONAR_H_
23 
24 #include "CaliberateMotors.nxc"
25 #include "Circle.nxc"
26 
33 #include "Angle.nxc"
34 #include "Vector.nxc"
35 #include "PID.nxc"
36 #include "Motor.nxc"
37 
41 struct Sonar
42 {
43  int sensorPort;
47  /* Thresholds */
53  /* Sensor motor on which it is mounted */
55  int gearRatio;
61  /* Current orientation of sensor motor relative to it's rest position */
66 };
67 
87 void SonarInit(Sonar &sonar, int sensorPort,
88  Vector &sensorLocation, int sensorOffset,
89  int sensorCutOffMin, int sensorCutOffMax,
90  int toleranceAngle, long toleranceDistance,
91  int sensorMotor, int gearRatio,
92  int motorMovePower, int motorScanPower,
93  int sensorMinAngle, int sensorMaxAngle)
94 {
95  sonar.sensorPort = sensorPort;
96  sonar.sensorMotor = sensorMotor;
97  sonar.gearRatio = gearRatio;
98  sonar.sensorMinAngle = sensorMinAngle;
99  sonar.sensorMaxAngle = sensorMaxAngle;
100  sonar.sensorCutOffMin = sensorCutOffMin;
101  sonar.sensorCutOffMax = sensorCutOffMax;
102  sonar.sensorLocation = sensorLocation;
103  sonar.sensorOffset = sensorOffset;
104  sonar.toleranceAngle = toleranceAngle;
105  sonar.toleranceDistance = toleranceDistance;
106  sonar.motorMovePower = motorMovePower;
107  sonar.motorScanPower = motorScanPower;
108  sonar.currentViewAngle = 0;
109  sonar.neutralTachoCount = GetOutput(sonar.sensorMotor, TachoCount);
111  1000, /* PID scale */
112  450, /* P value */
113  0, /* I value */
114  0, /* D value */
115  5, /* Steady state counts threshold */
116  2, /* Output gain */
117  motorMovePower); /* Output absolute maximum */
118 
119 }
120 
125 int SonarGetCurrentViewAngle(Sonar &sonar, Vector &robotLocation)
126 {
127  int absoluteAngle;
128 
129  absoluteAngle = sonar.currentViewAngle;
130  absoluteAngle = AngleAbsAdd(absoluteAngle, sonar.sensorLocation.theta);
131  absoluteAngle = AngleAbsAdd(absoluteAngle, robotLocation.theta);
132  return absoluteAngle;
133 }
134 
139 int SonarGetMinViewAngle(Sonar &sonar, Vector &robotLocation)
140 {
141  int absoluteAngle;
142 
143  absoluteAngle = AngleRelToAbs(sonar.sensorMinAngle);
144  absoluteAngle = AngleAbsAdd(absoluteAngle, sonar.sensorLocation.theta);
145  absoluteAngle = AngleAbsAdd(absoluteAngle, robotLocation.theta);
146  return absoluteAngle;
147 }
148 
153 int SonarGetMaxViewAngle(Sonar &sonar, Vector &robotLocation)
154 {
155  int absoluteAngle;
156 
157  absoluteAngle = AngleRelToAbs(sonar.sensorMaxAngle);
158  absoluteAngle = AngleAbsAdd(absoluteAngle, sonar.sensorLocation.theta);
159  absoluteAngle = AngleAbsAdd(absoluteAngle, robotLocation.theta);
160  return absoluteAngle;
161 }
162 
166 void SonarGetPosition(Sonar &sonar, Vector &robotLocation, Vector &retVec)
167 {
168  /* Orient sonar to robot orientation */
169  retVec = sonar.sensorLocation;
170  VectorRotate(retVec, robotLocation.theta);
171  VectorAdd(retVec, robotLocation);
172  retVec.theta = robotLocation.theta + sonar.sensorLocation.theta;
173 }
174 
189 bool SonarSweep(Sonar &sonar, int angleFrom, int angleTo, long &rangeData[])
190 {
191  int samplingPeriod = 20; /* msec */
192  long motorTacho, output, lastError, tachoFrom, tachoTo;
193  long senseDistance;
194  int lastViewAngle, senseAngle, dataSize, dataIndex;
195  int i;
196 
197  /* Run the motor. Speed is evaluated later during positioning */
198  MotorStart(sonar.sensorMotor, 0);
199 
200  lastViewAngle = sonar.currentViewAngle;
201 
202  dataSize = ((Abs(angleTo - angleFrom) + 1) * 2);
203  ArrayInit(rangeData, 0, dataSize);
204 
205  /* Initialize rangeData array with angles and unread values (-1) */
206  if (angleTo > angleFrom)
207  {
208  dataIndex = 0;
209  for (i = angleFrom; i <= angleTo; i++)
210  {
211  rangeData[dataIndex] = i;
212  dataIndex = dataIndex + 1;
213  rangeData[dataIndex] = -1;
214  dataIndex = dataIndex + 1;
215  }
216  }
217  else
218  {
219  dataIndex = 0;
220  for (i = angleFrom; i >= angleTo; i--)
221  {
222  rangeData[dataIndex] = i;
223  dataIndex = dataIndex + 1;
224  rangeData[dataIndex] = -1;
225  dataIndex = dataIndex + 1;
226  }
227  }
228 
229  tachoFrom = ((angleFrom - sonar.currentViewAngle) * sonar.gearRatio)/100;
230  tachoTo = ((angleTo - sonar.currentViewAngle) * sonar.gearRatio)/100;
231 
232  /* Find the closest point either 'From' and 'To' angles from current angle,
233  * to start scanning
234  */
235  if (Abs((lastViewAngle - angleFrom)) > Abs((lastViewAngle - angleTo)))
236  {
237  long tmp;
238  tmp = tachoFrom;
239  tachoTo = tachoFrom;
240  tachoFrom = tmp;
241  }
242 
243  /* Move to the closest point */
244  sonar.pidControl.absMaxOutput = sonar.motorMovePower;
245  PIDControlSetPoint(sonar.pidControl, tachoFrom);
246  while(!PIDControlCheckEnd(sonar.pidControl))
247  {
248  motorTacho = GetOutput(sonar.sensorMotor, BlockTachoCount);
249  senseAngle = ((motorTacho * 100)/sonar.gearRatio);
250  sonar.currentViewAngle = lastViewAngle + senseAngle;
251  output = PIDControlStep(sonar.pidControl, motorTacho);
252  MotorSetPower(sonar.sensorMotor, output);
253  Wait(samplingPeriod);
254  }
255 
256  /* Rotate to the 'to' angle and at the same time take sonar readings
257  * and fill up the rangeData array
258  */
259  sonar.pidControl.absMaxOutput = sonar.motorScanPower;
260  PIDControlSetPoint(sonar.pidControl, tachoTo);
261  while(!PIDControlCheckEnd(sonar.pidControl))
262  {
263  motorTacho = GetOutput(sonar.sensorMotor, BlockTachoCount);
264 
265  /* Read sensor */
266  senseAngle = ((motorTacho * 100)/sonar.gearRatio);
267  sonar.currentViewAngle = lastViewAngle + senseAngle;
268  senseDistance = SensorUS(sonar.sensorPort); /* cm */
269  senseDistance = senseDistance * 10; /* mm */
270  senseDistance += sonar.sensorOffset; /* Reading should be from mount */
271  dataIndex = 2 * Abs(sonar.currentViewAngle - angleFrom);
272  if (dataIndex >= 0 && dataIndex < dataSize)
273  {
274  if ((senseDistance >= sonar.sensorCutOffMin) &&
275  (senseDistance <= sonar.sensorCutOffMax))
276  {
277  rangeData[dataIndex] = sonar.currentViewAngle;
278  rangeData[(dataIndex + 1)] = senseDistance;
279  }
280  }
281  /* Adjust output */
282  output = PIDControlStep(sonar.pidControl, motorTacho);
283  MotorSetPower(sonar.sensorMotor, output);
284  Wait(samplingPeriod);
285  }
286  MotorStop(sonar.sensorMotor);
287 }
288 
300 void SonarSweepPrint(Sonar &sonar, long &rangeData[], int waitTime)
301 {
302  int arrLen = ArrayLen(rangeData);
303  /*
304  for (int i = 0; i < arrLen; i++)
305  {
306  int x = (i/8) * 40;
307  int y = (i % 8) * 8;
308  NumOut(x, y, rangeData[i], true);
309  }
310  Wait(waitTime);
311  */
312  ClearScreen();
313  for (int i = 0; i < ArrayLen(rangeData); i++)
314  {
315  int theta = 90 + rangeData[i];
316  i = i + 1;
317  int d = rangeData[i]/5;
318  int x = (d * Cos(theta)) / 100;
319  int y = (d * Sin(theta)) / 100;
320  if (d > 0)
321  CircleOut(x + 50, y, 2, false);
322  }
323  Wait(waitTime);
324 }
325 
336 bool SonarGetObjectDistanceAtAngle(Sonar &sonar, Vector robotLocation,
337  int viewAngle, long viewDistance,
338  int &retBestAngle, long &retBestDistance)
339 {
340  Vector returnObject;
341  int angleFrom, angleTo, i;
342  long minima;
343  long rangeData[];
344  bool found;
345 
346  if ((viewAngle < sonar.sensorMinAngle) || (viewAngle > sonar.sensorMaxAngle))
347  return false;
348 
349  angleFrom = viewAngle - (sonar.toleranceAngle/2);
350  angleTo = viewAngle + (sonar.toleranceAngle/2);
351 
352  SonarSweep(sonar, angleFrom, angleTo, rangeData);
353  /* SonarSweepPrint(sonar, rangeData, 2000); */
354 
355  /* Find the closest reading to the expected distance */
356  minima = 1000000;
357  for (i = 0; i < ArrayLen(rangeData); i = i + 2)
358  {
359  int dist = rangeData[i+1];
360  if (dist > 0 && Abs(dist - viewDistance) < minima)
361  {
362  retBestDistance = dist;
363  retBestAngle = rangeData[i];
364  minima = Abs(retBestDistance - viewDistance);
365  found = true;
366  }
367  }
368  return found;
369 }
370 
383 bool SonarGetObjectDistanceAtLocation(Sonar &sonar, Vector robotLocation,
384  Vector &viewLocation, int &retBestAngle,
385  long &retBestDistance)
386 {
387  Vector sonarLocationWorld, returnObject, objectError;
388  int viewAngle, bestAngle;
389  long viewDistance, bestDistance;
390  long distance;
391  bool found;
392 
393  /* Sonar location in world */
394  SonarGetPosition(sonar, robotLocation, sonarLocationWorld);
395 
396  /* Absolute view angle in the world - 0 to 360 */
397  viewAngle = VectorGetAngleVec(sonarLocationWorld, viewLocation);
398  /* View angle relative to sensor orientation */
399  viewAngle = AngleAbsSub(viewAngle, sonarLocationWorld.theta);
400  /* Relative view angle - -180 to +180 */
401  viewAngle = AngleAbsToRel(viewAngle);
402 
403  viewDistance = VectorGetDistanceVec(sonarLocationWorld, viewLocation);
404 
405  /* Find object at the relative view angle */
406  found = SonarGetObjectDistanceAtAngle(sonar, robotLocation,
407  viewAngle, viewDistance,
408  bestAngle, bestDistance);
409  if (found)
410  {
411  retBestAngle = bestAngle;
412  retBestDistance = bestDistance;
413  }
414  return found;
415 }
416 
428 bool SonarTriangulateLandmarks(Sonar &sonar, Vector &robotLocation,
429  Vector &landmark1, Vector &landmark2,
430  Vector &retLocation)
431 {
432  bool found, intersect;
433  long d1, d2;
434  int angle1, angle2;
435  Vector sonarLocation, intersect1, intersect2;
436 
437  found = SonarGetObjectDistanceAtLocation(sonar, robotLocation, landmark1,
438  angle1, d1);
439  if (found == false)
440  return false;
441 
442  found = SonarGetObjectDistanceAtLocation(sonar, robotLocation, landmark2,
443  angle2, d2);
444  if (found == false)
445  return false;
446 
447  intersect = CircleIntersectCircle(landmark1, d1, landmark2, d2,
448  intersect1, intersect2);
449  if (intersect == false)
450  return false;
451 
452  /* Sonar location in world coordinate */
453  SonarGetPosition(sonar, robotLocation, sonarLocation);
454 
455  d1 = VectorGetDistanceVec(sonarLocation, intersect1);
456  d2 = VectorGetDistanceVec(sonarLocation, intersect2);
457 
458  /* retLoation, currently triangulated position of the sonar, is one
459  * closest to the current sonar position.
460  */
461  if (d1 > d2)
462  retLocation = intersect2;
463  else
464  retLocation = intersect1;
465 
466  sonarLocation = sonar.sensorLocation;
467  VectorRotate(sonarLocation, retLocation.theta);
468  VectorSubtract(retLocation, sonarLocation);
469 
470  /* Fixing robot orientation */
471  /* Theta set to sonar angle (in world coordinate) */
472  retLocation.theta = VectorGetAngleVec(retLocation, landmark2);
473 
474  /* Theta set to robot orientation by removing sonar angle at the time
475  * of landmark2 observation.
476  */
477  retLocation.theta = AngleAbsSub(retLocation.theta, angle2);
478 
479  return true;
480 }
481 
496 bool SonarFindObject(Sonar &sonar, Vector robotLocation, Vector &retObject)
497 {
498  long senseDistance = SensorUS(sonar.sensorPort); /* cm */
499  senseDistance = senseDistance * 10; /* mm */
500  senseDistance += sonar.sensorOffset; /* Reading from sensor mount point */
501  if ((senseDistance >= sonar.sensorCutOffMin) &&
502  (senseDistance <= sonar.sensorCutOffMax))
503  {
504  /* Translate position to robot location */
505  Vector retObject = robotLocation;
506  //VectorPrint(retObject, "robotLocation", 2000);
507 
508  /* Translate position to sonar location */
509  VectorAdd(retObject, sonar.sensorLocation);
510  //VectorPrint(retObject, "sensorLocation", 2000);
511 
512  /* Object location relative to the sensor */
513  int objectAngle = retObject.theta + sonar.currentViewAngle;
514 
515  /* Translate position to object location */
516  VectorTranslate(retObject, senseDistance, objectAngle);
517  //VectorPrint(retObject, "objectLocation", 2000);
518 
519  /* Orientation is meaningless for an object location */
520  retObject.theta = 0;
521 
522  return true;
523  }
524  return false;
525 }
526 
536 bool SonarFindObjectAtAngle(Sonar &sonar, Vector robotLocation,
537  int viewAngle, long viewDistance,
538  Vector &retObject)
539 {
540  Vector returnObject;
541  int angleFrom, angleTo, bestAngle, i;
542  long bestDistance, minima;
543  long rangeData[];
544  bool found;
545 
546  if ((viewAngle < sonar.sensorMinAngle) || (viewAngle > sonar.sensorMaxAngle))
547  return false;
548 
549  angleFrom = viewAngle - (sonar.toleranceAngle/2);
550  angleTo = viewAngle + (sonar.toleranceAngle/2);
551 
552  SonarSweep(sonar, angleFrom, angleTo, rangeData);
553  //SonarSweepPrint(sonar, rangeData, 2000);
554 
555  /* Find the closest reading to the expected distance */
556  minima = 1000000;
557  for (i = 0; i < ArrayLen(rangeData); i = i + 2)
558  {
559  int dist = rangeData[i+1];
560  if (dist > 0 && Abs(dist - viewDistance) < minima)
561  {
562  bestDistance = dist;
563  bestAngle = rangeData[i];
564  minima = Abs(bestDistance - viewDistance);
565  found = true;
566  NumOut(0, LCD_LINE1, dist, true);
567  NumOut(0, LCD_LINE2, viewDistance, false);
568  NumOut(0, LCD_LINE3, minima, false);
569  Wait(1000);
570  }
571  }
572 
573  if (found)
574  {
575  /* Translate position to robot location */
576  Vector retObject = robotLocation;
577 
578  /* Translate position to sonar location */
579  VectorAdd(retObject, sonar.sensorLocation);
580 
581  /* Object location relative to the sensor */
582  int objectAngle = retObject.theta + bestAngle;
583 
584  /* Translate position to object location */
585  VectorTranslate(retObject, bestDistance, objectAngle);
586 
587  /* Orientation is meaningless for an object location */
588  retObject.theta = 0;
589  }
590  return found;
591 }
592 
602 bool SonarFindObjectAtLocation(Sonar &sonar, Vector robotLocation,
603  Vector viewLocation, Vector &retObject)
604 {
605  Vector viewLocationRelative, sonarLocationWorld, returnObject, objectError;
606  int viewAngle;
607  long viewDistance;
608  bool found;
609 
610  // VectorPrint(viewLocationRelative, "view location", 2000);
611 
612  found = false;
613 
614  /* Sonar location in world */
615  sonarLocationWorld = robotLocation;
616  // VectorPrint(sonarLocationWorld, "Robot location", 2000);
617 
618  VectorAdd(sonarLocationWorld, sonar.sensorLocation);
619  // VectorPrint(sonarLocationWorld, "Sonar location", 2000);
620 
621  /* Reference view location relative to sensor */
622  viewLocationRelative = viewLocation;
623  VectorSubtract(viewLocationRelative, sonarLocationWorld);
624  // VectorPrint(viewLocationRelative, "view rel to sonar", 2000);
625 
626  /* Reference view angle relative to sensor - 0 to 360 */
627  viewAngle = VectorGetAngle(viewLocationRelative);
628  viewDistance = VectorGetDistance(viewLocationRelative);
629 
630  /* view angle - -180 to +180 */
631  if (viewAngle > 180) viewAngle = viewAngle - 360;
632 
633  /* Find object at the relative view angle */
634  found = SonarFindObjectAtAngle(sonar, robotLocation, viewAngle,
635  viewDistance, returnObject);
636  objectError = returnObject;
637  VectorSubtract(objectError, viewLocation);
638 
639  if (found && (VectorGetDistance(objectError) < sonar.toleranceDistance))
640  retObject = returnObject;
641  else
642  found = false;
643  return found;
644 }
645 
657 bool SonarFindObjectRelative(Sonar &sonar, Vector &retObject)
658 {
659  long senseDistance, objectAngle;
660 
661  senseDistance = SensorUS(sonar.sensorPort); /* cm */
662  senseDistance = senseDistance * 10; /* mm */
663  senseDistance += sonar.sensorOffset; /* Reading from sensor mount point */
664  if (senseDistance < sonar.sensorCutOffMin && senseDistance > sonar.sensorCutOffMax)
665  {
666  /* Object location relative to the sensor */
667  objectAngle = sonar.sensorLocation.theta + sonar.currentViewAngle;
668  retObject.x = sonar.sensorLocation.x + senseDistance * Cos(objectAngle);
669  retObject.y = sonar.sensorLocation.y + senseDistance * Sin(objectAngle);
670  retObject.theta = 0;
671  return true;
672  }
673  return false;
674 }
675 
685 bool SonarFindObjectAtAngleRelative(Sonar &sonar, int viewAngle,
686  Vector &retObject)
687 {
688  int deltaAngle;
689 
690  if ((viewAngle < sonar.sensorMinAngle) || (viewAngle > sonar.sensorMaxAngle))
691  return false;
692  deltaAngle = viewAngle - sonar.currentViewAngle;
693  sonar.currentViewAngle = viewAngle;
694  deltaAngle = (deltaAngle * sonar.gearRatio) / 100;
695  if (deltaAngle > 0)
696  RotateMotor(sonar.sensorMotor, sonar.motorMovePower, deltaAngle);
697  else if (deltaAngle < 0)
698  RotateMotor(sonar.sensorMotor, -sonar.motorMovePower, -deltaAngle);
699  return SonarFindObjectRelative(sonar, retObject);
700 }
701 
702 #ifdef ENABLE_TEST
703 #include "RobotPrima.nxc"
704 
705 void TestSonar ()
706 {
707  Vector robotLocation;
708  Vector viewLocation1;
709  Vector viewLocation2;
710  Sonar sonar;
711  Vector sensorLocation;
712  Vector retLocation;
713  int retBestAngle;
714  long retBestDistance;
715  bool success;
716  long rangeData[];
717 
718  VectorInit(sensorLocation, 60, 0, 0);
719  VectorInit(robotLocation, 0, 0, 0);
720  VectorInit(viewLocation1, 250, 250, 0);
721  VectorInit(viewLocation2, 300, 0, 0);
722 
723  SetSensorLowspeed(PRIMA_RANGE_SENSOR);
724  SonarInit(sonar, PRIMA_RANGE_SENSOR, sensorLocation,
725  10, /* Sensor reading offset from mount point */
726  60, /* Sensor range cutoff (min): 6 cm */
727  2000, /* Sensor range cutoff (max): 200 cm */
728  20, /* Sensor find area angle: +/- 10 degrees */
729  100, /* Sensor find area distance: +/- 10 cm */
730  OUT_A, /* sonar motor */
731  500, /* sonar motor gear ratio */
732  100, /* sonar move motor speed */
733  10, /* sonar scan motor speed */
734  -70, /* sonar motor min angle */
735  +70); /* sonar motor max angle */
736  /*
737  SonarSweep(sonar, -10, 10, rangeData);
738  SonarSweepPrint(sonar, rangeData, 2000);
739 
740  success = SonarGetObjectDistanceAtAngle(sonar, robotLocation, 45, 150,
741  retBestAngle, retBestDistance);
742  if (success == true)
743  {
744  TextOut(0, LCD_LINE1, "Best Angle 1", true);
745  NumOut(0, LCD_LINE2, retBestAngle, false);
746  TextOut(0, LCD_LINE3, "Best Distance 1", false);
747  NumOut(0, LCD_LINE4, retBestDistance, false);
748  Wait(2000);
749  }
750  SonarGetObjectDistanceAtLocation (sonar, robotLocation, viewLocation1, retBestAngle,
751  retBestDistance);
752  success = true;
753  if (success == true)
754  {
755  TextOut(0, LCD_LINE1, "Best Angle 1", true);
756  NumOut(0, LCD_LINE2, retBestAngle, false);
757  TextOut(0, LCD_LINE3, "Best Distance 1", false);
758  NumOut(0, LCD_LINE4, retBestDistance, false);
759  Wait(2000);
760  }
761  */
762  success = SonarTriangulateLandmarks(sonar, robotLocation, viewLocation1,
763  viewLocation2, retLocation);
764  if (success == true)
765  {
766  VectorPrint(retLocation, "Robot Location", 2000);
767  }
768  Wait(20000);
769 }
770 
771 /*
772 task main()
773 {
774  TestSonar();
775 }
776 */
777 #endif /* ENABLE_TEST */
778 #endif /* __SONAR_H_ */
779 
long PIDControlStep(PIDControl &pidControl, long currentPoint)
Steps the PID controller. currentPoint is the current state of the system. The return value is the ou...
Definition: PID.nxc:142
A vector that represents a 2D point by x-y coordinates and a direction angle.
Definition: Vector.nxc:43
bool SonarFindObjectRelative(Sonar &sonar, Vector &retObject)
Returns the relative (to robot reference) location of an object detected by the sonar, if any was detected within the range of sensorCutOffMin and sensorCutOffMax.
Definition: Sonar.nxc:657
bool SonarSweep(Sonar &sonar, int angleFrom, int angleTo, long &rangeData[])
Sweeps the sonar from angleFrom to angleTo and returns an array of angle/distance pairs...
Definition: Sonar.nxc:189
int toleranceAngle
Definition: Sonar.nxc:50
void MotorStart(int motor, int power)
Starts the given motor with the given power.
Definition: Motor.nxc:35
int motorScanPower
Definition: Sonar.nxc:59
int currentViewAngle
Definition: Sonar.nxc:62
PID controller class.
Definition: PID.nxc:32
void SonarGetPosition(Sonar &sonar, Vector &robotLocation, Vector &retVec)
Gets the current sonar mount position and orientation in world coordinate.
Definition: Sonar.nxc:166
long VectorGetDistanceVec(Vector &a, Vector &b)
Gets the distance between given two vectors.
Definition: Vector.nxc:100
int sensorMaxAngle
Definition: Sonar.nxc:57
void VectorRotate(Vector &vec, long theta)
Rotates the point in the vector by given angle. The direction component in the vector is not touched...
Definition: Vector.nxc:211
long VectorGetDistance(Vector &a)
Gets the polar distance of the given vector. It's the distance of its x-y coordicate from origin...
Definition: Vector.nxc:76
bool SonarGetObjectDistanceAtLocation(Sonar &sonar, Vector robotLocation, Vector &viewLocation, int &retBestAngle, long &retBestDistance)
Returns the distance of an object expected to be found at the given viewLocation (in world coordinate...
Definition: Sonar.nxc:383
Sonar class that represents an ultrasonic sensor mounted on a robot.
Definition: Sonar.nxc:41
int motorMovePower
Definition: Sonar.nxc:58
int VectorGetAngle(Vector &a)
Gets the polar angle of the given vectors. It's the angle of its x-y coordinate from origin...
Definition: Vector.nxc:88
void SonarSweepPrint(Sonar &sonar, long &rangeData[], int waitTime)
Prints the rangeData array on screen like a radar. Used for debugging.
Definition: Sonar.nxc:300
PIDControl pidControl
Definition: Sonar.nxc:64
bool SonarFindObjectAtLocation(Sonar &sonar, Vector robotLocation, Vector viewLocation, Vector &retObject)
Returns the absolute location of an object found around the reference view location.
Definition: Sonar.nxc:602
int AngleAbsAdd(int absAngleA, int angle)
Adds angle to an absolute angle (0 .. 360) absAngleA. Returns an absolute angle properly truncated to...
Definition: Angle.nxc:65
int sensorCutOffMax
Definition: Sonar.nxc:49
int SonarGetCurrentViewAngle(Sonar &sonar, Vector &robotLocation)
Gets current absolute view angle in the world at given robot orientation.
Definition: Sonar.nxc:125
A simple 3-components vector implementation (2D point + direction)
bool SonarFindObject(Sonar &sonar, Vector robotLocation, Vector &retObject)
Returns the absolute location of an object in the world detected by the sonar at current angle...
Definition: Sonar.nxc:496
void SonarInit(Sonar &sonar, int sensorPort, Vector &sensorLocation, int sensorOffset, int sensorCutOffMin, int sensorCutOffMax, int toleranceAngle, long toleranceDistance, int sensorMotor, int gearRatio, int motorMovePower, int motorScanPower, int sensorMinAngle, int sensorMaxAngle)
Initializes sonar with an ultrasonic sensor connected at sensorPort and mounted on a motor sensorMoto...
Definition: Sonar.nxc:87
long theta
Definition: Vector.nxc:47
int sensorMotor
Definition: Sonar.nxc:54
void MotorSetPower(int motor, int power)
Set the power of given motor.
Definition: Motor.nxc:59
void PIDControlInit(PIDControl &pidControl, long PIDScale, long PValue, long IValue, long DValue, int steadyStateCountThreshold, long outputGain, long absMaxOutput)
Initializes a PID controller with the given P, I and D values, output parameters. ...
Definition: PID.nxc:71
long neutralTachoCount
Definition: Sonar.nxc:63
int SonarGetMinViewAngle(Sonar &sonar, Vector &robotLocation)
Gets the absolute minimum view angle in the world at given robot orientation.
Definition: Sonar.nxc:139
bool PIDControlCheckEnd(PIDControl &pidControl)
Checks if the PID controller has ended.
Definition: PID.nxc:120
void VectorSubtract(Vector &a, Vector &b)
Subtracts vector b from a. i.e. a -= b.
Definition: Vector.nxc:182
Vector sensorLocation
Definition: Sonar.nxc:44
bool SonarFindObjectAtAngle(Sonar &sonar, Vector robotLocation, int viewAngle, long viewDistance, Vector &retObject)
Returns the absolute location of an object found at given view angle.
Definition: Sonar.nxc:536
Provides simple arithmetic for absolute and relative angles.
void VectorTranslate(Vector &a, long distance, int angle)
Translate the vector point to a new point moved the given distance towards the given direction angle...
Definition: Vector.nxc:163
Generic PID controller implementation.
void MotorStop(int motor)
Stops the given motor.
Definition: Motor.nxc:47
long toleranceDistance
Definition: Sonar.nxc:51
long y
Definition: Vector.nxc:46
void PIDControlSetPoint(PIDControl &pidControl, long setPoint)
Sets a new set point for the controller. The controller state is reset and the new target is pursued...
Definition: PID.nxc:103
int sensorMinAngle
Definition: Sonar.nxc:56
Motor setup caliberation utilities.
bool SonarTriangulateLandmarks(Sonar &sonar, Vector &robotLocation, Vector &landmark1, Vector &landmark2, Vector &retLocation)
Triangulates the position robot from two known landmarks.
Definition: Sonar.nxc:428
int gearRatio
Definition: Sonar.nxc:55
int SonarGetMaxViewAngle(Sonar &sonar, Vector &robotLocation)
Gets the absolute maximum view angle in the world at given robot orientation.
Definition: Sonar.nxc:153
int sensorCutOffMin
Definition: Sonar.nxc:48
long x
Definition: Vector.nxc:45
long absMaxOutput
Definition: PID.nxc:45
int AngleAbsToRel(int absAngle)
Converts absolute angle (0 .. 360) to relative angle (-180 .. 180)
Definition: Angle.nxc:44
int sensorOffset
Definition: Sonar.nxc:45
int AngleAbsSub(int absAngleA, int angle)
Subtracts angle from an absolute angle (0 .. 360) absAngleA. Returns an absolute angle properly trunc...
Definition: Angle.nxc:76
An experimental robotic platform for developing this library.
bool CircleIntersectCircle(Vector &circleACenter, long circleARadius, Vector &circleBCenter, long circleBRadius, Vector &retIntersect1, Vector &retIntersect2)
Determins the 2 points of intersection of two overlapping circles.
Definition: Circle.nxc:45
int sensorPort
Definition: Sonar.nxc:43
Motor control implementation.
bool SonarGetObjectDistanceAtAngle(Sonar &sonar, Vector robotLocation, int viewAngle, long viewDistance, int &retBestAngle, long &retBestDistance)
Returns the distance of an object expected to be found at the given viewAngle (relative to sonar neut...
Definition: Sonar.nxc:336
void VectorAdd(Vector &a, Vector &b)
Adds vector b to a. i.e. a += b.
Definition: Vector.nxc:172
Circle geometry operations.
int AngleRelToAbs(int relAngle)
Converts relative angle (-180 .. 180) to absolute angle (0 .. 360)
Definition: Angle.nxc:54
long VectorGetAngleVec(Vector &a, Vector &b)
Gets the angle of the line formed by the given two vectors.
Definition: Vector.nxc:110
bool SonarFindObjectAtAngleRelative(Sonar &sonar, int viewAngle, Vector &retObject)
Returns the relative (to robot's reference point) location of an object found at given view angle...
Definition: Sonar.nxc:685
void VectorInit(Vector &a, long x, long y, long theta)
Initializes the vector with given x, y and theta components.
Definition: Vector.nxc:122