0511 Programming in Lua in One program <<
Previous Next >> 機械手臂多人對戰
task2-remoteAPI
請各組員分別將個人在 stage1 與 stage2 所完成的 coppeliasim 場景, 採 Python remote API 進行操控, 並將過程拍成影片後, 放在個人與分組網站上.
stage3 專案中的 CoppeliaSim 請一律使用 4.2.0 版.
範例: http://mde.tw/cad2020/content/CoppeliaSim.html
W10 範例:
各班上課時所分配到的內部 IP, 192.168.192.1+組別+排序別
意即, 第五組排序別第一即為該組組長, 其 IP 為 192.168.192.151
以 http://mde.tw/cad2020/downloads/coppeliasim/vrep_remoteapi_ex.7z 為例, 每一個 Python3 remote api 程式執行需要 sim.py, simConst.py 與 remoteApi.dll 等三個檔案, 若針對 2w1a.ttt 場景, 以下列 Python 程式對位於 192.168.192.1 伺服器上的 CoppeliaSim 場景進行操控:
import sim as vrep
import math
import random
import time
print ('Start')
# Close eventual old connections
vrep.simxFinish(-1)
# Connect to V-REP remote server
clientID = vrep.simxStart('192.168.192.1', 19997, True, True, 5000, 5)
if clientID != -1:
print ('Connected to remote API server')
res = vrep.simxAddStatusbarMessage(
clientID, "teacher",
vrep.simx_opmode_oneshot)
if res not in (vrep.simx_return_ok, vrep.simx_return_novalue_flag):
print("Could not add a message to the status bar.")
# Communication operating mode with the remote API : wait for its answer before continuing (blocking mode)
# http://www.coppeliarobotics.com/helpFiles/en/remoteApiConstants.htm
opmode = vrep.simx_opmode_oneshot_wait
# Try to retrieve motors and robot handlers
# http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm#simxGetObjectHandle
ret1, wristHandle = vrep.simxGetObjectHandle(clientID, "WristMotor", opmode)
ret2, elbowHandle = vrep.simxGetObjectHandle(clientID, "ElbowMotor", opmode)
ret3, shoulderHandle = vrep.simxGetObjectHandle(clientID, "ShoulderMotor", opmode)
ret4, robotHandle = vrep.simxGetObjectHandle(clientID, "2W1A", opmode)
# If handlers are OK, execute three random simulations
if ret1 == 0 and ret2 == 0 and ret3 == 0:
random.seed()
for i in range(0, 3):
# Start the simulation
# http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm#simxStartSimulation
vrep.simxStartSimulation(clientID, opmode)
print("----- Simulation started -----")
# Start getting the robot position
# Unlike other commands, we will use a streaming operating mode
# http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm#simxGetObjectPosition
pret, robotPos = vrep.simxGetObjectPosition(clientID, robotHandle, -1, vrep.simx_opmode_streaming)
print("2w1a position: (x = " + str(robotPos[0]) +\
", y = " + str(robotPos[1]) + ")")
# Start getting the robot orientation
# Unlike other commands, we will use a streaming operating mode
# http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm#simxGetObjectOrientation
oret, robotOrient = vrep.simxGetObjectOrientation(clientID, robotHandle, -1, vrep.simx_opmode_streaming)
print("2w1a orientation: (x = " + str(robotOrient[0]) + \
", y = " + str(robotOrient[1]) +\
", z = " + str(robotOrient[2]) + ")")
# Make the robot move randomly five times
for j in range(0, 5):
# Generating random positions for the motors
awrist = random.randint(0, 300)
aelbow = random.randint(0, 300)
ashoulder = random.randint(0, 300)
# The control functions use Radians to determine the target position.
# Here, we use maths.radians to convert degrees into radians.
# http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm#simxSetJointTargetPosition
print("Motors target positions: " + str(ashoulder) + " " + str(aelbow) + " " + str(awrist))
vrep.simxSetJointTargetPosition(clientID, wristHandle, math.radians(awrist), opmode)
vrep.simxSetJointTargetPosition(clientID, elbowHandle, math.radians(aelbow), opmode)
vrep.simxSetJointTargetPosition(clientID, shoulderHandle, math.radians(ashoulder), opmode)
# Wait in order to let the motors finish their movements
# Tip: there must be a more efficient way to do it...
time.sleep(5)
# Get the motors effective positions after the movement sequence
# http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm#simxGetJointPosition
pwrist = vrep.simxGetJointPosition(clientID, wristHandle, opmode)
pelbow = vrep.simxGetJointPosition(clientID, elbowHandle, opmode)
pshoulder = vrep.simxGetJointPosition(clientID, shoulderHandle, opmode)
print("Motors reached positions: " + str(ashoulder) + " " + str(aelbow) + " " + str(awrist))
# Get the robot position after the movement sequence
pret, robotPos = vrep.simxGetObjectPosition(clientID, robotHandle, -1, vrep.simx_opmode_buffer)
print("2w1a position: (x = " + str(robotPos[0]) +\
", y = " + str(robotPos[1]) + ")")
# Get the robot orientation after the movement sequence
oret, robotOrient = vrep.simxGetObjectOrientation(clientID, robotHandle, -1, vrep.simx_opmode_buffer)
print("2w1a orientation: (x = " + str(robotOrient[0]) +\
", y = " + str(robotOrient[1]) +\
", z = " + str(robotOrient[2]) + ")")
# End the simulation, wait to be sure V-REP had the time to stop it entirely
# http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm#simxStopSimulation
vrep.simxStopSimulation(clientID, opmode)
time.sleep(1)
print("----- Simulation ended -----")
# Close the connection to V-REP remote server
# http://www.coppeliarobotics.com/helpFiles/en/remoteApiFunctionsPython.htm#simxFinish
vrep.simxFinish(clientID)
else:
print ('Failed connecting to remote API server')
print ('End')
CoppeliaSim 場景 remote API 協同監控流程:
- 各組員下載 https://portableapps.com/apps/music_video/obs-studio-portable
- 擔任 CoppliaSim 場景直播者時, 必須以 @gm 帳號登入 Youtube 後, 啟動 Live stream 取得直播串流金鑰, 將金鑰輸入 OBS 後, 擷取電腦顯示器啟動串流, 然後利用 copy video url 取得直播網址.
- 將直播網誌公布在 https://gitter.im/mdecourse/cd2021, 並附上伺服器 IPv4 內部網路 IP 位址與場景開放埠號.
- 直播者同時必須在個人網站上公布 Python remote API 操控直播場景相關參變數以及監控條件.
- 待遠端各協同操控學員準備就緒後, 完成操控流程後將所錄製的影片嵌入個人與分組網站中.
直播範例錄影:
參考資料:
RoboComp is a cutting-edge open-source robotics framework providing tools to easily create, modify and manage robot software components.
https://github.com/mdecourse/robocomp
Code to connect RocoComp to V-REP
https://github.com/mdecourse/V-REP-component
0511 Programming in Lua in One program <<
Previous Next >> 機械手臂多人對戰