wd.c 29.2 KB
Newer Older
pixhawk's avatar
pixhawk committed
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
/*****************************************************************************
 *  Copyright (c) 2008, University of Florida.
 *  All rights reserved.
 *  
 *  This file is part of OpenJAUS.  OpenJAUS is distributed under the BSD 
 *  license.  See the LICENSE file for details.
 * 
 *  Redistribution and use in source and binary forms, with or without 
 *  modification, are permitted provided that the following conditions 
 *  are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of the University of Florida nor the names of its 
 *       contributors may be used to endorse or promote products derived from 
 *       this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
// File:		wd.c 
//
// Version:		1.0
//
// Written by:	Danny Kent (jaus AT dannykent DOT com)
//
// Date:		08/24/06
//
// Description:	This file contains the skeleton C code for implementing a JAUS component in a Linux environment
//				This code is designed to work with the node manager and JAUS library software written by CIMAR

#include <jaus.h>			// JAUS message set (USER: JAUS libraries must be installed first)
#include <openJaus.h>	// Node managment functions for sending and receiving JAUS messages (USER: Node Manager must be installed)
#include <stdlib.h>	
#include <string.h>
#include "utm/utmLib.h"
// USER: Add include files here as appropriate

#include "wd.h"
#include "vehicle.h"

#if defined (WIN32)
	#define _USE_MATH_DEFINES
	#include <math.h>
#endif

// Incoming Service Connection Defines
#define WD_INC_SC_TIMEOUT_SEC 			1.0		// The timeout between receiving service connection messages
#define WD_INC_SC_UPDATE_RATE_HZ 		20.0	// Requested service connection update rate
#define WD_INC_SC_PRESENCE_VECTOR 		0xFF	// The GPOS Presence Vector, set this to the fields desired (default = ALL)
#define WD_INC_SC_QUEUE_SIZE			1		// The Service Connection Manager's queue size (0 = infinite)

#define MPH_PER_MPS						2.23694
#define MPS_PER_MPH						0.44704
//#define ACCEL_GRAVITY_MPSPS				9.81	// Acceleration due to gravity

#define REQUEST_TIMEOUT_SEC				5.0 	// How long to wait between any requests

#define WAYPOINT_POP_DISTANCE_M						3.0
#define MISSION_FINISHED_DISTANCE_M					3.0
#define WD_DEFAULT_MIN_SPEED_MPS					1.0
#define WD_DEFAULT_MAX_SPEED_MPS					4.0 //11.18

// Gains
#define ACCELERATION_MPSPS				0.75	// Mps2
#define DECCELERATION_MPSPS				2.0		// Mps2
#define LINEAR_EFFORT_K_FF				14.0 	// %Effort / Mps
#define LINEAR_EFFORT_BIAS_FF  			-57.5	// %Effort
#define LINEAR_EFFORT_K_P				15.0 	// %Effort / (Mps Error)
#define LINEAR_EFFORT_K_I				6.5 	// %Effort / (M Error)
#define LINEAR_EFFORT_I_LIM				6.5 	// M Error
#define LINEAR_EFFORT_K_D				20.0
#define BRAKE_K							-1.3	// %Effort / %Effort
#define THROTTLE_K						0.65	// %Effort / %Effort
#define STICKTION_SPEED_MPS				0.5
#define STICKTION_EFFORT				-17.5
//#define PITCH_FF_EFFORT_PER_RAD			150
#define STEERING_FF_EFFORT				200
#define WHEEL_ROTATION_EFFORT_PER_RAD	100	
#define WHEEL_ROTATION_RAD_PER_EFFORT	0.006

// Private function prototypes
void wdProcessMessage(OjCmpt wd, JausMessage message);
void wdInitState(OjCmpt wd);
void wdStandbyState(OjCmpt wd);
void wdReadyState(OjCmpt wd);

// USER: Insert any private function prototypes here
void wdManagePdControl(OjCmpt wd);
void wdExcecuteControl(OjCmpt wd, VehicleState);
double wdAngleSubtract(double a, double b);

typedef struct
{
	ReportWrenchEffortMessage reportWrench;
	ReportGlobalPoseMessage reportGpos;
	ReportVelocityStateMessage reportVss;
	SetWrenchEffortMessage setWrench;
	SetTravelSpeedMessage setSpeed;
	SetGlobalWaypointMessage setWaypoint;

	int gposSc;
	int vssSc;
	int pdWrenchSc;
	int pdStatusSc;

	VehicleState vehicleState;

	JausState pdState;
	JausAddress pdAddress;
	
	JausBoolean inControl;
	JausBoolean requestControl;
	
	JausArray waypoints;					// Array of commaned waypoints
	int currentWaypoint;
	double waypointDistance;

}WdData;

OjCmpt wdCreate(void)
{
	OjCmpt cmpt;
	WdData *data;
	JausAddress addr;
	
	cmpt = ojCmptCreate("Waypoint Driver", JAUS_GLOBAL_WAYPOINT_DRIVER, WD_THREAD_DESIRED_RATE_HZ);

	ojCmptAddService(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER);

	ojCmptAddServiceInputMessage(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER, JAUS_SET_TRAVEL_SPEED, 0xFF);
	ojCmptAddServiceInputMessage(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER, JAUS_REPORT_GLOBAL_POSE, 0xFF);
	ojCmptAddServiceInputMessage(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER, JAUS_REPORT_VELOCITY_STATE, 0xFF);
	ojCmptAddServiceInputMessage(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER, JAUS_REPORT_WRENCH_EFFORT, 0xFF);
	ojCmptAddServiceInputMessage(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER, JAUS_SET_GLOBAL_WAYPOINT, 0xFF);
	ojCmptAddServiceInputMessage(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER, JAUS_QUERY_GLOBAL_WAYPOINT, 0xFF);
	ojCmptAddServiceInputMessage(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER, JAUS_QUERY_WAYPOINT_COUNT, 0xFF);
	ojCmptAddServiceOutputMessage(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER, JAUS_REPORT_WAYPOINT_COUNT, 0xFF);
	ojCmptAddServiceOutputMessage(cmpt, JAUS_GLOBAL_WAYPOINT_DRIVER, JAUS_REPORT_GLOBAL_WAYPOINT, 0xFF);

	ojCmptSetMessageProcessorCallback(cmpt, wdProcessMessage);
	ojCmptSetStateCallback(cmpt, JAUS_INITIALIZE_STATE, wdInitState);
	ojCmptSetStateCallback(cmpt, JAUS_STANDBY_STATE, wdStandbyState);
	ojCmptSetStateCallback(cmpt, JAUS_READY_STATE, wdReadyState);
	ojCmptSetState(cmpt, JAUS_INITIALIZE_STATE);
	
	ojCmptSetAuthority(cmpt, 127);
	
	addr = ojCmptGetAddress(cmpt);
	
	data = (WdData*)malloc(sizeof(WdData));
	
	data->setWrench = setWrenchEffortMessageCreate();
	jausAddressCopy(data->setWrench->source, addr);
	data->setWrench->presenceVector = 0;
	jausUnsignedShortSetBit(&data->setWrench->presenceVector, JAUS_WRENCH_PV_PROPULSIVE_LINEAR_X_BIT);
	jausUnsignedShortSetBit(&data->setWrench->presenceVector, JAUS_WRENCH_PV_PROPULSIVE_ROTATIONAL_Z_BIT);
	jausUnsignedShortSetBit(&data->setWrench->presenceVector, JAUS_WRENCH_PV_RESISTIVE_LINEAR_X_BIT);
	
	data->reportWrench = reportWrenchEffortMessageCreate();
	data->reportGpos = reportGlobalPoseMessageCreate();
	data->reportVss = reportVelocityStateMessageCreate();
	data->setSpeed = setTravelSpeedMessageCreate();
	data->setWaypoint = NULL;

	data->pdAddress = jausAddressCreate();
	data->pdAddress->subsystem = addr->subsystem;
	data->pdAddress->component = JAUS_PRIMITIVE_DRIVER;
	data->pdState = JAUS_UNDEFINED_STATE;
	
	data->inControl = JAUS_FALSE;
	data->requestControl = JAUS_FALSE;

	addr->subsystem = 0;
	addr->node = 0;
	addr->instance = 0;
	addr->component = JAUS_GLOBAL_POSE_SENSOR;
	data->gposSc = ojCmptEstablishSc(	cmpt, 
										JAUS_REPORT_GLOBAL_POSE,
										WD_INC_SC_PRESENCE_VECTOR, 
										addr, 
										WD_INC_SC_UPDATE_RATE_HZ, 
										WD_INC_SC_TIMEOUT_SEC, 
										WD_INC_SC_QUEUE_SIZE);
	addr->component = JAUS_VELOCITY_STATE_SENSOR;
	data->vssSc = ojCmptEstablishSc(	cmpt, 
										JAUS_REPORT_VELOCITY_STATE,
										WD_INC_SC_PRESENCE_VECTOR, 
										addr, 
										WD_INC_SC_UPDATE_RATE_HZ, 
										WD_INC_SC_TIMEOUT_SEC, 
										WD_INC_SC_QUEUE_SIZE);
	addr->component = JAUS_PRIMITIVE_DRIVER;
	data->pdWrenchSc = ojCmptEstablishSc(	cmpt, 
											JAUS_REPORT_WRENCH_EFFORT,
											WD_INC_SC_PRESENCE_VECTOR, 
											addr, 
											WD_INC_SC_UPDATE_RATE_HZ, 
											WD_INC_SC_TIMEOUT_SEC, 
											WD_INC_SC_QUEUE_SIZE);
	data->pdStatusSc = ojCmptEstablishSc(	cmpt, 
											JAUS_REPORT_COMPONENT_STATUS,
											WD_INC_SC_PRESENCE_VECTOR, 
											addr, 
											WD_INC_SC_UPDATE_RATE_HZ, 
											WD_INC_SC_TIMEOUT_SEC, 
											WD_INC_SC_QUEUE_SIZE);
	jausAddressDestroy(addr);	
	
	data->waypoints = jausArrayCreate();	
	data->vehicleState = vehicleStateCreate();
	data->waypointDistance = 0;
	data->currentWaypoint = 0;
	
	ojCmptSetUserData(cmpt, (void *)data);
		
	if(ojCmptRun(cmpt))
	{
		ojCmptDestroy(cmpt);
		return NULL;
	}

	return cmpt;
}

void wdDestroy(OjCmpt wd)
{
	ReleaseComponentControlMessage releaseControl = NULL;
	JausMessage txMessage;
	JausAddress address;
	WdData *data;
	
	data = (WdData*)ojCmptGetUserData(wd);

	if(ojCmptIsIncomingScActive(wd, data->gposSc))
	{
		ojCmptTerminateSc(wd, data->gposSc);
	}
	if(ojCmptIsIncomingScActive(wd, data->vssSc))
	{
		ojCmptTerminateSc(wd, data->vssSc);
	}
	if(ojCmptIsIncomingScActive(wd, data->pdWrenchSc))
	{
		ojCmptTerminateSc(wd, data->pdWrenchSc);
	}
	if(ojCmptIsIncomingScActive(wd, data->pdStatusSc))
	{
		ojCmptTerminateSc(wd, data->pdStatusSc);
	}

	if(data->inControl)
	{
		releaseControl = releaseComponentControlMessageCreate();
		address = ojCmptGetAddress(wd);
		jausAddressCopy(releaseControl->source, address);
		jausAddressDestroy(address);
		jausAddressCopy(releaseControl->destination, data->pdAddress);
		
		txMessage = releaseComponentControlMessageToJausMessage(releaseControl);
		ojCmptSendMessage(wd, txMessage);
		jausMessageDestroy(txMessage);
		
		releaseComponentControlMessageDestroy(releaseControl);
	}
	
	ojCmptDestroy(wd);

	// Destory Global Messages
	if(data->setSpeed)
	{
		setTravelSpeedMessageDestroy(data->setSpeed);
	}
	if(data->reportGpos)
	{
		reportGlobalPoseMessageDestroy(data->reportGpos);
	}
	if(data->reportVss)
	{
		reportVelocityStateMessageDestroy(data->reportVss);
	}
	if(data->reportWrench)
	{
		reportWrenchEffortMessageDestroy(data->reportWrench);
	}
	jausArrayDestroy(data->waypoints, (void *)setGlobalWaypointMessageDestroy);
	setWrenchEffortMessageDestroy(data->setWrench);
	jausAddressDestroy(data->pdAddress);
	vehicleStateDestroy(data->vehicleState);	
	free(data);
}

ReportWrenchEffortMessage wdGetReportedWrench(OjCmpt wd)
{ 
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);
	
	if(ojCmptIsIncomingScActive(wd, data->pdWrenchSc))
	{
		return data->reportWrench; 
	}
	else
	{
		return NULL;
	}
}

SetWrenchEffortMessage wdGetCommandedWrench(OjCmpt wd)
{ 
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->setWrench; 
}

ReportVelocityStateMessage wdGetVss(OjCmpt wd)
{ 
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->reportVss; 
}

ReportGlobalPoseMessage wdGetGpos(OjCmpt wd)
{ 
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->reportGpos; 
}

SetTravelSpeedMessage wdGetTravelSpeed(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->setSpeed;
}

SetGlobalWaypointMessage wdGetGlobalWaypoint(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	if(data->currentWaypoint < data->waypoints->elementCount)
	{
		return (SetGlobalWaypointMessage) data->waypoints->elementData[data->currentWaypoint];
	}
	else
	{
		return NULL;
	} 
}

JausState wdGetPdState(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->pdState;
}

JausBoolean wdGetPdWrenchScStatus(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return ojCmptIsIncomingScActive(wd, data->pdWrenchSc);
}

JausBoolean wdGetPdStatusScStatus(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return ojCmptIsIncomingScActive(wd, data->pdStatusSc);
}

JausBoolean wdGetVssScStatus(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return ojCmptIsIncomingScActive(wd, data->vssSc);
}

JausBoolean wdGetGposScStatus(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return ojCmptIsIncomingScActive(wd, data->gposSc);
}

JausBoolean wdGetInControlStatus(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->inControl;
}

VehicleState wdGetDesiredVehicleState(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->vehicleState;
}

double wdGetWaypointDistanceM(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->waypointDistance;
}

int wdGetActiveWaypoint(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->currentWaypoint;
}

int wdGetWaypointCount(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->waypoints->elementCount;
}

void wdToggleRequestControl(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	data->requestControl = !data->requestControl;
}

JausBoolean wdGetRequestControl(OjCmpt wd)
{
	WdData *data;
	data = (WdData*)ojCmptGetUserData(wd);

	return data->requestControl;
}

void wdCreateWaypoint(OjCmpt wd)
{
	SetGlobalWaypointMessage setWaypoint = NULL;
	JausMessage message = NULL;
	
	setWaypoint = setGlobalWaypointMessageCreate();
	setWaypoint->latitudeDegrees = 29.001;
	setWaypoint->longitudeDegrees = -80.001;
	
	message = setGlobalWaypointMessageToJausMessage(setWaypoint);
	setGlobalWaypointMessageDestroy(setWaypoint);
	wdProcessMessage(wd, message);	
}

void wdSetSpeed(OjCmpt wd)
{
	SetTravelSpeedMessage setSpeed = NULL;
	JausMessage message = NULL;
	
	setSpeed = setTravelSpeedMessageCreate();
	setSpeed->speedMps = 4.0;
	
	message = setTravelSpeedMessageToJausMessage(setSpeed);
	setTravelSpeedMessageDestroy(setSpeed);
	wdProcessMessage(wd, message);
}

// Function: wdProcessMessage
// Access:		Private
// Description:	This function is responsible for handling incoming JAUS messages from the Node Manager.
//				Incoming messages are processed according to message type.
void wdProcessMessage(OjCmpt wd, JausMessage message)
{
	JausMessage txMessage;
	JausAddress address;
	ConfirmComponentControlMessage confirmComponentControl;
	RejectComponentControlMessage rejectComponentControl;
	ReportComponentStatusMessage reportComponentStatus;
	QueryGlobalWaypointMessage queryGlobalWaypointMessage;
	ReportGlobalWaypointMessage reportGlobalWaypointMessage;
	QueryWaypointCountMessage queryWaypointCountMessage;
	ReportWaypointCountMessage reportWaypointCountMessage;
	SetTravelSpeedMessage setTravelSpeed;
	ReportGlobalPoseMessage reportGpos;
	ReportVelocityStateMessage reportVss;
	ReportWrenchEffortMessage reportWrench;	
	SetGlobalWaypointMessage tempGlobalWaypoint;
	int i;
	WdData *data;
	
	data = (WdData*)ojCmptGetUserData(wd);

	// This block of code is intended to reject commands from non-controlling components
	if(ojCmptHasController(wd) && jausMessageIsRejectableCommand(message) )
	{
		address = ojCmptGetControllerAddress(wd);
		if(!jausAddressEqual(message->source, address))
		{		
			//jausAddressToString(message->source, buf);
			//cError("pd: Received command message %s from non-controlling component (%s).\n", jausMessageCommandCodeString(message), buf);
			jausAddressDestroy(address);
			return;
		}
		jausAddressDestroy(address);
	}	

	switch(message->commandCode) // Switch the processing algorithm according to the JAUS message type
	{	
		case JAUS_REPORT_COMPONENT_STATUS:
			reportComponentStatus = reportComponentStatusMessageFromJausMessage(message);
			if(reportComponentStatus)
			{
				if(jausAddressEqual(reportComponentStatus->source, data->pdAddress))
				{
					data->pdState = reportComponentStatus->primaryStatusCode;
				}
				reportComponentStatusMessageDestroy(reportComponentStatus);
			}
			break;

		case JAUS_CONFIRM_COMPONENT_CONTROL:
			confirmComponentControl = confirmComponentControlMessageFromJausMessage(message);
			if(confirmComponentControl)
			{
				if(jausAddressEqual(confirmComponentControl->source, data->pdAddress))
				{
					//cDebug(4,"wd: Confirmed control of PD\n");
					data->inControl = JAUS_TRUE;
				}
				confirmComponentControlMessageDestroy(confirmComponentControl);
			}
			break;
			
		case JAUS_REJECT_COMPONENT_CONTROL:
			rejectComponentControl = rejectComponentControlMessageFromJausMessage(message);
			if(rejectComponentControl)
			{
				if(jausAddressEqual(rejectComponentControl->source, data->pdAddress))
				{			
					//cDebug(4,"wd: Lost control of PD\n");
					data->inControl = JAUS_FALSE;
				}
				rejectComponentControlMessageDestroy(rejectComponentControl);
			}
			break;
			
		case JAUS_SET_TRAVEL_SPEED:
			setTravelSpeed = setTravelSpeedMessageFromJausMessage(message);
			if(setTravelSpeed)
			{
				setTravelSpeedMessageDestroy(data->setSpeed);
				data->setSpeed = setTravelSpeed;
			}
			break;

		case JAUS_REPORT_GLOBAL_POSE:
			reportGpos = reportGlobalPoseMessageFromJausMessage(message);
			if(reportGpos)
			{
				reportGlobalPoseMessageDestroy(data->reportGpos);
				data->reportGpos = reportGpos;
			}

			if(data->currentWaypoint < data->waypoints->elementCount)
			{
				// update waypoint index
				tempGlobalWaypoint = (SetGlobalWaypointMessage) data->waypoints->elementData[data->currentWaypoint];
				data->waypointDistance = greatCircleDistance(	data->reportGpos->latitudeDegrees * RAD_PER_DEG,
																data->reportGpos->longitudeDegrees * RAD_PER_DEG,
																tempGlobalWaypoint->latitudeDegrees * RAD_PER_DEG,
																tempGlobalWaypoint->longitudeDegrees * RAD_PER_DEG);
								
				if(data->waypointDistance < WAYPOINT_POP_DISTANCE_M)
				{
					//cError("wd: popping waypoint: %d\n",currentWaypointIndex); 
					data->currentWaypoint++;
				}
			}
			break;

		case JAUS_REPORT_VELOCITY_STATE:
			reportVss = reportVelocityStateMessageFromJausMessage(message);
			if(reportVss)
			{
				reportVelocityStateMessageDestroy(data->reportVss);
				data->reportVss = reportVss;
			}
			break;

		case JAUS_REPORT_WRENCH_EFFORT:
			reportWrench = reportWrenchEffortMessageFromJausMessage(message);
			if(reportWrench)
			{
				reportWrenchEffortMessageDestroy(data->reportWrench);
				data->reportWrench = reportWrench;
			}
			break;
			
		case JAUS_SET_GLOBAL_WAYPOINT:
			tempGlobalWaypoint = setGlobalWaypointMessageFromJausMessage(message);
			if(tempGlobalWaypoint)
			{
				data->setWaypoint = tempGlobalWaypoint;
				jausArrayAdd(data->waypoints, tempGlobalWaypoint);
			}
			break;

		case JAUS_QUERY_GLOBAL_WAYPOINT:
			queryGlobalWaypointMessage = queryGlobalWaypointMessageFromJausMessage(message);
			// loop thru waypoints to find the one that matches the request
			// if there's a match, prepare/send the report, else whine
			for(i = 0; i < data->waypoints->elementCount; i++)
			{
				tempGlobalWaypoint = (SetGlobalWaypointMessage) data->waypoints->elementData[i];
				if(tempGlobalWaypoint->waypointNumber == queryGlobalWaypointMessage->waypointNumber)
				{
					reportGlobalWaypointMessage = reportGlobalWaypointMessageCreate();
					jausAddressCopy(reportGlobalWaypointMessage->destination, queryGlobalWaypointMessage->source);
					address = ojCmptGetAddress(wd);
					jausAddressCopy(reportGlobalWaypointMessage->source, address);
					jausAddressDestroy(address);
					reportGlobalWaypointMessage->presenceVector = NO_PRESENCE_VECTOR;
					reportGlobalWaypointMessage->waypointNumber = tempGlobalWaypoint->waypointNumber;
					reportGlobalWaypointMessage->latitudeDegrees = tempGlobalWaypoint->latitudeDegrees;
					reportGlobalWaypointMessage->longitudeDegrees = tempGlobalWaypoint->longitudeDegrees;
					txMessage = reportGlobalWaypointMessageToJausMessage(reportGlobalWaypointMessage);
					reportGlobalWaypointMessageDestroy(reportGlobalWaypointMessage);
					ojCmptSendMessage(wd, txMessage);
					jausMessageDestroy(txMessage);
				}
			}
			queryGlobalWaypointMessageDestroy(queryGlobalWaypointMessage);
			break;
			
		case JAUS_QUERY_WAYPOINT_COUNT:
			queryWaypointCountMessage = queryWaypointCountMessageFromJausMessage(message);
			if(!queryWaypointCountMessage)
			{
				//cError("wd: Error unpacking %s message.\n", jausMessageCommandCodeString(message));
			}
			reportWaypointCountMessage = reportWaypointCountMessageCreate();
			jausAddressCopy(reportWaypointCountMessage->destination, queryWaypointCountMessage->source);
			address = ojCmptGetAddress(wd);
			jausAddressCopy(reportWaypointCountMessage->source, address);
			jausAddressDestroy(address);
			reportWaypointCountMessage->waypointCount = data->waypoints->elementCount;
			txMessage = reportWaypointCountMessageToJausMessage(reportWaypointCountMessage);
			reportWaypointCountMessageDestroy(reportWaypointCountMessage);
			ojCmptSendMessage(wd, txMessage);
			queryWaypointCountMessageDestroy(queryWaypointCountMessage);
			break;
		
		default:
			ojCmptDefaultMessageProcessor(wd, message);
			break;
	}
}

void wdInitState(OjCmpt wd)
{
	WdData *data;
	
	data = (WdData*)ojCmptGetUserData(wd);
		
	if(data->pdAddress->node == 0)
	{
		if(ojCmptLookupAddress(wd, data->pdAddress))
		{
			jausAddressCopy(data->setWrench->destination, data->pdAddress);
		}
	}

	wdManagePdControl(wd);

	// Check for critcal service connections or conditions here
	if(	ojCmptIsIncomingScActive(wd, data->gposSc) &&
		ojCmptIsIncomingScActive(wd, data->vssSc) &&
		ojCmptIsIncomingScActive(wd, data->pdWrenchSc) &&
		ojCmptIsIncomingScActive(wd, data->pdStatusSc) &&
		data->inControl)
	{
		// Transition to Standby
		ojCmptSetState(wd, JAUS_STANDBY_STATE);
		//cDebug(4, "wd: Switching to STANDBY State\n");
	}
}

void wdStandbyState(OjCmpt wd)
{
	WdData *data;
	
	data = (WdData*)ojCmptGetUserData(wd);

	if(	!ojCmptIsIncomingScActive(wd, data->gposSc) ||
		!ojCmptIsIncomingScActive(wd, data->vssSc) ||
		!ojCmptIsIncomingScActive(wd, data->pdWrenchSc) ||
		!ojCmptIsIncomingScActive(wd, data->pdStatusSc) ||
		!data->inControl)
	{
		ojCmptSetState(wd, JAUS_INITIALIZE_STATE);
		return;
	}
		
	wdManagePdControl(wd);

	// Setup Vehicle State		
	data->vehicleState->desiredSpeedMps = 0;
	data->vehicleState->desiredPhiEffort = 0;
	wdExcecuteControl(wd, data->vehicleState);
	
	if(data->pdState == JAUS_READY_STATE)
	{
		ojCmptSetState(wd, JAUS_READY_STATE);
	}	
}

void wdReadyState(OjCmpt wd)
{
	SetGlobalWaypointMessage tempGlobalWaypoint;
	double waypointHeading = 0;
	double headingDelta = 0;
	WdData *data;
	
	data = (WdData*)ojCmptGetUserData(wd);

	if(	!ojCmptIsIncomingScActive(wd, data->gposSc) ||
		!ojCmptIsIncomingScActive(wd, data->vssSc) ||
		!ojCmptIsIncomingScActive(wd, data->pdWrenchSc) ||
		!ojCmptIsIncomingScActive(wd, data->pdStatusSc) ||
		!data->inControl)
	{
		ojCmptSetState(wd, JAUS_INITIALIZE_STATE);
		return;
	}
	
	if(data->pdState != JAUS_READY_STATE)
	{
		ojCmptSetState(wd, JAUS_STANDBY_STATE);
		return;
	}	
	
	wdManagePdControl(wd);

	data->vehicleState->speedMps = data->reportVss? (float)data->reportVss->velocityXMps : 0.0f;

	if(data->currentWaypoint < data->waypoints->elementCount)
	{
		tempGlobalWaypoint = (SetGlobalWaypointMessage) data->waypoints->elementData[data->currentWaypoint];

		data->waypointDistance = greatCircleDistance(	data->reportGpos->latitudeDegrees * RAD_PER_DEG,
														data->reportGpos->longitudeDegrees * RAD_PER_DEG,
														tempGlobalWaypoint->latitudeDegrees * RAD_PER_DEG,
														tempGlobalWaypoint->longitudeDegrees * RAD_PER_DEG);
		waypointHeading = greatCircleCourse(data->reportGpos->latitudeDegrees * RAD_PER_DEG,
											data->reportGpos->longitudeDegrees * RAD_PER_DEG,
											tempGlobalWaypoint->latitudeDegrees * RAD_PER_DEG,
											tempGlobalWaypoint->longitudeDegrees * RAD_PER_DEG);
		headingDelta = wdAngleSubtract(waypointHeading, data->reportGpos->yawRadians);
		
		data->vehicleState->desiredPhiEffort = (float)(WHEEL_ROTATION_EFFORT_PER_RAD * headingDelta);
		data->vehicleState->desiredSpeedMps = data->setSpeed? (float)data->setSpeed->speedMps : 0.0f;
		//cLog("HeadingDelta: %7.2f\n", headingDelta);
		//cLog("vehicleState->desiredPhiEffort: %7.2f\n", data->vehicleState->desiredPhiEffort);		
	}
	else
	{
		//hang out
		data->vehicleState->desiredSpeedMps = 0;
		data->vehicleState->desiredPhiEffort = 0;
	}
	
	wdExcecuteControl(wd, data->vehicleState);
}

void wdManagePdControl(OjCmpt wd)
{
	JausAddress address;
	JausMessage message = NULL;
	ReleaseComponentControlMessage releaseControl = NULL;
	RequestComponentControlMessage requestControl = NULL;
	WdData *data;
	
	data = (WdData*)ojCmptGetUserData(wd);
	
	// Check Control
	if(data->requestControl)
	{
		if(!data->inControl && data->pdAddress->node)
		{
			//jausAddressToString(pd->address, buf);
			//cDebug(4, "wd: Requesting control of PD %s\n", buf);
	
			requestControl = requestComponentControlMessageCreate();
			address = ojCmptGetAddress(wd);
			jausAddressCopy(requestControl->source, address);
			jausAddressDestroy(address);
			jausAddressCopy(requestControl->destination, data->pdAddress);
			requestControl->authorityCode = ojCmptGetAuthority(wd);
			
			message = requestComponentControlMessageToJausMessage(requestControl);
			ojCmptSendMessage(wd, message);
			jausMessageDestroy(message);
			
			requestComponentControlMessageDestroy(requestControl);
		}
	}
	else
	{
		if(data->inControl)
		{
			// Release Control
			releaseControl = releaseComponentControlMessageCreate();
			address = ojCmptGetAddress(wd);
			jausAddressCopy(releaseControl->source, address);
			jausAddressDestroy(address);
			jausAddressCopy(releaseControl->destination, data->pdAddress);
			
			message = releaseComponentControlMessageToJausMessage(releaseControl);
			ojCmptSendMessage(wd, message);
			jausMessageDestroy(message);
			
			releaseComponentControlMessageDestroy(releaseControl);
			
			data->inControl = JAUS_FALSE;
		}
	}
}

void wdExcecuteControl(OjCmpt wd, VehicleState vehicleState)
{
	static double speedCommand = 0;
	static double prevSpeedError = 0;
	static double dampedSpeedErrorDerivative = 0;
	static double acceleration = ACCELERATION_MPSPS; //Mpsps
	static double decceleration = DECCELERATION_MPSPS; //Mpsps
	static double lastExcecuteTime = -1; 
	static double dt;
	static double linearEffortInt = 0.0;
	double linearEffort;
	double speedError;
	JausMessage txMessage;
	WdData *data;
	
	data = (WdData*)ojCmptGetUserData(wd);
	
	if(lastExcecuteTime < 0)
	{
		lastExcecuteTime = ojGetTimeSec();
		dt = 1.0 / WD_THREAD_DESIRED_RATE_HZ;
	}
	else
	{
		dt = ojGetTimeSec() - lastExcecuteTime;
		lastExcecuteTime = ojGetTimeSec();
	}
	
	if(speedCommand < vehicleState->desiredSpeedMps)
	{
		speedCommand += acceleration * dt;
		if(speedCommand > vehicleState->desiredSpeedMps)
		{
			speedCommand = vehicleState->desiredSpeedMps;
		}
	}	
	else
	{
		speedCommand -= decceleration * dt;		
		if(speedCommand < vehicleState->desiredSpeedMps)
		{
			speedCommand = vehicleState->desiredSpeedMps;
		}
	}
	
	speedError = speedCommand - vehicleState->speedMps;	

	linearEffortInt += speedError * dt;
	if(linearEffortInt > LINEAR_EFFORT_I_LIM)
	{
		linearEffortInt = LINEAR_EFFORT_I_LIM;
	}
	if(linearEffortInt < -LINEAR_EFFORT_I_LIM)
	{
		linearEffortInt = -LINEAR_EFFORT_I_LIM;
	}

	if(dt > 0.001)
	{
		dampedSpeedErrorDerivative = 0.9 * dampedSpeedErrorDerivative + 0.1 * (speedError - prevSpeedError) / dt;
		prevSpeedError = speedError;
	}

	linearEffort = LINEAR_EFFORT_K_FF * speedCommand + LINEAR_EFFORT_BIAS_FF; 
	linearEffort += LINEAR_EFFORT_K_P * speedError;
	linearEffort += LINEAR_EFFORT_K_I * linearEffortInt;
	linearEffort += LINEAR_EFFORT_K_D * dampedSpeedErrorDerivative;
	
	// Pitch feed forward
	//linearEffort += PITCH_FF_EFFORT_PER_RAD * sin(wdDampedPitchRad); // Pitch feed forward effort
	
	// Steering feed forward
	if(data->reportWrench)
	{
		linearEffort += STEERING_FF_EFFORT * (1.0/cos(data->reportWrench->propulsiveRotationalEffortZPercent * WHEEL_ROTATION_RAD_PER_EFFORT) - 1.0);
	}
	
	// Sticktion feed forward
	if(speedCommand < WD_DEFAULT_MIN_SPEED_MPS)
	{
		linearEffort += STICKTION_EFFORT; 
	}

	if(linearEffort > 0)
	{
		data->setWrench->propulsiveLinearEffortXPercent = THROTTLE_K * linearEffort;
		data->setWrench->resistiveLinearEffortXPercent = 0;
	}
	else
	{
		data->setWrench->propulsiveLinearEffortXPercent = 0;
		data->setWrench->resistiveLinearEffortXPercent = BRAKE_K * linearEffort;
	}

	if(data->setWrench->propulsiveLinearEffortXPercent > 100.0)
	{
		data->setWrench->propulsiveLinearEffortXPercent = 100.0;
	}
	if(data->setWrench->propulsiveLinearEffortXPercent < 0.0)
	{
		data->setWrench->propulsiveLinearEffortXPercent = 0.0;
	}

	if(data->setWrench->resistiveLinearEffortXPercent > 100.0)
	{
		data->setWrench->resistiveLinearEffortXPercent = 100.0;
	}
	if(data->setWrench->resistiveLinearEffortXPercent < 0.0)
	{
		data->setWrench->resistiveLinearEffortXPercent = 0.0;
	}

	data->setWrench->propulsiveRotationalEffortZPercent = vehicleState->desiredPhiEffort;

	if(data->setWrench->propulsiveRotationalEffortZPercent > 100.0)
	{
		data->setWrench->propulsiveRotationalEffortZPercent = 100.0;
	}
	if(data->setWrench->propulsiveRotationalEffortZPercent < -100.0)
	{
		data->setWrench->propulsiveRotationalEffortZPercent = -100.0;
	}

	jausAddressCopy(data->setWrench->destination, data->pdAddress);

	txMessage = setWrenchEffortMessageToJausMessage(data->setWrench);
	ojCmptSendMessage(wd, txMessage);		
	jausMessageDestroy(txMessage);
}

// Function calculates angle a minus angle b
double wdAngleSubtract(double a, double b)
{
   return atan2(sin(a-b), cos(a-b));
}