Skip to content

Commit 8c509c4

Browse files
committed
Fanuc ChatGPT
1 parent 5ff3f8f commit 8c509c4

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ We introduce an experimental feature: Robot Apps. This class facilitates modular
102102

103103
1. [Pick and Place App](examples/PickAndPlaceApp.py)
104104
1. [Aruco Tracking App](examples/ArucoTrackingApp.py)
105+
1. [FANUC ChatGPT](examples/fanucpy-gpt/README.MD)
105106

106107
## Citation
107108
Please use the following to cite if you are using this library in academic publications [Towards Modular and Plug-and-Produce Manufacturing Apps](https://www.sciencedirect.com/science/article/pii/S2212827122004255)

examples/fanucpy-gpt/README.MD

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# FANUCPY-GPT
2+
3+
This is a very early attempt to program and control industrial robots with plain human language using OpenAI ChatGPT.
4+
5+
## Usage
6+
Check [demo.py](demo.py) and use with CAUTION!!!

examples/fanucpy-gpt/demo.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import openai
2+
import subprocess
3+
import json
4+
5+
# open reference code
6+
with open("reference.py", "r") as f:
7+
ref_code = " ".join(f.readlines())
8+
9+
# prepare API call
10+
openai.api_key = "PUT-YOUR-OWN-KEY"
11+
messages = [
12+
{"role": "system", "content": "You are coding assistant. Provide only code."},
13+
{"role": "user", "content": "This is a reference code: " + ref_code},
14+
]
15+
16+
17+
while True:
18+
cmd = input("Provide a command: ")
19+
if cmd == "end":
20+
break
21+
msg = {
22+
"role": "user",
23+
"content": "Only using functions in the reference code "
24+
f"write a full code with all necessary imports for the following task: {cmd}"
25+
"If the task starts with remember ensure the code contains print."
26+
"Otherwise there is no need for explanation an do not output anything.```"
27+
}
28+
messages.append(msg)
29+
30+
# call ChatGPT
31+
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
32+
chat_out = response["choices"][0]["message"]["content"]
33+
34+
# save chat history
35+
msg = {"role": "assistant", "content": chat_out}
36+
messages.append(msg)
37+
38+
if "fanucpy" in chat_out:
39+
# write to a python file
40+
with open("generated_code.py", "w") as f:
41+
code = chat_out.split("```")
42+
if len(code) > 1:
43+
code = code[1]
44+
else:
45+
code = code[0]
46+
code = code.strip("python")
47+
f.write(code)
48+
49+
# run code in physical robot
50+
output = subprocess.check_output(["python", "generated_code.py"])
51+
print(output)
52+
msg = {"role": "assistant", "content": "generated output " + str(output)}
53+
messages.append(msg)
54+
55+
with open("messages.json", "w") as f:
56+
json.dump(messages, f)

examples/fanucpy-gpt/reference.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from fanucpy import Robot
2+
3+
# connecting to a physical robot
4+
robot = Robot(
5+
robot_model="Fanuc",
6+
host="192.168.0.109",
7+
port=18735,
8+
ee_DO_type="RDO",
9+
ee_DO_num=7,
10+
)
11+
robot.connect()
12+
13+
# move in joint space
14+
robot.move(
15+
"joint",
16+
vals=[19.0, 66.0, -33.0, 18.0, -30.0, -33.0],
17+
velocity=100,
18+
acceleration=100,
19+
cnt_val=0,
20+
linear=False,
21+
)
22+
23+
# move in cartesian space
24+
robot.move(
25+
"pose",
26+
vals=[0.0, -28.0, -35.0, 0.0, -55.0, 0.0],
27+
velocity=50,
28+
acceleration=50,
29+
cnt_val=0,
30+
linear=False,
31+
)
32+
33+
# open gripper
34+
robot.gripper(True)
35+
36+
# close gripper
37+
robot.gripper(False)
38+
39+
# get robot state
40+
print(f"Current pose: {robot.get_curpos()}")
41+
print(f"Current joints: {robot.get_curjpos()}")
42+
print(f"Instantaneous power: {robot.get_ins_power()}")
43+
print(f"Get gripper state: {robot.get_rdo(7)}")

0 commit comments

Comments
 (0)