KrakenManipulatorController.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if (AZ::EditContext* ec = serialize->GetEditContext())
  52. {
  53. ec->Class<ManipulatorController>("ManipulatorController", "ManipulatorController")
  54. ->ClassElement(AZ::Edit::ClassElements::EditorData, "ManipulatorController")
  55. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  56. ->Attribute(AZ::Edit::Attributes::Category, "AppleKraken")
  57. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_entityX, "m_entityX", "m_entityX")
  58. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_entityY, "m_entityY", "m_entityY")
  59. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_entityZ, "m_entity_z1", "m_entityZ")
  60. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_vectorX, "vx", "vx")
  61. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_vectorY, "vy", "vy")
  62. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_vectorZ, "vz", "vz")
  63. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_effector, "Effector", "Effector")
  64. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::m_restEntity, "Rest entity", "Rest Entity")
  65. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::max_errorXZ, "max_errorXZ", "max error XZ to retract nose")
  66. ->DataElement(AZ::Edit::UIHandlers::EntityId, &ManipulatorController::max_errorY, "max_errorY", "max error Y to retract nose");
  67. }
  68. }
  69. }
  70. void ManipulatorController::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  71. {
  72. if (!initialized)
  73. {
  74. // get offset to effector
  75. AZ::Transform transformBaseLink;
  76. AZ::TransformBus::EventResult(transformBaseLink, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  77. AZ::Transform transformEffector;
  78. AZ::TransformBus::EventResult(transformEffector, m_effector, &AZ::TransformBus::Events::GetWorldTM);
  79. m_transform_base_link_to_effector = transformBaseLink.GetInverse() * transformEffector;
  80. initialized = true;
  81. return;
  82. }
  83. if (m_desiredApple)
  84. {
  85. m_desiredPosition = *m_desiredApple;
  86. }
  87. else
  88. {
  89. AZ::TransformBus::EventResult(m_desiredPosition, m_restEntity, &AZ::TransformBus::Events::GetWorldTranslation);
  90. }
  91. // apple is given in the World's coordinate system
  92. AZ::Transform current_base_link;
  93. AZ::TransformBus::EventResult(current_base_link, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  94. AZ::Vector3 position_in_baselink_tf = current_base_link.GetInverse().TransformPoint(m_desiredPosition);
  95. AZ::Vector3 position_in_effector_tf = m_transform_base_link_to_effector.GetInverse().TransformPoint(position_in_baselink_tf);
  96. float setpoint_x = position_in_effector_tf.Dot(m_vectorX);
  97. float setpoint_z = position_in_effector_tf.Dot(m_vectorZ);
  98. float error_x = std::numeric_limits<float>::max();
  99. float error_z = std::numeric_limits<float>::max();
  100. if (m_entityX.IsValid())
  101. {
  102. auto component_x = getMotorizedJoint(m_entityX);
  103. if (component_x)
  104. {
  105. component_x->SetSetpoint(setpoint_x);
  106. error_x = component_x->GetError();
  107. }
  108. }
  109. if (m_entityZ.IsValid())
  110. {
  111. auto component_z = getMotorizedJoint(m_entityZ);
  112. if (component_z)
  113. {
  114. component_z->SetSetpoint(setpoint_z);
  115. error_z = component_z->GetError();
  116. }
  117. }
  118. // auto - disable nose retrieve only if we reached small error.
  119. if (m_noseRetrieved == true)
  120. {
  121. m_time_XZ_ok += deltaTime;
  122. if (m_time_XZ_ok > m_timeSetpointReach)
  123. {
  124. if (error_x < max_errorXZ && error_x > -max_errorXZ && error_z < max_errorXZ && error_z > -max_errorXZ)
  125. {
  126. AZ_Printf("ManipulatorController", "Nose is sliding out \n");
  127. m_noseRetrieved = false;
  128. m_time_XZ_ok = 0;
  129. }
  130. }
  131. }
  132. float setpoint_y = position_in_effector_tf.Dot(m_vectorY);
  133. if (m_entityY.IsValid())
  134. {
  135. auto component_y = getMotorizedJoint(m_entityY);
  136. if (component_y)
  137. {
  138. if (m_noseRetrieved)
  139. {
  140. setpoint_y = 0;
  141. m_time_Y_ok += deltaTime;
  142. if (m_time_Y_ok > m_timeSetpointReach)
  143. {
  144. auto error_y = getMotorizedJoint(m_entityY)->GetError();
  145. if (error_y < max_errorY && error_y > -max_errorY)
  146. {
  147. m_noseRetrievingSuccess = true;
  148. m_time_Y_ok = 0.0;
  149. }
  150. }
  151. }
  152. else
  153. {
  154. m_noseRetrievingSuccess = false;
  155. }
  156. component_y->SetSetpoint(setpoint_y);
  157. }
  158. }
  159. //auto currentPosition = GetPosition();
  160. AZ_Printf("ManipulatorController", "#### [%f, %f, %f] [%f, %f, %f]\n",setpoint_x, setpoint_y, setpoint_z,
  161. getMotorizedJoint(m_entityX)->GetCurrentPosition(),
  162. getMotorizedJoint(m_entityY)->GetCurrentPosition(),
  163. getMotorizedJoint(m_entityZ)->GetCurrentPosition());
  164. }
  165. void ManipulatorController::PickApple(const AZ::Vector3 position)
  166. {
  167. m_noseRetrieved = true;
  168. m_time_XZ_ok = 0;
  169. AZ_Printf("ManipulatorController", "PickApple\n");
  170. ResetTimer();
  171. m_desiredApple = position;
  172. };
  173. AZ::Vector3 ManipulatorController::GetPosition()
  174. {
  175. auto currentPosition = AZ::Vector3(
  176. getMotorizedJoint(m_entityX)->GetCurrentPosition(),
  177. getMotorizedJoint(m_entityY)->GetCurrentPosition(),
  178. getMotorizedJoint(m_entityZ)->GetCurrentPosition()
  179. );
  180. return currentPosition;
  181. };
  182. void ManipulatorController::Retrieve()
  183. {
  184. AZ_Printf("ManipulatorController", "Retrieve\n");
  185. m_time_XZ_ok = std::numeric_limits<float>::lowest();
  186. m_noseRetrieved = true;
  187. m_desiredApple.reset();
  188. };
  189. void ManipulatorController::RetrieveNose()
  190. {
  191. AZ_Printf("ManipulatorController", "RetrieveNose\n");
  192. m_time_XZ_ok = std::numeric_limits<float>::lowest();
  193. m_noseRetrieved = true;
  194. }
  195. int ManipulatorController::GetStatus()
  196. {
  197. return 0;
  198. };
  199. void ManipulatorController::ResetTimer()
  200. {
  201. AZ_Printf("ManipulatorController", "Timer is RESET!\n");
  202. m_time_XZ_ok = 0;
  203. }
  204. bool ManipulatorController::IsNoseRetreived()
  205. {
  206. return m_noseRetrievingSuccess;
  207. }
  208. AZ::EntityId ManipulatorController::GetEffectorEntity()
  209. {
  210. return m_effector;
  211. }
  212. AZ::EntityId ManipulatorController::GetRestEntity()
  213. {
  214. return m_restEntity;
  215. }
  216. } // namespace AppleKraken