KrakenManipulatorController.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "KrakenManipulatorController.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzFramework/Components/TransformComponent.h>
  11. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  12. namespace AppleKraken
  13. {
  14. ROS2::MotorizedJoint* ManipulatorController::getMotorizedJoint(const AZ::EntityId& entityWithMotJoint)
  15. {
  16. AZ_Assert(entityWithMotJoint.IsValid(), "Entity Invalid");
  17. AZ::Entity* e_ptr{ nullptr };
  18. AZ::ComponentApplicationBus::BroadcastResult(e_ptr, &AZ::ComponentApplicationRequests::FindEntity, entityWithMotJoint);
  19. AZ_Assert(e_ptr, "No such entity");
  20. ROS2::MotorizedJoint* component = e_ptr->FindComponent<ROS2::MotorizedJoint>();
  21. // AZ_Assert(component, "No ROS2::MotorizedJoint in %s", e_ptr->GetName().c_str());
  22. return component;
  23. }
  24. void ManipulatorController::Activate()
  25. {
  26. ManipulatorRequestBus::Handler::BusConnect(GetEntityId());
  27. AZ::TickBus::Handler::BusConnect();
  28. initialized = false;
  29. }
  30. void ManipulatorController::Deactivate()
  31. {
  32. ManipulatorRequestBus::Handler::BusDisconnect(GetEntityId());
  33. AZ::TickBus::Handler::BusDisconnect();
  34. }
  35. void ManipulatorController::Reflect(AZ::ReflectContext* context)
  36. {
  37. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  38. {
  39. serialize->Class<ManipulatorController, AZ::Component>()
  40. ->Version(2)
  41. ->Field("ManipulatorEntityX", &ManipulatorController::m_entityX)
  42. ->Field("ManipulatorEntityY", &ManipulatorController::m_entityY)
  43. ->Field("ManipulatorEntityZ", &ManipulatorController::m_entityZ)
  44. ->Field("ManipulatorVecX", &ManipulatorController::m_vectorX)
  45. ->Field("ManipulatorVecY", &ManipulatorController::m_vectorY)
  46. ->Field("ManipulatorVecZ", &ManipulatorController::m_vectorZ)
  47. ->Field("RestEntity", &ManipulatorController::m_restEntity)
  48. ->Field("m_effector", &ManipulatorController::m_effector)
  49. ->Field("max_errorXZ", &ManipulatorController::max_errorXZ)
  50. ->Field("max_errorY", &ManipulatorController::max_errorY)
  51. ->Field("timeSetpointReach", &ManipulatorController::m_timeSetpointReach);
  52. if (AZ::EditContext* ec = serialize->GetEditContext())
  53. {
  54. ec->Class<ManipulatorController>("ManipulatorController", "ManipulatorController")
  55. ->ClassElement(AZ::Edit::ClassElements::EditorData, "ManipulatorController")
  56. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  57. ->Attribute(AZ::Edit::Attributes::Category, "AppleKraken")
  58. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_entityX, "m_entityX", "m_entityX")
  59. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_entityY, "m_entityY", "m_entityY")
  60. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_entityZ, "m_entity_z1", "m_entityZ")
  61. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_vectorX, "vx", "vx")
  62. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_vectorY, "vy", "vy")
  63. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_vectorZ, "vz", "vz")
  64. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_effector, "Effector", "Effector")
  65. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_restEntity, "Rest entity", "Rest Entity")
  66. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::max_errorXZ, "max_errorXZ", "max error XZ to retract nose")
  67. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::max_errorY, "max_errorY", "max error Y to retract nose")
  68. ->DataElement(
  69. AZ::Edit::UIHandlers::EntityId,
  70. &ManipulatorController::m_timeSetpointReach,
  71. "SetPoint Reach time",
  72. "SetPoint reach time");
  73. }
  74. }
  75. }
  76. void ManipulatorController::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  77. {
  78. if (!initialized)
  79. {
  80. // get offset to effector
  81. AZ::Transform transformBaseLink;
  82. AZ::TransformBus::EventResult(transformBaseLink, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  83. AZ::Transform transformEffector;
  84. AZ::TransformBus::EventResult(transformEffector, m_effector, &AZ::TransformBus::Events::GetWorldTM);
  85. m_transform_base_link_to_effector = transformBaseLink.GetInverse() * transformEffector;
  86. initialized = true;
  87. return;
  88. }
  89. if (m_desiredApple)
  90. {
  91. m_desiredPosition = *m_desiredApple;
  92. }
  93. else
  94. {
  95. AZ::TransformBus::EventResult(m_desiredPosition, m_restEntity, &AZ::TransformBus::Events::GetWorldTranslation);
  96. }
  97. // apple is given in the World's coordinate system
  98. AZ::Transform current_base_link;
  99. AZ::TransformBus::EventResult(current_base_link, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  100. AZ::Vector3 position_in_baselink_tf = current_base_link.GetInverse().TransformPoint(m_desiredPosition);
  101. AZ::Vector3 position_in_effector_tf = m_transform_base_link_to_effector.GetInverse().TransformPoint(position_in_baselink_tf);
  102. float setpoint_x = position_in_effector_tf.Dot(m_vectorX);
  103. float setpoint_z = position_in_effector_tf.Dot(m_vectorZ);
  104. float error_x = std::numeric_limits<float>::max();
  105. float error_z = std::numeric_limits<float>::max();
  106. if (m_entityX.IsValid())
  107. {
  108. auto component_x = getMotorizedJoint(m_entityX);
  109. if (component_x)
  110. {
  111. component_x->SetSetpoint(setpoint_x);
  112. error_x = component_x->GetError();
  113. }
  114. }
  115. if (m_entityZ.IsValid())
  116. {
  117. auto component_z = getMotorizedJoint(m_entityZ);
  118. if (component_z)
  119. {
  120. component_z->SetSetpoint(setpoint_z);
  121. error_z = component_z->GetError();
  122. }
  123. }
  124. // auto - disable nose retrieve only if we reached small error.
  125. if (m_noseRetrieved == true)
  126. {
  127. m_time_XZ_ok += deltaTime;
  128. if (m_time_XZ_ok > m_timeSetpointReach)
  129. {
  130. if (error_x < max_errorXZ && error_x > -max_errorXZ && error_z < max_errorXZ && error_z > -max_errorXZ)
  131. {
  132. AZ_Printf("ManipulatorController", "Nose is sliding out \n");
  133. m_noseRetrieved = false;
  134. m_time_XZ_ok = 0;
  135. }
  136. }
  137. }
  138. float setpoint_y = position_in_effector_tf.Dot(m_vectorY);
  139. if (m_entityY.IsValid())
  140. {
  141. auto component_y = getMotorizedJoint(m_entityY);
  142. if (component_y)
  143. {
  144. if (m_noseRetrieved)
  145. {
  146. setpoint_y = 0;
  147. m_time_Y_ok += deltaTime;
  148. if (m_time_Y_ok > m_timeSetpointReach)
  149. {
  150. auto error_y = getMotorizedJoint(m_entityY)->GetError();
  151. if (error_y < max_errorY && error_y > -max_errorY)
  152. {
  153. m_noseRetrievingSuccess = true;
  154. m_time_Y_ok = 0.0;
  155. }
  156. }
  157. }
  158. else
  159. {
  160. m_noseRetrievingSuccess = false;
  161. }
  162. component_y->SetSetpoint(setpoint_y);
  163. }
  164. }
  165. }
  166. void ManipulatorController::PickApple(const AZ::Vector3 position)
  167. {
  168. m_noseRetrieved = true;
  169. m_time_XZ_ok = 0;
  170. AZ_Printf("ManipulatorController", "PickApple\n");
  171. ResetTimer();
  172. m_desiredApple = position;
  173. };
  174. AZ::Vector3 ManipulatorController::GetPosition()
  175. {
  176. auto currentPosition = AZ::Vector3(
  177. getMotorizedJoint(m_entityX)->GetCurrentPosition(),
  178. getMotorizedJoint(m_entityY)->GetCurrentPosition(),
  179. getMotorizedJoint(m_entityZ)->GetCurrentPosition()
  180. );
  181. return currentPosition;
  182. };
  183. void ManipulatorController::Retrieve()
  184. {
  185. AZ_Printf("ManipulatorController", "Retrieve\n");
  186. m_time_XZ_ok = std::numeric_limits<float>::lowest();
  187. m_noseRetrieved = true;
  188. m_desiredApple.reset();
  189. };
  190. void ManipulatorController::RetrieveNose()
  191. {
  192. AZ_Printf("ManipulatorController", "RetrieveNose\n");
  193. m_time_XZ_ok = std::numeric_limits<float>::lowest();
  194. m_noseRetrieved = true;
  195. }
  196. int ManipulatorController::GetStatus()
  197. {
  198. return 0;
  199. };
  200. void ManipulatorController::ResetTimer()
  201. {
  202. AZ_Printf("ManipulatorController", "Timer is RESET!\n");
  203. m_time_XZ_ok = 0;
  204. }
  205. bool ManipulatorController::IsNoseRetreived()
  206. {
  207. return m_noseRetrievingSuccess;
  208. }
  209. } // namespace AppleKraken