Why when I use movement and turn together, the turn does not work - roblox

why if i use it code - rotation is working
p_partnew.Position = Vector3.new (i,p_coord_y, p_coord_z)
p_partnew.CFrame = p_partnew.CFrame*CFrame.Angles(p_angles_x,p_angles_y, p_angles_z)
if i use it code - rotation is NOT working
p_partnew.CFrame = CFrame.new (i,p_coord_y, p_coord_z)
p_partnew.CFrame = p_partnew.CFrame*CFrame.Angles(p_angles_x,p_angles_y, p_angles_z)

In the first example, only the position of the part is being modified and then the rotation is applied. The second example sets the whole CFrame to the position which will override the original rotation of the object, and then applies the rotation.
Simply put, #1 adds p_angles to the rotation, while #2 sets the rotation to p_angles.

To understand what's going on, take a look at Understanding CFrames.
A CFrame is a 4x3 matrix with components corresponding to the Part's Position and Orientation. When you get or set a Part's Position property, it is just reading and writing to that specific section of the CFrame's values.
Let's look at some example CFrames :
Example
CFrame Components
A Part located at (0, 0, 0) with no rotationPart.CFrame = CFrame.new(0,0,0)
0 0 0 10 0 0 1 0 0 0 1
A Part located at (1, 2, 3) with no rotationPart.CFrame = CFrame.new(1,2,3)
1 2 3 10 0 0 1 0 0 0 1
A Part located at (0, 0, 0) with (90, 0, 0) rotationPart.CFrame = CFrame.new(0,0,0) * CFrame.Angles(math.rad(90), 0, 0)
0 0 0 10 0 0 A-1 0 1 A
A Part located at (0, 0, 0) with (0, 90, 0) rotationPart.CFrame = CFrame.new(0,0,0) * CFrame.Angles(0, math.rad(90), 0)
0 0 0 A0 1 0 10 -1 0 A
A Part located at (0, 0, 0) with (0, 0, 90) rotationPart.CFrame = CFrame.new(0,0,0) * CFrame.Angles(0, 0, math.rad(90))
0 0 0 A-1 0 1 A0 0 0 1
A Part located at (1, 2, 3) with (90, 90, 90) rotationPart.CFrame = CFrame.new(1,2,3) * CFrame.Angles(math.rad(90), math.rad(90), math.rad(90))
1 2 3 10 A A B-1 0 1 B
Terms
Values
A
-4.3711388286738e-08
B
1.9106854651647e-15
In your first code sample, you are setting the Position first. This preserves the original CFrame, and updates just the values for Position.
-- imagine that p_partnew.CFrame looks like this :
-- ? ? ? ?
-- ? ? ? ?
-- ? ? ? ?
-- set just the position values in the CFrame, keep everything else
p_partnew.Position = Vector3.new(i, p_coord_y, p_coord_z)
-- p_partnew.CFrame now looks like this :
-- i p_coord_y p_coord_z ?
-- ? ? ? ?
-- ? ? ? ?
-- apply a transformation of angles
p_partnew.CFrame = p_partnew.CFrame * CFrame.Angles(p_angles_x, p_angles_y, p_angles_z)
In the second code sample, you are setting the entire CFrame first with just the position values. This wipes out all the other data that existed in that CFrame before.
-- set the entire CFrame
p_partnew.CFrame = CFrame.new(i, p_coord_y, p_coord_z)
-- p_partnew.CFrame now looks like this :
-- i p_coord_y p_coord_z 1
-- 0 0 0 1
-- 0 0 0 1
-- apply a transformation of angles
p_partnew.CFrame = p_partnew.CFrame * CFrame.Angles(p_angles_x, p_angles_y, p_angles_z)
So if the first example works with rotation, but the second doesn't, then the answer is that the original rotation information is getting lost when you set the CFrame. You could try saving that information first, then applying it to the new position, and then applying your changes (assuming that your changes are small increments). That would look something like this :
-- store the previous orientation
local o = p_partnew.Orientation
-- create a set of changes based on new angles
local angles = CFrame.Angles(math.rad(o.X) + p_angles_x, math.rad(o.Y) + p_angles_y, math.rad(o.Z) + p_angles_z)
-- set the new CFrame
p_partnew.CFrame = CFrame.new(i, p_coord_y, p_coord_z):ToWorldSpace(angles)

Related

Use candle stick count rather than resolution - Pine Script

Looking for a simple way to set my input for a cumulative volume range indicator to a specified amount of CANDLES rather than selecting a RESOLUTION to look back at.
is_new_day = change(time(cTimeFrame)) != 0 ? 1 : 0
cnt_new_day = barssince(is_new_day)
// Accumulation
cvol = 0.0
for i = 0 to cnt_new_day
cvol := cvol + volume[i]
plot(cvol, "Cumulative Volume", style=plot.style_columns, color= color.yellow)
You need to accumulate volume in one variable and reset it every N bar
//#version=5
indicator("My Script")
length = input.int(1, "Length", minval=1)
cvol = volume
cvol += bar_index % length == 0 ? 0 : cvol[1]
plot(cvol, "Cumulative Volume", style=plot.style_columns, color= color.yellow)

prevent gimbal lock on angle-axis transformation with rotation of 90 degree in y-axis

I would like to put a sticker on the surface of target. I use a depth cam and a robotics arm.
The setting is like this.
I know there will be gimbal lock on y axis because the target is parallel to z axis.
M1: cam2targetT:
0.933286 -0.358041 0.0279849 -0.250368
-0.162293 -0.489985 -0.85649 0.0660773
0.320371 0.794808 -0.515404 1.03789
0 0 0 1
M2: arm2camT:
0 0.707107 0.707107 0.1
1 0 0 0.53
0 0.707107 -0.707107 0.96
0 0 0 1
Mf: arm2tagetT:
0.111778 0.215542 -0.970075 0.880623
0.933286 -0.358041 0.0279849 0.279632
-0.341295 -0.908486 -0.241184 0.272825
0 0 0 1
When i send the command to UR hand, it is in this format.
I used this
Eigen::AngleAxisd newAngleAxis(mat);
std::cout << newAngleAxis.angle() << "\n" << newAngleAxis.axis() << "\n\n";
and i got this:
3.87376
0.700441
0.470301
-0.536842
The final command to UR is this pose.
Mf in axis-angle T:
0.880623,0.279632,0.272825
AngleAxisd R:
2.71334,1.82184,-2.0796
And as expected, gimbal lock happened, so what question is, how can I find the correct angle-axis in this case? is the problem can be solved by quaternions and how?
P.S. I don't think I can change the rotation order in UR.

how to properly count the number of vehicles?

I have a circle of radius 10 m. I want to count the number of vehicles entering the circle it (the distance from the center car <= 10m)
I'm right . I can use the toolbar "Minitor" to count the number of vehicles currently in liquidation xe.nhung "minitor" much larger than the actual number of vehicles that pass through the circle. I attached the "minitor" by "total-cars".
how to properly count the number of vehicles?
ask cars
[
if distancexy 0 0 < 10
[
set total-cars (total-cars + 1)
]
]
I am not very sure about your question, but maybe this code could help you:
set total-cars count cars with [distancexy 0 0 <= 10]
You can use the following code in the monitor control directly:
count cars with [distancexy 0 0 <= 10]
import cv2
import time
bgsMOG = cv2.createBackgroundSubtractorMOG2(detectShadows=False)
kernal=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
cap = cv2.VideoCapture(0)
counter =0
time.sleep(2)
if cap:
while True:
ret, frame = cap.read()
if ret:
#fgmask = bgsMOG.apply(frame, None, 0.01)
blur = cv2.GaussianBlur(frame, (5, 5), 0)
fgmask = bgsMOG.apply(blur)
morhpho = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernal)
#line for detection
cv2.line(frame,(20,270),(320,270),(175,175,0),5)
_,contours, hierarchy = cv2.findContours(morhpho,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
ax1=20 #coordinate of line where vehicle will be count if intersect
ay1=270
ax2=320
ay2=270
try: hierarchy = hierarchy[0]
except: hierarchy = []
#for contour, hier in zip(contours, hierarchy):
for (i, contour) in enumerate(contours):
(x,y,w,h) = cv2.boundingRect(contour)
if w > 20 and h > 25:
rec=cv2.rectangle(frame, (x,y), (x+w,y+h), (180, 0, 0), 1)
x1=w/2 #to find centroid
y1=h/2
cx=x+x1
cy=y+y1
centroid=(cx,cy)
M = cv2.moments(contour)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# draw the contour and center of the shape on the image
cv2.circle(frame, (cX, cY), 2, (255, 255, 255), -1)
cv2.circle(frame,(int(cx),int(cy)),1,(0,255,0),-1)
dy=cY-270 #my first code to increase counter
print("centroid",cX,":",cY)
if dy==0:
if (cX<=320)and(cX>=20):
counter=counter+1
print("1st ct",counter)
print len(contour)
#FileName = "D:/data/" + str(y) + ".jpg"
#cv2.imshow("cropped",rec)
#cv2.imwrite(FileName,rec)
if cy==270:
if centroid > (27, 268) and centroid < (325, 285):
if (cX <= 320) and (cX >= 20):
counter =counter+1
print "counter=", counter
if cY > 10 and cY < 250:
cv2.putText(frame, str(counter),(10,150),cv2.FONT_HERSHEY_SIMPLEX,2, (255, 0, 0), 1, True)
#cv2.resizeWindow('Output',320,180)
cv2.imshow('Output', frame)
cv2.imshow('mor', morhpho)
cv2.imshow('blur', blur)
#cv2.imshow('FGMASK', morhpho)
key = cv2.waitKey(1)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Tuning Gabor filter

I am trying to seek out pathologies in pictures of noisy vertical stacked layers
with Gabor filtering. For each column, i regard the neigborhood with 10 pixels to the left and right and filter the part of the image with the gabor kernel. Then I take the frobenious norm, so that I have for each column a scalar value.
Here is my result using that image posted below. For me it seems counterintuitive that the response of 0 degree is that much higher than the response of 45 degrees.
But the desired effect is satisfied, meaning that i can state a condition such that the pathology near the 300th column is hit using that the value of 0 degrees is below the value of 45 degrees.
I expected the other way round or is my image just too noisy?
So my questions are: How can I refine the parameter lambda and gamma to maximize the effect when the structure of vertcal stacked layers are broken?(in the middle of the picture around column 290 - 320)
When I tried to change parameters I got too much false positives such that i can not distinguish anymore.
And how can it be that the values of 0 degree is that greater than the filter response of 45 degrees? For me it seems very odd considering that image.
================
Here is the image
Here is my code
windowRadius = 10;
bw = 1;
for k=0:23
theta(k+1)= k*pi/12;
end
psi = [0 pi/2];
lambda = 8; % std value 8
gamma = 0.5; % std value 0.5
for colIndx=1: size(Img,2)
if colIndx-windowRadius < 1
left = 1;
else
left = colIndx - windowRadius;
end
if colIndx+windowRadius > size(Img,2)
right = size(Img,2);
else
right = colIndx + windowRadius;
end
for i=1:length(theta)
gb{i} = gabor_fn(bw,gamma,psi(1),lambda,theta(i)) ...
+ 1i * gabor_fn(bw,gamma,psi(2),lambda,theta(i));
end
gabor_out0deg{colIndx} = imfilter(Img(:, left : right),gb{1},'symmetric');
gabor_out45deg{colIndx} = imfilter(Img(:, left : right),gb{4},'symmetric');
gabor_out90deg{colIndx} = imfilter(Img(:, left : right),gb{7},'symmetric');
gaborFroNorm0deg(colIndx) = norm(gabor_out0deg{colIndx},'fro') / ((right - left) * size(Img,1));
gaborFroNorm45deg(colIndx)= norm(gabor_out45deg{colIndx},'fro') / ((right - left) * size(Img,1));
gaborFroNorm90deg(colIndx)= norm(gabor_out90deg{colIndx},'fro') / ((right - left) * size(Img,1));
end

How to swap negative rotation values over to positive rotation values?

Example: I have a circle which is split up into two halfs. One half goes from 0 to -179,99999999999 while the other goes from 0 to 179,99999999999. Typical example: transform.rotation.z of an CALayer. Instead of reaching from 0 to 360 it is slip up like that.
So when I want to develop a gauge for example (in theory), I want to read values from 0 to 360 rather than getting a -142 and thinking about what that might be on that 0-360 scale.
How to convert this mathematically correctly? Sine? Cosine? Is there anything useful for this?
Isn't the normalization achieved by something as simple as:
assert(value >= -180.0 && value <= +180.0);
if (value < 0)
value += 360.0;
I'd probably put even this into a function if I'm going to need it in more than one place. If the code needs to deal with numbers that might already be normalized, then you change the assertion. If it needs to deal with numbers outside the range -180..+360, then you have more work to do (adding or subtracting appropriate multiples of 360).
while (x < 0) {
x = x + 360;
}
while (x > 360) {
x = x - 360;
}
This will work on any value, positive or negative.
((value % 360) + 360) % 360
The first (value % 360) makes it to -359 to 359.
The + 360 removes any negative number: Value now 1 to 719
The last % 360 makes it to 0
to 359
Say x is the value with range (-180, 180), y is the value you want display,
y = x + 180;
That will change shift reading to range (0, 360).
If you don't mind spending a few extra CPU cycles on values that are already positive, this should work on any value -360 < x < 360:
x = (x + 360) % 360;
I provide code to return 0 - 360 degree angle values from the layer's transform property in this answer to your previous question.