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)
132 return absoluteAngle;
146 return absoluteAngle;
160 return absoluteAngle;
191 int samplingPeriod = 20;
192 long motorTacho, output, lastError, tachoFrom, tachoTo;
194 int lastViewAngle, senseAngle, dataSize, dataIndex;
202 dataSize = ((Abs(angleTo - angleFrom) + 1) * 2);
203 ArrayInit(rangeData, 0, dataSize);
206 if (angleTo > angleFrom)
209 for (i = angleFrom; i <= angleTo; i++)
211 rangeData[dataIndex] = i;
212 dataIndex = dataIndex + 1;
213 rangeData[dataIndex] = -1;
214 dataIndex = dataIndex + 1;
220 for (i = angleFrom; i >= angleTo; i--)
222 rangeData[dataIndex] = i;
223 dataIndex = dataIndex + 1;
224 rangeData[dataIndex] = -1;
225 dataIndex = dataIndex + 1;
235 if (Abs((lastViewAngle - angleFrom)) > Abs((lastViewAngle - angleTo)))
248 motorTacho = GetOutput(sonar.
sensorMotor, BlockTachoCount);
249 senseAngle = ((motorTacho * 100)/sonar.
gearRatio);
253 Wait(samplingPeriod);
263 motorTacho = GetOutput(sonar.
sensorMotor, BlockTachoCount);
266 senseAngle = ((motorTacho * 100)/sonar.
gearRatio);
269 senseDistance = senseDistance * 10;
272 if (dataIndex >= 0 && dataIndex < dataSize)
278 rangeData[(dataIndex + 1)] = senseDistance;
284 Wait(samplingPeriod);
302 int arrLen = ArrayLen(rangeData);
313 for (
int i = 0; i < ArrayLen(rangeData); i++)
315 int theta = 90 + rangeData[i];
317 int d = rangeData[i]/5;
318 int x = (d * Cos(theta)) / 100;
319 int y = (d * Sin(theta)) / 100;
321 CircleOut(x + 50, y, 2,
false);
337 int viewAngle,
long viewDistance,
338 int &retBestAngle,
long &retBestDistance)
341 int angleFrom, angleTo, i;
352 SonarSweep(sonar, angleFrom, angleTo, rangeData);
357 for (i = 0; i < ArrayLen(rangeData); i = i + 2)
359 int dist = rangeData[i+1];
360 if (dist > 0 && Abs(dist - viewDistance) < minima)
362 retBestDistance = dist;
363 retBestAngle = rangeData[i];
364 minima = Abs(retBestDistance - viewDistance);
384 Vector &viewLocation,
int &retBestAngle,
385 long &retBestDistance)
387 Vector sonarLocationWorld, returnObject, objectError;
388 int viewAngle, bestAngle;
389 long viewDistance, bestDistance;
407 viewAngle, viewDistance,
408 bestAngle, bestDistance);
411 retBestAngle = bestAngle;
412 retBestDistance = bestDistance;
432 bool found, intersect;
435 Vector sonarLocation, intersect1, intersect2;
448 intersect1, intersect2);
449 if (intersect ==
false)
462 retLocation = intersect2;
464 retLocation = intersect1;
498 long senseDistance = SensorUS(sonar.
sensorPort);
499 senseDistance = senseDistance * 10;
505 Vector retObject = robotLocation;
537 int viewAngle,
long viewDistance,
541 int angleFrom, angleTo, bestAngle, i;
542 long bestDistance, minima;
552 SonarSweep(sonar, angleFrom, angleTo, rangeData);
557 for (i = 0; i < ArrayLen(rangeData); i = i + 2)
559 int dist = rangeData[i+1];
560 if (dist > 0 && Abs(dist - viewDistance) < minima)
563 bestAngle = rangeData[i];
564 minima = Abs(bestDistance - viewDistance);
566 NumOut(0, LCD_LINE1, dist,
true);
567 NumOut(0, LCD_LINE2, viewDistance,
false);
568 NumOut(0, LCD_LINE3, minima,
false);
576 Vector retObject = robotLocation;
582 int objectAngle = retObject.
theta + bestAngle;
605 Vector viewLocationRelative, sonarLocationWorld, returnObject, objectError;
615 sonarLocationWorld = robotLocation;
622 viewLocationRelative = viewLocation;
631 if (viewAngle > 180) viewAngle = viewAngle - 360;
635 viewDistance, returnObject);
636 objectError = returnObject;
640 retObject = returnObject;
659 long senseDistance, objectAngle;
662 senseDistance = senseDistance * 10;
664 if (senseDistance < sonar.sensorCutOffMin && senseDistance > sonar.
sensorCutOffMax)
694 deltaAngle = (deltaAngle * sonar.
gearRatio) / 100;
697 else if (deltaAngle < 0)
714 long retBestDistance;
723 SetSensorLowspeed(PRIMA_RANGE_SENSOR);
724 SonarInit(sonar, PRIMA_RANGE_SENSOR, sensorLocation,
763 viewLocation2, retLocation);
766 VectorPrint(retLocation,
"Robot Location", 2000);
long PIDControlStep(PIDControl &pidControl, long currentPoint)
Steps the PID controller. currentPoint is the current state of the system. The return value is the ou...
A vector that represents a 2D point by x-y coordinates and a direction angle.
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.
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...
void MotorStart(int motor, int power)
Starts the given motor with the given power.
void SonarGetPosition(Sonar &sonar, Vector &robotLocation, Vector &retVec)
Gets the current sonar mount position and orientation in world coordinate.
long VectorGetDistanceVec(Vector &a, Vector &b)
Gets the distance between given two vectors.
void VectorRotate(Vector &vec, long theta)
Rotates the point in the vector by given angle. The direction component in the vector is not touched...
long VectorGetDistance(Vector &a)
Gets the polar distance of the given vector. It's the distance of its x-y coordicate from origin...
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...
Sonar class that represents an ultrasonic sensor mounted on a robot.
int VectorGetAngle(Vector &a)
Gets the polar angle of the given vectors. It's the angle of its x-y coordinate from origin...
void SonarSweepPrint(Sonar &sonar, long &rangeData[], int waitTime)
Prints the rangeData array on screen like a radar. Used for debugging.
bool SonarFindObjectAtLocation(Sonar &sonar, Vector robotLocation, Vector viewLocation, Vector &retObject)
Returns the absolute location of an object found around the reference view location.
int AngleAbsAdd(int absAngleA, int angle)
Adds angle to an absolute angle (0 .. 360) absAngleA. Returns an absolute angle properly truncated to...
int SonarGetCurrentViewAngle(Sonar &sonar, Vector &robotLocation)
Gets current absolute view angle in the world at given robot orientation.
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...
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...
void MotorSetPower(int motor, int power)
Set the power of given motor.
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. ...
int SonarGetMinViewAngle(Sonar &sonar, Vector &robotLocation)
Gets the absolute minimum view angle in the world at given robot orientation.
bool PIDControlCheckEnd(PIDControl &pidControl)
Checks if the PID controller has ended.
void VectorSubtract(Vector &a, Vector &b)
Subtracts vector b from a. i.e. a -= b.
bool SonarFindObjectAtAngle(Sonar &sonar, Vector robotLocation, int viewAngle, long viewDistance, Vector &retObject)
Returns the absolute location of an object found at given view angle.
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...
Generic PID controller implementation.
void MotorStop(int motor)
Stops the given motor.
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...
Motor setup caliberation utilities.
bool SonarTriangulateLandmarks(Sonar &sonar, Vector &robotLocation, Vector &landmark1, Vector &landmark2, Vector &retLocation)
Triangulates the position robot from two known landmarks.
int SonarGetMaxViewAngle(Sonar &sonar, Vector &robotLocation)
Gets the absolute maximum view angle in the world at given robot orientation.
int AngleAbsToRel(int absAngle)
Converts absolute angle (0 .. 360) to relative angle (-180 .. 180)
int AngleAbsSub(int absAngleA, int angle)
Subtracts angle from an absolute angle (0 .. 360) absAngleA. Returns an absolute angle properly trunc...
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.
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...
void VectorAdd(Vector &a, Vector &b)
Adds vector b to a. i.e. a += b.
Circle geometry operations.
int AngleRelToAbs(int relAngle)
Converts relative angle (-180 .. 180) to absolute angle (0 .. 360)
long VectorGetAngleVec(Vector &a, Vector &b)
Gets the angle of the line formed by the given two vectors.
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...
void VectorInit(Vector &a, long x, long y, long theta)
Initializes the vector with given x, y and theta components.