PYTHON
43
FindDistance
Guest on 2nd August 2022 12:47:53 AM
from cozmo_fsm import *
import math
import numpy as np
class FindDistance(StateNode):
def start(self, event=None):
if self.running:return
super().start(event)
#Translation matrix between the camera and base frame
#Note - the base frame lies on the front axel
cam_to_base_matrix = robot.kine.joint_to_base('camera')
#Initiates the location of the object as 100mm in front of the camera
#In the camera frame, z points forward, so z = 100
object_location = np.array([[0],[0],[100],[1]])
#Finds the location of the object relative to the base frame(front axel)
base_location = np.matmul(cam_to_base_matrix, object_location)
#Gets the x location from the matrix since x points forward in the base frame
dist = base_location[0][0]
print(dist)
self.post_completion()
#Runs through the FSM to find the values
class KineCalc(StateMachineProgram):
$setup {
StateNode() =N=> SetHeadAngle(-10) =C=> FindDistance() =C=>
SetHeadAngle(0) =C=> FindDistance() =C=>
SetHeadAngle(10) =C=> FindDistance() =C=>
SetHeadAngle(20) =C=> FindDistance() =C=>
SetHeadAngle(30) =C=> FindDistance() =C=>
SetHeadAngle(40) =C=> FindDistance()
}