Задани на лабораторные работы. ПРК / Professional Microsoft Robotics Developer Studio
.pdf
www.it-ebooks.info
|
Chapter 17: Writing New Hardware Services |
11100, |
// 7 |
9200, |
// 8 |
7740, |
// 9 |
6500 |
// 10 |
};
///<summary>
///CalculateTimeToMove
///</summary>
///<param name=”distance”>Distance to travel</param>
///<param name=”power”>Drive power to use</param>
///<returns>Number of milliseconds</returns>
public int CalculateTimeToMove(double distance, double power)
{
//Drive distance is in meters, but we have to convert it to a
//time delay in milliseconds. The “magic” numbers here were found
//by trial and error using a 30cm drive distance. It will change
//as the batteries run down and due to a variety of other factors. int pwr = (int)(Math.Abs(power) * 10 + 0.5);
double delay = Math.Abs(distance) * TimeFactors[pwr]; return (int)(delay);
}
The results of timed motions will never be exactly correct, nor reproducible, due to variations in the timing on the TimeoutPort. Because it uses Windows timers, the TimeoutPort is only accurate to the nearest 15ms. In addition, the service is running on a multi-tasking operating system that was not designed for real-time use.
Other programs running on your PC might have control of the CPU when a timer fires, forcing your service to wait its turn. Therefore, it is advisable to close down all non-essential programs before you run an MRDS application.
Even if the timer were perfectly accurate, there is still a chance that a sensor poll might be in progress when it is time to stop the motors. As explained earlier, there must be a short pause between commands, so in general there can be a variation of up to 100ms in the total elapsed time due to serial port contention. At top speed, 100ms equates to several centimeters of travel.
The errors tend to average out, so over a series of motions the result will be close to correct. For example, if you make four 90-degree rotations, the robot will turn close to 360 degrees even though the individual rotations might not be 90 degrees.
Note that the Hemisson does not drive straight. The motor speeds are neither regulated nor matched, so your Hemisson might drift to the left or the right. The problem is worst at lower speeds.
Lastly, the Hemisson also implements the HttpPost operation for the Drive service. If you use a web browser, you can see that there are additional buttons at the top of the web page that are not present on the Integrator Drive page. The values of the left and right motor power are also in textboxes so that you can change them (see Figure 17-10).
793
www.it-ebooks.info
Part IV: Robotics Hardware
Figure 17-10
In Figure 17-10, the robot is executing a RotateDegrees operation. This is not implemented in the main Hemisson (Brick) service, but in the Drive service. You can move the code from the Drive service to the main service if you want. In that case, the Drive service would be almost trivial because the main service already implements the SetDrivePower operation. In effect, the Drive service would just be a shell that offered an operations port that complies with the generic Differential Drive contract. All the real work would be done in the main service.
If you view the code in the HttpPostHandler, you can see how it handles the different buttons at the top of the web form:
if (parameters[“Stop”] != null)
{
drive.AllStop req = new drive.AllStop(); _mainPort.Post(req); success.Body.WaitPort = req.ResponsePort;
}
else if (parameters[“EnableDrive”] != null)
{
drive.EnableDrive req = new drive.EnableDrive(); req.Body.Enable = true;
_mainPort.Post(req); success.Body.WaitPort = req.ResponsePort;
}
else if (parameters[“DisableDrive”] != null)
794
www.it-ebooks.info
Chapter 17: Writing New Hardware Services
{
drive.EnableDrive req = new drive.EnableDrive(); req.Body.Enable = false;
_mainPort.Post(req); success.Body.WaitPort = req.ResponsePort;
}
else if (parameters[“SetPower”] != null)
{
if (_state.IsEnabled)
{
if (!string.IsNullOrEmpty(parameters[“LeftPower”]))
{
Each of the submit buttons has a name. When you click one of them, a value is sent along with the other fields on the page. The other buttons are ignored, so you only need to check for the existence of a parameter with the name of a button to determine which button was pressed.
There is another important “trick” here — a deferred response. Notice that in the preceding code, the response port from the original message is placed into a success message as the WaitPort property. As the form is processed, error messages are appended to the ErrorMessage string. The last step in the handler is to check this string:
if (ErrorMessage == string.Empty)
{
//Post a message to finally issue the HTTP response
//IMPORTANT NOTE:
//If we simply redisplay the state now, it will be WRONG!
//It is quite likely that we have issued a request to an
//Exclusive handler in the code above. If we wait for a
//response, then we create a Deadlock!
//The solution is to post a message, which gives the other
//handler a chance to run, then wait for the response, and
//finally send the HTTP page back to the user. Got that? success.Body.Submit = submit;
//This is the ONLY place where these messages are sent
//Do NOT wait for a response!
_httpPostPort.Post(success);
}
else
HttpPostFailure(submit, ErrorMessage);
If there is an error, then a response can be posted back to the caller immediately. This is not a problem. However, if you want the refreshed page to contain the correct information, you must wait until another operation has completed. To avoid deadlocks, the current operation must finish and the other operation has to be given a chance to execute. This is done by posting a message to an internal port called _httpPostPort.
In the body of the HttpPost handler, a request has already been posted to one of the other drive operations: AllStop, EnableDrive, or SetDrivePower. This other request is already waiting in the
795
www.it-ebooks.info
Part IV: Robotics Hardware
queue when the HttpPostSuccess message is posted, so as soon as the HttpPost handler completes, the other operation will execute first. Then the HttpPostSuccessHandler is called:
///<summary>
///Send Http Post Success Handler
///</summary>
///Processes successful HttpPost messages by first waiting for the
///requested task to be performed, then sending back the web page
///NOTE: This is a complicated process, but it is necessary to avoid
///creating a Deadlock!
private IEnumerator<ITask> HttpPostSuccessHandler(HttpPostSuccess success)
{
Fault fail = null;
//Wait for the request to be processed
//Otherwise, the settings displayed on the web page are the
//OLD state settings, not the new ones!
if (success.Body.WaitPort != null)
{
yield return Arbiter.Choice( success.Body.WaitPort, delegate(DefaultUpdateResponseType resp) { }, delegate(Fault f) { fail = f; }
);
}
// Make sure that the requested task completed successfully if (fail == null)
{
// Post a HTTP response (finally!) HttpResponseType rsp =
new HttpResponseType(HttpStatusCode.OK, _state, _transform); success.Body.Submit.ResponsePort.Post(rsp);
}
else
{
// There is still a chance for this to fail! HttpPostFailure(success.Body.Submit, fail);
}
}
The success handler does the waiting, not the HttpPost handler, which has already completed execution. It does this using the WaitPort. Note that there is still a chance that the other operation might fail, in which case the “success” handler actually sends back a “fail” message (as a Fault). However, if all goes well, the web page is redisplayed using the XSLT file, just as it is for a HttpGet — but now it has the most up-to-date state information.
Note that in order for this approach to work, the success messages must participate in the main interleave. Therefore, the receiver is added to the main interleave in the Start method:
//Set up the Stop Motion handler which must be in the Exclusive group
//along with the other drive requests MainPortInterleave.CombineWith(
796
www.it-ebooks.info
Chapter 17: Writing New Hardware Services
new Interleave(
new ExclusiveReceiverGroup( Arbiter.ReceiveWithIterator<StopMotion>(true, 
_internalStopPort, StopMotionHandler) ),
new ConcurrentReceiverGroup( Arbiter.ReceiveWithIterator<HttpPostSuccess>(true, 
_httpPostPort, HttpPostSuccessHandler)
)
)
);
It doesn’t matter that the HttpPostSuccessHandler is Concurrent because it will never execute at the same time as an Exclusive handler.
Overall, the implementation of the Hemisson services demonstrates more functionality than the Integrator. The Hemisson can be driven around using the TeleOperation program while you watch the live video. The range of the Sena Bluetooth dongle is quite good. The only known problem is that sometimes the Hemisson does not connect when the service starts, but you can fix this by powering it off and on again and restarting the service.
Where to Go from Here
That’s the end of the book, but the fun does not stop here. New applications will continue to be posted on the book’s website. You can develop your own robotics applications and, it is hoped, share them with the MRDS community.
If we can believe the pundits, robotics is at a similar stage to the early development of the PC. Over the next few years we can expect dramatic improvements in the capabilities of robots in terms of physical agility, manual dexterity, object recognition, intelligence, and so on. The hobby robots and robotic toys of today will pale in comparison to the commercially available robots on the market in ten years.
One rapidly developing area is telepresence — similar to the TeleOperation service in Chapter 4. In late 2007, a whole raft of robots was announced that can be remotely controlled over the Internet with full video and audio capability — Spykee from Meccano, ConnectR from iRobot, and Rovio from WowWee Robotics. These should become available in 2008. This approach overcomes the artificial intelligence “roadblock” that researchers hit some 20 years ago, and it extends the concept of robotic toys to something useful in the home. Grandma will be able to “tele” in on the family robot and check whether you are keeping your room tidy.
With an aging population in many countries, such as Spain and Japan, placing “companion robots” into the homes of the elderly is one way to care for them. Research has shown that elderly people living in nursing homes become more active and responsive when they are given small, furry robotics pets, such as seals, pandas, and so on, to play with. Meanwhile, children’s toys are getting more intelligent and lifelike, such as Pleo the dinosaur. These robots, however, are just as much about human interaction as they are about robotics. More effort is required on the user interface than on the robotics.
797
www.it-ebooks.info
Part IV: Robotics Hardware
It’s a great time to be on the ground floor of a new industry, but don’t expect to be writing services to control differential drives in 2020. Will we see domestic robots powered by MRDS roaming around homes? Microsoft would like to think so. Only time will tell.
Summary
This chapter has shown you how to build a set of services for a completely new robot, albeit a simple one. From hardware and design to timed operations and polling, you have seen what is required to build and test a new robot.
Of course, despite the fact that it is over 800 pages long, this book still does not cover some areas of MRDS. You need to read the online documentation to fill in the gaps, and when V2.0 comes out you will probably have to start all over again. Welcome to the constantly evolving world of robotics.
Now go out and program your robots!
798
Symbols
( ) (parentheses), 481 @ (at sign), 101
/(division operator), 481
/(forward slash), 100, 607(hyphen), 100, 607
! (logical operator), 481 && (logical operator), 481 || (logical operator), 481(minus operator), 481 % (modulo operator), 481
* (multiplication operator), 481 + (plus operator), 481
A
abstract services. See generic contracts acceleration sensors, 591
Ackermann steering, 565 Action Input Pin, 478
Actions and Notifications dialog box, 489, 492 activate, 42
Activate method, 55, 56, 81 ActivateDsspOperationHandlers, 136 ActivationSettings, 41, 72, 74, 765, 768 ActiveSync, 687
ActiveSync Remote Display, 688, 690 activities (VPL), 8, 471. See also specific
activities
basic (list), 479–480 configuring, 508–513
modifying diagram for real robot, 512–513 setting configuration for diagram, 509–511 SimpleDashboard service, starting upon
execution, 511–512 custom, 487–493 inputs, 478–479 outputs, 478–479 services and, 501–502
Activity activity, 480 activity boxes, 473
ActivRobots, 584. See also Pioneer 3DX
www.it-ebooks.info
Index
Index
actuators, 585, 592–593 analog, 585
digital, 585
digital outputs, 592 generic contracts for, 585 motors, 592
sound/voice synthesis, 593
Add Bluetooth Device Wizard, 604–606 AGEIA PhysX engine. See PhysX engine Aldebaran Nao robot, 580, 734 Aldebaran Robotics, 580, 734
Alias Wavefront format, 249 aliasing, 240 AllStopHandler, 310
/alt qualifier, 98, 116, 216, 297, 763 Ambient color value, 251
analog actuator, 585 Analog Input (double), 738 Analog Output (double), 738 analog sensors, 585 AnalogSensor, 270 AnalogSensorArray, 270 Angular (member), 382 AngularDamping, 250, 260 anti-aliasing setting, 240
Anycom Blue USB-240 Adapter, 603
APIs (application programming interfaces), 84 operations as, 92
Append function, 494
application programming interfaces. See APIs Application tab, 114
applications, 84. See also services Approach mode, 453
Approach state, 355, 369–370 Arbiter.Activate, 42, 48, 55 Arbiter.CombineWith method, 137 Arbiter.FromHandler, 42, 69 Arbiter.FromIteratorHandler, 47, 68 Arbiter.Receive, 46, 47, 55 arbiters, 38
combining, 60–61 nested, 60–61 receivers and, 55–65
arithmetic operations, 481
www.it-ebooks.info
Arm Mover project (VPL)
Arm Mover project (VPL), 534–542
ArmMover activity, 535–536, 542 LynxL6Arm activity, 540, 541, 542 Lynxmotion L6 arm and, 542 moving the arm, 539–542
SetPollInterval action, 536–539, 542 Xbox controller and, 534, 535, 537
Arm Pose panel, 675, 676, 677 ArmMover activity, 535–536, 542 arms. See robotic arms
arrow keys, 181
Articulated Arm contract, generic, 668–670 articulated entities, 377–418. See also joints;
robotic arms
articulated robotic arms, 419, 565, 574, 655–665. See also legged robots; robotic arms
articulated robots, 10, 375, 467, 655, 656, 665 artificial intelligence ‘roadblock,’ 797
aspect ratio, 206 AssemblyInfo.cs, 278
asynchronous events, 30, 38, 44, 52, 90, 91.
See also CCR asynchronous PTP, 660 at sign (@), 101 Atmel, 732, 733
AttachedCameraEntity, 403, 438
autonomous robots, 10, 12, 13, 170, 265, 457, 572–573, 681–730. See also Stinger CE
development environment, 684 PC-based, 681–684
AvoidBoundary state, 425 AvoidCollision state, 355, 373–374 AVR brand, 733
Axim PDA, 686, 687 AxleSpeed, 296
B
6-BackAndForth project, 517–518 BackAway state, 356, 372–373 backslashes, 607. See also forward slash Ball Follower project, 554–558
BallFollower VPL diagram, 556–558 Simulation Environment, 554–556
BallCourt, 554, 555, 556 BallFollower VPL diagram, 556–558 barriers, 337, 341–342, 374 base.Initialize method, 288, 321, 325 base.Start, 136
baseVal, 405
BASIC, 471, 733, 754 advantages, 733 chips with built-in, 733
Basic Activities toolbox, 473, 479–480 basic input/output system (BIOS), 30 Basic Simulation Environment, 230
BASIC Stamp, 93, 94, 612–613, 642, 731, 732, 733, 735, 736
Battery (generic contract), 270 Bayes Rule, 466
beeping/flashing. See flashing/beeping Behavior method, 629, 638, 641
behavior states (Robo-Magellan simulation scenario), 354–374
behavior-based controllers, 594–596 BehaviorLoop, 356, 357, 367 behaviors, 85, 91–92, 625
reactive, 636
Belkin USB VCD (video capture device), 160, 205, 206
bin directory, 19 binormal axis, 371
BIOS (basic input/output system), 30.
See also RIOS
bipedal robots, 580, 655. See also humanoid robots bit banging, 755, 765
bitmaps, 81, 99, 194, 243, 462, 544 from byte array, 213
maze, 461, 462, 463, 464, 465 PictureBox and, 206, 213, 214, 215, 364 Simulation Environment and, 546 System.Drawing and, 161, 348
Blender, 318
Blind state, 425, 427 blocking I/O, 76 blocking threads, 46 bluesphere, 260 Bluetooth, 160, 163, 221
Add Bluetooth Device Wizard, 604–606 connection
Boe-Bot, 614–615
LEGO NXT, 602–606 device names, 605 discovery, 603
ZX-Bluetooth module, 754, 755
BMP. See bitmaps
Board Support Package (BSP), 720
Boe-Bot (Parallax), 8, 9, 93–94, 576–578. See also remotely connected robots
BASIC Stamp, 612–613, 642, 732, 735, 736 Bluetooth connection, 614–615
brick, 169 building, 611–612 bumpers, 167–169
communicating with, 615 flashing /beeping, 640–643
800
www.it-ebooks.info
Clear
monitor program, 613
Parallax services, 578, 642, 647, 651
Robotics with the Boe-Bot, 611–612 using, with MRDS, 611–615 wandering, 647–652
.bos format, 249 bounciness, 255, 261, 314 boxes, 238
giant, 280
BoxShapeProperties, 271 brain. See brick Braintech, 84 breadboarding, 612 breakpoints, 125, 126
Breakpoints (VPL debugger), 487
brick, 84, 93, 601, 735. See also Generic Brick contract; specific robots
brightness sensor, 547–548 Broadcom, 603 BSBumper.cs, 167, 168, 169 BSDrive service, 93, 94
BSP (Board Support Package), 720 BSServices, 122
Build Events tab, 114 Build tab, 114
built-in entities, 252–267 bump notifications, 651–652 bump sensors, 643, 748 bumper array, 586
Bumper service, 169 BumperArrayEntity, 265, 273 BumperPressed function, 790 bumpers, 586, 789. See also contact
sensor array
Boe-Bot, 167–169 virtual, 586
Busy variable, 526, 528 button handling, 184–185 byte array, 213, 767
C
C++, 471, 476, 720 templates, 45
CAD packages, 318
Calculate activity, 479, 481, 483, 484 arithmetic operations, 481
logical operations, 481 callback procedures, 55 camera entity, 402–403
camera frames, processing. See vision processing algorithm
Camera menu, 235–236
CameraEntity, 266, 272, 322, 403, 547, 548, 550. See also AttachedCameraEntity
cameras, 590. See also Main Camera; webcams for Corobot entity, 322–323, 340
hexapod project, 438
for Lynxmotion L6 robotic arm, 679 non-realtime, 236
realtime, 235–236 capsules, 238 CapsuleShapeProperties, 271
Cascading Style Sheet (CSS) files, 193, 194, 195, 199
case sensitivity commands/parameters, 100 contract identifiers, 148–149 JavaScript, 205
URI, 86
XML, 188, 193 XSLT and, 191
CastsShadow property, 259 causalities, 5, 39, 77–79, 91
CCR (Concurrency and Coordination Runtime), 5, 29–82
applications. See services architecture, 38
DSS v., 29 online forum, 27
user guide, 27, 29
Ccr.Adapters.WinForms, 161, 352, 353, 691, 727 CCRExamples project, 33–34
CenterOfMass, 260
central intelligence. See brick
CF (Compact Framework) services, 54, 154, 171, 219, 572, 578, 680, 749
deploying to PDA, 709–711 StingerDriveByWire service, 708–713
CF (CompactFlash) slot, 723 Channel 9, 28
chassis, 284, 285, 286 checkform, 204, 205 Child, 69
child entities, 248, 288 chips. See microcontrollers Choice, 38, 44, 57–59
Chrysanthakopoulos, George, 73
Class Reference (MRDS), 37, 221 classes, 37
generic, 45
inheritance, 98, 215. See also generic contracts Intellisense and, 37
Object Browser and, 37 online reference, 37
Clear, 51
Index
801
www.it-ebooks.info
cliff sensors
cliff sensors, 532 /clone qualifier, 116
closed-loop control system, 585, 593–594 CLR environment, 38, 39, 45, 50, 54, 65, 66,
73, 76, 243
CML (concurrent mapping and localization), 636.
See also SLAM
code, sample. See sample code coding tips, 37
color intensity (value), 450, 452, 463 color space
HSV, 450, 452
RGB, 450, 452, 462, 463, 590
ColorValue, 270
COM ports, 87, 132, 570, 574, 605, 607, 608, 609, 614, 615, 623, 707, 714
combined rendering mode, 239 combining arbiters, 60–61 command overload, 776–779 CommandArguments, 543, 544 commands (MRDS), 100–101 Comment activity, 479, 480 common directory, 19 Community forum, 27
Community Technology Preview. See CTP Compact Framework environment. See CF services CompactFlash cards, 683, 723
CompactFlash (CF) slot, 723 companion robots, 797 compass sensors, 590–591
compiling diagrams to services, 502–508 service compilation example, 503–508 service compilation options, 502–503
compiling services, 121–123 completion ports, 68, 69, 139–141 Concatenate function, 494 concurrency, 30–31, 39
Concurrency and Coordination Runtime. See CCR “Concurrent Affairs,” 81
concurrent mapping and localization (CML), 636.
See also SLAM
Concurrent receiver group, 64, 92 conditionals (VPL), 482–484 cones. See traffic cones
config directory, 19 config file, 89 ConfigureBrick, 748 ConfigureDevices, 749
Connect button, 171, 207, 264, 443, 608 ConnectDrive, 207
connection requests, 207–209 Connectors, 382
ConnectR, 797
ConnectWebCam, 207, 210 Conscious Robots website, 467 Console Output, 99, 106, 629
Console.WriteLine, 33, 98, 220. See also LogError; LogInfo; LogVerbose; LogWarning
Constructor service, 99, 100
contact sensor array, 168, 647, 648, 789
contact sensors, 586. See also bumpers; whiskers Contact state, 425, 427
ContactSensor, 270 ContactSensorUpdate, 509 continuations, 44, 55 Continuous option, 422 Contract Directory, 106–107 contract directory cache, 124 contract ID, 208, 265 contract identifiers, 85, 86–87
case sensitive, 148–149
ContractPrefix, 503 contracts, 85, 86–87
generic. See generic contracts information on. See DssInfo
control loop, 585. See also closed-loop control system Control Panel, 99, 102–103
manually start services with, 102–103, 205 Motion Recorder, 675
control structures, 68 controllers, 584, 593–596
behavior-based, 594–596 closed-loop control system, 593–594
PID, 216, 585, 593, 594, 596, 636, 730 controllers (game). See game controllers controlling multiple robots, 520–522 ConvertFile method, 544
convex mesh, 262 ConvexMeshShapeProperties, 271 Coordinations, 699
CoreCon component, 720
Corobot entity, 7, 11, 269, 276. See also Corobot robot; Robo-Magellan simulation scenario; soccer environment
appearance, enhancing of, 318–321 Approach state, 355, 369–370 AvoidCollision state, 355, 373–374 BackAway state, 356, 372–373 behavior states, 354–374
cameras for, 322–323, 340 chassis, 284, 285
Corobot service, 277–281, 336 defining, 281–293
Drive methods, 293–296 DriveDistance, 312
testing, 312–313
802
