Week11 <<
Previous Next >> Week13
Week12
Task1 :
讀取 stage3_2a.txt, 建立 Stage3 的分組倉儲, 分組網頁, 以及各組員倉儲及網頁連結.
程式碼 :
with open("stage3_2a.txt") as fh:
#先將我們存起來stage3_2a.txt打開並命名為fh
data = fh.readlines()
#將stage3_2a.txt的資料以串列形式存為data
for i in range(6):
#將次數限定就不會告知list index out of range了
newdata1 = data[i].replace('4823122','40823122')
#因為40823122的學號打錯了,所以用 replace 把舊的替換成新的
newdata2 = newdata1.replace('\t\t','')
#因為有一組只有6人,所以用 replace 把空位刪除
newdata3 = newdata2.replace('_','-')
#因為在編輯txt的時候-會變成_,所以用 replace 把_修正為-
group = newdata3.rstrip("\n").split("\t")
#先取出newdata list中的第i項,消除元素中/n,再以\t取出需要的文字
print('<p><a href="https://github.com/'+group[1]+'/'+group[0]+'">'+group[0]+' repo</a> | <a href="https://'+group[2]+'.github.io/'+group[0]+'">'+group[0]+' site</a></p>')
for j in range(1,18,2):
#設一個範圍,(1到18,每次+2,1<=j<18)
try:
n = group[j].replace('40823112','a40823112')
#因為40823112的github帳號是a40823112,所以用 replace 把帳號修正
print('<p>'+'<a href="https://github.com/'+n+'/cd2021">'+group[j]+' repo</a> | <a href="https://'+n+'.github.io/cd2021">'+group[j]+' site</a></p>')
except:
continue
#這邊使用try.....except,因為有一組6個人,導致有空格,所以需要用continue讓迴圈繼續跑
操作影片 :
Task2 :
下載 CoppeliaSim 4.2.0 載點
下載控制程式碼 remote API 來控制場景
http://mde.tw/cad2020/downloads/coppeliasim/vrep_remoteapi_ex.7z
把程式碼放進SciTE後GO
程式碼 :
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')
操作影片:
Week11 <<
Previous Next >> Week13