# Creating a Script to allow UR5 to read accelerometer and set gravity level in real time

**URL:** <https://forum.universal-robots.com/t/creating-a-script-to-allow-ur5-to-read-accelerometer-and-set-gravity-level-in-real-time/23227>\
**Category:** General Application Help: How do I?\
**Created:** [August 11, 2022, 10:53am UTC](https://forum.universal-robots.com/t/creating-a-script-to-allow-ur5-to-read-accelerometer-and-set-gravity-level-in-real-time/23227 "2022-08-11T10:53:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon62255605](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@anon62255605](https://forum.universal-robots.com/u/anon62255605)\
**Post date:** [August 11, 2022, 10:53am UTC](https://forum.universal-robots.com/t/creating-a-script-to-allow-ur5-to-read-accelerometer-and-set-gravity-level-in-real-time/23227/1 "2022-08-11T10:53:24Z")

</div>

Hello Everyone,  
I have a question regarding creating a script for a UR5 robot. Basically I am intending to be changing the gravity and orientation level that the UR5 will undergo, so to avoid the safety functions kicking in I am going to use an accelerometer to measure the gravity level of two axis, and then feed them into the robot using the two analogue inputs. Then I will read the two analogue inputs and then use them to use the set\_gravity function in the robot so it will update it’s gravity level in real time. However I am running into problems in writing the script. I intend to use the script function that is in the robot itself, adding in a script structure and write it on the robot itself rather than outside from a laptop and using the TCP connection. However, I am having problems in writing the script.  
I intially thought something like this could work:  
thread update\_gravity():  
while (True):  
Jx=(get\_standard\_analog\_in(0)-Xoffset)\*Xscale  
Jy=(get\_tool\_analog\_in(0)-Yoffset)\*Yscale  
Jz=(get\_standard\_analog\_in(1)-Zoffset)\*Zscale  
#or something similar with modbus\_get\_signal\_status(“Jx”,false) et.c.  
set\_gravity(Jx,Jy,Jz)  
sync(); # sleep until next 125Hz tick  
end

However I am running into problems, as the script writing in the robot seems very basic, with only True, False, and, or, xor and not functions available and trying to write while using the keyboard I am unable to submit it as a line.  
Does anyone have experience in writing a script within the GUI of the robot itself or will I have to use an outside source?  
Any help would be greatly appreciated,  
Thank you!

---

<div class="post-metadata">

**Author:** ![mvrooij](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/mvrooij/32/2151_2.png) [@mvrooij](https://forum.universal-robots.com/u/mvrooij)\
**Post date:** [August 12, 2022, 11:26am UTC](https://forum.universal-robots.com/t/creating-a-script-to-allow-ur5-to-read-accelerometer-and-set-gravity-level-in-real-time/23227/2 "2022-08-12T11:26:29Z")

</div>

I’m not sure what the question is… Your problem is that you cannot submit a program to the robot? Perhaps this is just a simple line ending issue (robot expects \r\n).

You can also directly input this program directly in the teach pendant or save it tinto a file, upload it to the robot and import it as a multi-line script.
