Im getting an error when running my code; "Nonexistant function 'get_global_function_tranfsorm' in base 'Nil'."

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Progamer27

Im making a vr game that utilizes a MPU6050 through i2c connections on a raspberry pi. I made a custom i2c module that the camera script uses. I have most of the code written out that grabs the MPU6050 data through the i2c module. The script is attached to a Camera. No errors appear in the editor, but I continue to get the error “Nonexistant function ‘get_global_function_tranfsorm’ in base ‘Nil’” when I run the script. Here’s my code:

extends Camera

var I2C = preload("res://lib/I2C.gd")
onready var camera = get_node("Camera")
var head_rotation = camera.get_global_transform().basis.get_euler()
var head_position = camera.get_global_transform().origin

# Constants for the MPU6050
const MPU6050_ADDRESS = 0x68
const MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B
const MPU6050_REGISTER_GYRO_XOUT_H = 0x43
const MPU6050_SCALE_ACCEL = 16384.0
const MPU6050_SCALE_GYRO = 131.0

# Variables for storing sensor data
var accel_x = 0
var accel_y = 0
var accel_z = 0
var gyro_x = 0
var gyro_y = 0
var gyro_z = 0

func _ready():
	if camera == null:
		print("Camera node not found")
		return

func update_mpu6050():
	# Open the I2C device file
	var i2c = I2C.new()
	i2c.open_device("/dev/i2c-1")
	i2c.set_address(MPU6050_ADDRESS)

	# Read the raw sensor data
	var data = [0, 0, 0, 0, 0, 0, 0, 0]
	i2c.request(MPU6050_REGISTER_ACCEL_XOUT_H, data)

	# Convert the raw data to acceleration and angular velocity values
	accel_x = (data[0] << 8 | data[1]) / MPU6050_SCALE_ACCEL
	accel_y = (data[2] << 8 | data[3]) / MPU6050_SCALE_ACCEL
	accel_z = (data[4] << 8 | data[5]) / MPU6050_SCALE_ACCEL
	gyro_x = (data[6] << 8 | data[7]) / MPU6050_SCALE_GYRO
	gyro_y = (data[8] << 8 | data[9]) / MPU6050_SCALE_GYRO
	gyro_z = (data[10] << 8 | data[11]) / MPU6050_SCALE_GYRO

	# Close the I2C device file
	i2c.close()


func update_camera(delta):
	update_mpu6050()

	# Update the head rotation
	head_rotation.x += gyro_x * delta
	head_rotation.y += gyro_y * delta
	head_rotation.z += gyro_z * delta

	# Update the head position
	head_position.x += accel_x * delta
	head_position.y += accel_y * delta
	head_position.z += accel_z * delta

	# Accumulate the parent transforms
	var current_node = camera
	while current_node.get_parent():
		current_node = current_node.get_parent()
		head_rotation = current_node.get_global_transform().basis.get_euler() * head_rotation
		head_position = current_node.get_global_transform().origin + head_position

	# Apply the new orientation and position to the camera
	camera.set_transform(Transform(head_rotation, head_position))

I’ve been trying to resolve this issue, but I can’t figure out how, and I’m hoping for a little guidance. Thanks y’all!

Edited to fix code formatting. To do that in future posts, paste your code into the message, select the code, and the press the {} button to format it.

jgodfrey | 2023-02-09 02:42

Also, did you cut/paste the error into the post or did you retype it? I ask as it contains some obvious typos. For instance notice the spelling of tranfsorm in your title (in addition to a few other issues). I ask as I don’t see that same typo in your code… So, you’ve either (incorrectly) typed the error message (don’t do that - copy/paste it), or you haven’t pasted the code that actually contains the problem…

jgodfrey | 2023-02-09 02:46

Ah, sorry about that one! I did retype it. I also noticed when I went to copy paste the code, only half of it was pasted in. I updated the script.

Progamer27 | 2023-02-09 02:56

Hmmm… Looks like when you updated the code, you didn’t format it for the forum. I’d suggest you edit the post and fix that (instructions outlined in my previous comment).

jgodfrey | 2023-02-09 02:58

So, this code is attached to the camera you’re trying to reference below?

onready var camera = getnode("Camera")

If that’s attempting to access the camera the script is attached to, it will fail and return null, leading to the error you report on the very next line…

var head_rotation = camera.get_global_transform().basis.get_euler()

You don’t need the camera reference there at all - just get rid of it…

jgodfrey | 2023-02-09 03:09

To format that code (easily and correctly) for the forum…

  • Edit your message
  • Remove all of the code
  • Repaste a fresh copy of your code (easier than fixing what’s in the forum now)
  • Select the entire code block in forum message editor
  • Press the { } button in the forum editor’s mini-tool bar.
  • Look at the Preview panel to verify proper formatting
  • Submit the changes

jgodfrey | 2023-02-09 03:27

Oh, BTW - as a fellow rpi tinkerer, this sounds like an interesting project. :slight_smile:

jgodfrey | 2023-02-09 03:29

Thanks for the info! I’ll try out what you suggested in a few, I’ve gotta get through school and everything first. Also, yeah! It’s been a pretty fun project.

Progamer27 | 2023-02-09 11:34

Alright, so I got rid of the camera reference and its entirety. The project runs with only one error showing up in the debugger twice. I’m not too sure if it’s important, but here it is anyways:
get_global_transform: Condition "!is_inside_tree()" is true. Returned: Transform()

Here’s the updated code:

extends Camera

var I2C = preload("res://lib/I2C.gd")
#onready var camera = get_node("Camera")
var head_rotation = get_global_transform().basis.get_euler()
var head_position = get_global_transform().origin

# Constants for the MPU6050
const MPU6050_ADDRESS = 0x68
const MPU6050_REGISTER_ACCEL_XOUT_H = 0x3B
const MPU6050_REGISTER_GYRO_XOUT_H = 0x43
const MPU6050_SCALE_ACCEL = 16384.0
const MPU6050_SCALE_GYRO = 131.0

# Variables for storing sensor data
var accel_x = 0
var accel_y = 0
var accel_z = 0
var gyro_x = 0
var gyro_y = 0
var gyro_z = 0


func update_mpu6050():
	# Open the I2C device file
	var i2c = I2C.new()
	i2c.open_device("/dev/i2c-1")
	i2c.set_address(MPU6050_ADDRESS)

	# Read the raw sensor data
	var data = [0, 0, 0, 0, 0, 0, 0, 0]
	i2c.request(MPU6050_REGISTER_ACCEL_XOUT_H, data)

	# Convert the raw data to acceleration and angular velocity values
	accel_x = (data[0] << 8 | data[1]) / MPU6050_SCALE_ACCEL
	accel_y = (data[2] << 8 | data[3]) / MPU6050_SCALE_ACCEL
	accel_z = (data[4] << 8 | data[5]) / MPU6050_SCALE_ACCEL
	gyro_x = (data[6] << 8 | data[7]) / MPU6050_SCALE_GYRO
	gyro_y = (data[8] << 8 | data[9]) / MPU6050_SCALE_GYRO
	gyro_z = (data[10] << 8 | data[11]) / MPU6050_SCALE_GYRO

	# Close the I2C device file
	i2c.close()


func update_camera(delta):
	update_mpu6050()

	# Update the head rotation
	head_rotation.x += gyro_x * delta
	head_rotation.y += gyro_y * delta
	head_rotation.z += gyro_z * delta

	# Update the head position
	head_position.x += accel_x * delta
	head_position.y += accel_y * delta
	head_position.z += accel_z * delta

	# Accumulate the parent transforms
	var current_node = Camera
	while current_node.get_parent():
		current_node = current_node.get_parent()
		head_rotation = current_node.get_global_transform().basis.get_euler() * head_rotation
		head_position = current_node.get_global_transform().origin + head_position

	# Apply the new orientation and position to the camera
	set_transform(Transform(head_rotation, head_position))

When I move the MPU6050 the camera does not rotate in return, which leads me to believe its something in the module I created for i2c communication with the MPU6050, or the data in the code I provided is incorrectly being translated into camera movements. I should also note that I gave permissions for Godot to access /dev/i2c-1.

Progamer27 | 2023-02-09 19:55

This code still looks suspect to me:

    var current_node = Camera

What is Camera in that context? Assuming you just want to start from the current node (the one containing the script, which IS a Camera), change that to:

    var current_node = self

jgodfrey | 2023-02-09 21:37

Yeah, in context its just to start from the current node and create a reference from it. I changed Camera to self like you suggested. Do you think the code for head_rotation in the update_camera function looks reasonable for altering the camera’s position and rotation? I’m stepping into coding with Godot with some experience from the past, and I’m definitely willing to expand my understandings.

Progamer27 | 2023-02-09 21:51

Hey! Just a quick update, I’d like to add that upon more research and testing, the camera script either for some reason isn’t using the functions defined in the I2C module I created, or isn’t accessing the I2C module at all.

Progamer27 | 2023-02-10 20:13