# Setting Digital input to freedrive through java

**URL:** https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113
**Category:** Java
**Created:** [June 13, 2018, 5:38pm UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113 "2018-06-13T17:38:34Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![anon44882538](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@anon44882538](https://forum.universal-robots.com/u/anon44882538)
#### Post date: [June 13, 2018, 5:38pm UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113/1 "2018-06-13T17:38:34Z")

</div>

Is there any way to set up a digital input as your freedrive function through Java, or does it have to be a socket script command that is sent every time that input is on? I know you can manually configure through the I/O setup on the pendant, but I would like to be have this automatically configured by the Cap if possible.

Thanks for your consideration,  
Karsten

---

<div class="post-metadata">

### Author: ![anon44882538](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@anon44882538](https://forum.universal-robots.com/u/anon44882538)
#### Post date: [June 18, 2018, 8:24pm UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113/2 "2018-06-18T20:24:21Z")

</div>

I apologize if what I wrote above wasn’t clear enough, I have attached the document to reassign the input through the robot. I was wondering if you could do the exact same reassignment through the cap. [Redefine UR Tool IO for free drive plus other options.pdf](https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/flex020/uploads/universal_robots/original/1X/670ed6e7458cd6ead61a2986733e9f7658ff0187.pdf) (603.1 KB)

Best,  
Karsten

---

<div class="post-metadata">

### Author: ![jbm](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/jbm/32/614_2.png) [@jbm](https://forum.universal-robots.com/u/jbm)
#### Post date: [June 28, 2018, 11:44am UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113/3 "2018-06-28T11:44:23Z")

</div>

You can from a daemon or thread monitor an input, and given this input toggles to a defined state, you can send a program to the controller that instructs the robot to stay in freedrive mode, such as:

```auto
def start_freedrive():
   freedrive_mode()
   while(True):
      sync()
   end
end

```

Since freedrive would be terminated, as soon as the program would stop, you cannot just send a single line, in which case the robot would only be in freedrive for a few milliseconds.  
When the button is released, you need to terminate this program, e.g. by sending `halt` or `end_freedrive_mode()`

---

<div class="post-metadata">

### Author: ![anon44882538](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@anon44882538](https://forum.universal-robots.com/u/anon44882538)
#### Post date: [June 28, 2018, 5:16pm UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113/4 "2018-06-28T17:16:01Z")

</div>

Thank you for the reply as I had that exact issue of the robot entering freedrive for only milliseconds. However, before I set up my daemon I am currently testing my function by monitoring the input through java and sending the function through the realtime client, and the robot is not behaving any differently. It is a very simple function so I am not sure what I am doing wrong? Is there a reason why you cannot send a while loop through the socket? The output I am getting from the command I send through the socket is below. Please advise

def myCustomCode():  
while get\_tool\_digital\_in(1) == True:  
freedrive\_mode()  
sync()  
end  
end

Thank you,  
Karsten

---

<div class="post-metadata">

### Author: ![anon44882538](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@anon44882538](https://forum.universal-robots.com/u/anon44882538)
#### Post date: [June 28, 2018, 5:18pm UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113/5 "2018-06-28T17:18:45Z")

</div>

The reply box did not allow my indents to show up in the above code, but it is properly indented when it actually sends.

Thanks

---

<div class="post-metadata">

### Author: ![anon42905246](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon42905246](https://forum.universal-robots.com/u/anon42905246)
#### Post date: [June 28, 2018, 9:14pm UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113/6 "2018-06-28T21:14:22Z")

</div>

> [@jbm](#):
>
> When the button is released, you need to terminate this program, e.g. by sending `halt` or `end_freedrive_mode()`

How about:

```auto
def start_freedrive():
    freedrive_mode()
    while get_tool_digital_signal(1):
        sync()
    end
    end_freedrive_mode()
end

```

@anon44882538 Are you sure you are not supposed to be looking at tool input 0?

Try sending:

```auto
def myCode():
    textmsg("Tool input 0", get_tool_digital_input(0))
    textmsg("Tool input 1", get_tool_digital_input(1))
end

```

Then check the log.

To get proper formatting you need to have 4 spaces in the beginning of the line and have one empty line between code and previous paragraph.  
To get also syntax coloring, write your code between 2 lines with triple ticks: ```. Then you don’t have to do indentation and free lines.

---

<div class="post-metadata">

### Author: ![anon44882538](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@anon44882538](https://forum.universal-robots.com/u/anon44882538)
#### Post date: [June 29, 2018, 11:45am UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113/7 "2018-06-29T11:45:25Z")

</div>

Yes I am sure its tool input 1, I actually looped the function i was sending through java so that I could feel the little jolts of freedrive, so it is going into freedrive, I just haven’t been able to get it to stay in freedrive yet. My end\_freedrive\_mode function is sending properly, so I just need to figure out how to make my while loop function. I will try what you said in the first part of your reply to start, and just see if I can get it to work by changing around my function and indentation a bit.

---

<div class="post-metadata">

### Author: ![anon42905246](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon42905246](https://forum.universal-robots.com/u/anon42905246)
#### Post date: [June 29, 2018, 5:08pm UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113/8 "2018-06-29T17:08:20Z")

</div>

What happens if you put this program in Polyscope directly? Does it also enter free drive in milliseconds jolts?

---

<div class="post-metadata">

### Author: ![anon65613222](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@anon65613222](https://forum.universal-robots.com/u/anon65613222)
#### Post date: [September 25, 2018, 9:01am UTC](https://forum.universal-robots.com/t/setting-digital-input-to-freedrive-through-java/2113/9 "2018-09-25T09:01:42Z")

</div>

Hello!

We have an integrator that was looking into doing the same thing.

Just for info for other people with similar problems:  
One thing that gave us trouble was the character encoding that, it was not UTF, so it was not reading \n as we expected it.

Anyhow, for troubleshooting, a simple solution to test is to send it on the primary or secondary interface, this example with a popup instead of a input:

Locally on the robot:  
`printf 'def test(): \n freedrive_mode() \n popup("test") \n while(1): \n sync() \n end \nend \n' | nc -q 0 127.0.0.1 30002`  
-q 0 flag on nc is to close the connection straight after sending.

or remote from a terminal with telnet  
`echo -e 'def test(): \n freedrive_mode() \n popup("test", blocking=True) \n end_freedrive_mode() \nend \n' | telnet IP_ADRESS_OF_ROBOT 30002`  
-e flag on echo is the same output as printf and this is without the while loop, so the blocking will keep the programs from running further, til popup is closed…

Hope it helped someone!
