Please select Into the mobile phone version | Continue to access the computer ver.
Mecanum Wheels Algorithm - Let the fun begin
2878 19 2019-7-27
Uploading and Loding Picture ...(0/1)
o(^-^)o
MarkusXL
Second Officer
United States
Offline

Works like a champ!

Just enter your desired angle, in degrees, of your desired direction of chassis translation.

Enter you desired max RPM for any wheel.  Enter your Turning Factor as Vo.  Keep it less than 4.0 or so to avoid rapid spinning.

This program will translate the chassis at that exact angle by giving each wheel the exactly correct RPM setting.

Now if thing is going to be any real fun, it will have to quickly take a variable direction and speed, according to yet another algorithm.


from math import sin, cos, radians, pi
'''
Mecanum Wheel Algorithm
VX is the RPM for each Wheel X
Vd is a constant for now - the max wheel RPM you want for this run.
T0 is Theta Zero is the angle in degrees of the desired direction of
translation, hopefully to be variable?  Zero is straight ahead.
This Theta value must be converted to radians for the trig functions.
Vo is the Turning Factor - keep it less that 4 to avoid fast spins
'''
T0 = 45  # Enter your angle in degrees for direction
Vd = 20  # Enter your max Wheel RPM
Vo = 0    # Turning Factor - use low numbers to avoid spinning, 0 is straight

V1 = Vd * (sin(radians(T0) + pi / 4) + Vo)
V2 = Vd * (cos(radians(T0) + pi / 4) - Vo)
V3 = Vd * (cos(radians(T0) + pi / 4) + Vo)
V4 = Vd * (sin(radians(T0) + pi / 4) - Vo)

chassis_ctrl.set_wheel_speed(V1, V2, V3, V4)
time.sleep(5)
chassis_ctrl.set_trans_speed(0)


2019-7-27
Use props
Malibu Aerial
Second Officer
Flight distance : 143898 ft
United States
Offline

Thanks for sharing.
2019-7-27
Use props
rhoude57 - YUL
Second Officer
Canada
Offline

This is way COOL!  Thanks for sharing!
2019-7-27
Use props
MarkusXL
Second Officer
United States
Offline

Thanks!  Enjoy!  It's just another small block of the puzzle.  The command chassis_ctrl.set_wheel_speed(V1, V2, V3, V4) is pretty useless without the math to properly adjust the RPM ratios for all the wheels.

I've been playing with those 3 variables - you can some interesting tricks with it out of the box.

It took me about 6 hours, it wasn't working, THEN I remembered I had to convert to radians.  It was a back-to-school moment.
2019-7-27
Use props
DJI Stephen
Super Moderator

Offline

Hello and good day MarkusXL. Thank you for sharing these algorithm and information to us. Have a safe and happy driving always.
2019-7-28
Use props
MarkusXL
Second Officer
United States
Offline

DJI Stephen Posted at 7-28 07:32
Hello and good day MarkusXL. Thank you for sharing these algorithm and information to us. Have a safe and happy driving always.

Thanks!  The S1 is a very fun aid to learn programming, I wish I had it when I was a kid, and I wish more kids could get into programming with such edu-bots.

Another code improvement!  Here is the Mechanum Wheel Algorithm in the form of a Python Function.

Now just copy and paste into your own programs.  Just "feed" the Function with the 3 inputs plus one for how much time you want to move and use it all over!   Happy autonomous driving!

from math import sin, cos, radians, pi


Vd = 5
T0 = 0
Vo = 0
Tm = 5

def mechanum(Vd, Vo, T0):
    V0 = Vd * (sin(radians(T0) + pi / 4) + Vo)
    V1 = Vd * (cos(radians(T0) + pi / 4) - Vo)
    V2 = Vd * (cos(radians(T0) + pi / 4) + Vo)
    V3 = Vd * (sin(radians(T0) + pi / 4) - Vo)
    return [V0, V1, V2, V3]

V = mechanum(Vd, Vo, T0)
chassis_ctrl.set_wheel_speed(V[0], V[1], V[2], V[3])
time.sleep(Tm)
2019-7-28
Use props
DJI Stephen
Super Moderator

Offline

You are very much welcome MarkusXL and thank you for these additional information you have shared. Amazing work.
2019-7-29
Use props
xiaoyuejin
lvl.2
United States
Offline

"from math import sin, cos, radians, pi"...

How do you know if there is a "math" package that you can import to begin with? Is there a hidden list of pre-installed packages somewhere?

2019-7-29
Use props
MarkusXL
Second Officer
United States
Offline

xiaoyuejin Posted at 7-29 23:40
"from math import sin, cos, radians, pi"...

How do you know if there is a "math" package that you can import to begin with? Is there a hidden list of pre-installed packages somewhere?

LOL JUST TRY IT

That's what I did.  Just get into the app | Lab | DIY Programming | Python (beta)

and start coding!
2019-7-30
Use props
fansc61ebd52
lvl.2
United States
Offline

So basically all you do is reading the sample code, then reverse-engineering to isolate the control functions, then try out the packages that you think should be there... Been there, done that. Don't want to do it again...
2019-7-30
Use props
MarkusXL
Second Officer
United States
Offline

I've been thinking about a short program that attempts to import various modules and report...

Maybe we can generate a complete list of available modules - but actually DJI should print out this list for us.

Moderator?


Googling the problem (probably all developers greatest tool) reveals this:

https://blog.revathskumar.com/20 ... ages-installed.html

2019-7-31
Use props
MarkusXL
Second Officer
United States
Offline

Oh man this is a drag - -

I poked around and found we are very lucky to be able to import math.

And we can import random...  and we can import time...

But, we cannot import cmath...  So I tried some more, going against a list of standard Python modules...

We cannot import itertools, or enum, errno, decimal, fractions,gc, glob,io, json, logging, modulefinder, numbers, operators, parser, pickle, pipes, string, struct, sys*, t*, u*, v*,w*,x*,y*,z*.

In other words, our available modules are quite limited.
2019-7-31
Use props
DJI Panda
Administrator
Flight distance : 405039 ft
  • >>>
China
Offline

Nicely done! I highlighted this post for more users to see.
Thanks for sharing.
2019-7-31
Use props
MarkusXL
Second Officer
United States
Offline

Your'e welcome!

Maybe some of these modules can be added in a future update!

I would really like to see enum, itertools, decimal, fractions, numbers, operators, strings, and pipes added at some point.  Of course, none of the html or ssl or external file machinery makes sense for the S1, but these others allow for more tricky operations by experienced developers or by advanced students looking to expand their abilities.
2019-8-1
Use props
gpvillamil
Second Officer
Flight distance : 210226 ft
United States
Offline

How is this different from the built-in Python API function? This one lets you set translation speed in x & y, and rotation around the z axis.

Python API:
Function: chassis_ctrl.move_with_speed(speed_x, speed_y, speed_rotation)
Parameters:
    ● speed_x(float): [0, 3.5] m/s
    ● speed_y(float): [0, 3.5] m/s
    ● speed_rotation(int): [-600, 600]°/s

And a similar one to drive the chassis at a given direction and speed:

Function: chassis_ctrl.move_degree_with_speed(speed, degree)
Parameters:
    ● speed(float): [0, 3.5] m/s
    ● degree(int): [-180, 180]°
2019-8-1
Use props
MarkusXL
Second Officer
United States
Offline

It would be interesting to compare the actual differences in movement.  Clearly, when you use manual control of the S1 with the app, the wheels never bind, so the mecanum wheels equations must be built in and available to their api.  Having this algorithm available for our own code allows the use of  chassis_ctrl.set_wheel_speed(V[0], V[1], V[2], V[3])  which is different from the other commands you mention in that it specifies individual wheel RPM.  I suspect this will allow for some fancy maneuvers with cleaner code, but that is just a hunch at this point.
2019-8-1
Use props
rhoude57 - YUL
Second Officer
Canada
Offline

MarkusXL Posted at 8-1 14:16
It would be interesting to compare the actual differences in movement.  Clearly, when you use manual control of the S1 with the app, the wheels never bind, so the mecanum wheels equations must be built in and available to their api.  Having this algorithm available for our own code allows the use of  chassis_ctrl.set_wheel_speed(V[0], V[1], V[2], V[3])  which is different from the other commands you mention in that it specifies individual wheel RPM.  I suspect this will allow for some fancy maneuvers with cleaner code, but that is just a hunch at this point.

I hope this thread is being sent to the Python (Beta) IDE development team. From the Beta status, we can imply there will be changes made to it.
Let'S hope our suggestions will be taken into account.
2019-8-7
Use props
ryanroy837
lvl.1

India
Offline

thanks all for your responses. i was having similar questions and your answers were helpful to me.
2019-10-22
Use props
JB63
Second Officer
  • >>>
United States
Offline

Very nice. Was just wondering ... Does the S! have a chassis-mounted IMU? If so, one can compare actual trajectory (integrating IMU signals) to that commanded by the wheels. If there is a difference, then a correction is to take place ... and before we know it, we'd need a Kalman filter implementation ... some day.
2019-10-22
Use props
Duane Degn
First Officer
Flight distance : 598415 ft
United States
Offline

JB63 Posted at 10-22 07:27
Very nice. Was just wondering ... Does the S! have a chassis-mounted IMU? If so, one can compare actual trajectory (integrating IMU signals) to that commanded by the wheels. If there is a difference, then a correction is to take place ... and before we know it, we'd need a Kalman filter implementation ... some day.

"Does the S! have a chassis-mounted IMU?"
The S1 has two IMUs. One is on the Motion Controller and one is on the PCB which controls the gimbal tilt motor.
The IMU is covered with some soft white material. I'm pretty sure the white material is used to reduce the IMU's temperature fluctuations. Apparently the gyros drift less if the temperature remains constant. You can also see (on the left side of the image) the PCB has been cut away to provide additional thermal isolation.

Here's a photo of the gimbal PCB with the IMU.

A different thermal material is used on this sensor.
I'm not aware of a way to access the IMU data from within a program.
2019-10-22
Use props
Advanced
You need to log in before you can reply Login | Register now

Credit Rules