You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
motors API: speed_pct vs speed_dps vs speed_rps #370
While working on #353#360 one thing we did not address was the API for specifying the motor speed in something other than speed_pct. The other two we want are degrees per second and rotations per second.
Some ideas that were floated:
Option 1 How about we make the default units % just like EV3-G, plus introduce scaling factor constants for those who want scientific units.
# run the motor at 50% of max speed
m.run(50)
# run the motor at 400 degrees/second
m.run(400 * m.DPS)
# run the motor at 60 RPM
m.run(60 * m.RPM)
Option 2 why not m.run(speed_pct=50), m.run(dps=400), and m.run(rpm=60)? If we define run as
def run(self, speed_pct=None, dps=None, rpm=None)
We have other parameters that need to be passed in though (speed) so basically everything becomes a positional arg. So the user would do one of
Say I'm a user and I want to rotate a motor for 720 degrees at 360 degrees per second, I'm asking how does that happen with option 3 via a call to m.on_for_degrees(). Today if you call m.on_for_degrees(360) it is going to assert because 360 is speed_pct and it is greater than 100.
m.run_to_rel_position(degrees_sp=720, dps_sp=360) should do it, where dps_sp is another proxy property, but for speed_sp. We can rename (or create an alias) the method to on_for_degrees (I would prefer even simpler and generic on_for though: it would take kwargs dict and it would be useable with degrees_sp or seconds_sp or what not).
Ignore run_to_rel_position, that is what is happening behind the scenes :) So you would have m.on_for_degrees(dps_sp=360, degrees=720)? If so that looks the same as option 2
defon_for(self, **kwargs):
forkey,valinkwargs:
setattr(self, key, val)
ifdegrees_spinkwargs: # this is tricky, need to check for other positional units...self.run_to_rel_position()
eliftime_spinkwargs:
self.run_timed()
...
We can come up with a name more generic than on_for_degrees that would mean 'turn for some rotations units'. rotate_for? Then we can do rotate_for(degrees_sp=720, dps_sp=360), or rotate_for(turns_sp=2, rpm_sp=1), and it would mean the same thing.
EDIT1: And the implementation for it is as simple as
To me using kwargs makes the API much less readable/obvious if you are a new user looking at this for the first time...and that is really who we are trying to target here. Today you can look at the EV3-G motor block and look at def on_for_degrees(self, speed_pct, degrees, brake=True, block=True): and connect the dots pretty quickly on how they are equivalent. If we use kwargs we lose that.
Well, for me the code with kwargs is more readable because it is more concise, and the resulting API is better because it gives you more freedom: you can use whatever meaningful combination of attributes you need, and you are not constrained by the set of positional arguments a developer thought would be enough.
it gives you more freedom: you can use whatever meaningful combination of attributes you need, and you are not constrained by the set of positional arguments a developer thought would be enough
Agreed but we already have that with the current run_XYZ API, that isn't the problem we are trying to solve. To me we don't want crazy amounts of freedom, we want a nice clean/obvious 1:1 mapping with the EV3-G motor blocks.
Just my 2c though...lets see what the others think since you and I are just going back and forth on this.
That is a good point. We can add proxy attributes and keep the current interface. And I think that would also let us implement the new 'simpler' API easier.
I've been working on translating an EV3-G program for the last couple days and I am finding that having the API exactly the same as EV3-G is really the most useful thing to do and perhaps more importantly, it is just really fun.
What I mean by exactly is having positional arguments in the same order as the EV3-G software that take the exact same value. I thought I would like to have fancy extra features or tweak things (like changing names) to make it "better", but I am finding that in practice, those kinds of changes are getting in the way (with the exception of correcting things that are just plain wrong in EV3-G, like using the name speed instead of power). So, I would say put this issue on the back burner and move on to something else. Once we have the basics, we can start using the library and find what we are really missing and add more if needed. By then, we should have a better idea of what feels right instead of just trying to guess like we are now.
My two cents: in addition to what others have said, I think that we should fully commit to this new API surface if we're going to support it (and I'm pretty darn sure we are). My vote would be to remove the old methods given that the new ones are 1:1 replacements for all the normal use cases (or, if they aren't, add optional arguments to make them so).
Please lets not do this until we have the new API fully implemented. My feeling is the purpose of the new API is to make translation from EV3-G to our library simple, but not necessary 1:1. When a nicer API is possible, why not use a nicer API?
I am not saying that what I am proposing is nicer, just lets not do this only to have 1:1 mapping with EV3-G. After all, people move to ev3dev because they are not satisfied with EV3-G.
Strongly suggest you implement an EV3G compatible API as a loadable helper module - leave the basic API as is because it's compatible across many scripting languages
I don't think it hurts for us to keep the old API around...
I do. A user has no way of knowing the difference between run_forever and on, or run_timed and on_for_seconds. With both APIs available, code will be fractured between the two and only be confusing. The two types of methods have entirely different calling patterns and behaviors which isn't clear to any user.
If we are making choices, I'd prefer they are intentional and deliberate so we make sure we don't add to the confusion.
To think of it, how does the user know to look for on_for_seconds as opposed to run_timed? Aren't both of those just arbitrary names we came up for 'green motor block'? If on_for_seconds is somehow canonical, @dlech, are you willing to rename the corresponding action in kernel drivers?
Creating a EV3G compatibility layer in a separate module sounds like a nice idea. Users can import * from ev3dev.ev3g and have just the ev3-g API available.
@dlech, are you willing to rename the corresponding action in kernel drivers?
No. At this point, the only kernel changes I am planning on making are things that make it into the mainline kernel. I would really like to avoid these small breaking changes if at all possible because it breaks all existing ev3dev software.
Creating a EV3G compatibility layer in a separate module sounds like a nice idea. Users can import * from ev3dev.ev3g and have just the ev3-g API available.
This is exactly what I meant in #348. The current ev3dev-lang-python is the "low level" api because it looks and feels like the hardware drivers. You don't want to change it because people are using it. The new "high level" api should look and feel like the EV3-G software. And most importantly, there should be a clear separation between these two. Putting them in separate modules is very clear separation.
In fact, this suggestion of an ev3dev.ev3g module fits well with what I have been thinking in regards to micropython. I'll post those thoughts in ev3dev/ev3dev#937 since it is off topic here.
Activity
ddemidov commentedon Sep 22, 2017
Option 3
(copied from #360 (comment))
Introduce proxy properties for various units. For example, we could have
and then same thing for
rotations_sp,power_sp(percentage of max_speed_sp), etc.dwalton76 commentedon Sep 22, 2017
@ddemidov can you give an example
m.on_for_degrees(...)call with option 3?ddemidov commentedon Sep 22, 2017
I would just call our existing
m.run_to_rel_position(degrees_sp=360).dwalton76 commentedon Sep 22, 2017
How would it be used for
m.on_for_degrees(...)though?ddemidov commentedon Sep 22, 2017
I think
m.run_to_rel_position(degrees_sp=360)is exactlym.on_for_degrees(360), no?dwalton76 commentedon Sep 22, 2017
Say I'm a user and I want to rotate a motor for 720 degrees at 360 degrees per second, I'm asking how does that happen with option 3 via a call to
m.on_for_degrees(). Today if you callm.on_for_degrees(360)it is going to assert because 360 is speed_pct and it is greater than 100.ddemidov commentedon Sep 22, 2017
m.run_to_rel_position(degrees_sp=720, dps_sp=360)should do it, wheredps_spis another proxy property, but forspeed_sp. We can rename (or create an alias) the method toon_for_degrees(I would prefer even simpler and genericon_forthough: it would take kwargs dict and it would be useable withdegrees_sporseconds_spor what not).dwalton76 commentedon Sep 22, 2017
Ignore
run_to_rel_position, that is what is happening behind the scenes :) So you would havem.on_for_degrees(dps_sp=360, degrees=720)? If so that looks the same as option 2ddemidov commentedon Sep 22, 2017
Yes, it looks very much like option 2, but the implementation should is easier. Instead of
we can do
or even
ddemidov commentedon Sep 22, 2017
We can come up with a name more generic than
on_for_degreesthat would mean 'turn for some rotations units'.rotate_for? Then we can dorotate_for(degrees_sp=720, dps_sp=360), orrotate_for(turns_sp=2, rpm_sp=1), and it would mean the same thing.EDIT1: And the implementation for it is as simple as
EDIT2: And that is exactly the implementation of our current
run_to_rel_position.dwalton76 commentedon Sep 22, 2017
To me using kwargs makes the API much less readable/obvious if you are a new user looking at this for the first time...and that is really who we are trying to target here. Today you can look at the EV3-G motor block and look at
def on_for_degrees(self, speed_pct, degrees, brake=True, block=True):and connect the dots pretty quickly on how they are equivalent. If we use kwargs we lose that.ddemidov commentedon Sep 22, 2017
We can put positional arguments into the documentation, but implement it with keyword args.
dwalton76 commentedon Sep 22, 2017
That doesn't solve the problem of having readable/obvious code though :)
ddemidov commentedon Sep 22, 2017
Well, for me the code with kwargs is more readable because it is more concise, and the resulting API is better because it gives you more freedom: you can use whatever meaningful combination of attributes you need, and you are not constrained by the set of positional arguments a developer thought would be enough.
dwalton76 commentedon Sep 22, 2017
it gives you more freedom: you can use whatever meaningful combination of attributes you need, and you are not constrained by the set of positional arguments a developer thought would be enough
Agreed but we already have that with the current run_XYZ API, that isn't the problem we are trying to solve. To me we don't want crazy amounts of freedom, we want a nice clean/obvious 1:1 mapping with the EV3-G motor blocks.
Just my 2c though...lets see what the others think since you and I are just going back and forth on this.
ddemidov commentedon Sep 22, 2017
That is a good point. We can add proxy attributes and keep the current interface. And I think that would also let us implement the new 'simpler' API easier.
dlech commentedon Sep 22, 2017
I've been working on translating an EV3-G program for the last couple days and I am finding that having the API exactly the same as EV3-G is really the most useful thing to do and perhaps more importantly, it is just really fun.
What I mean by exactly is having positional arguments in the same order as the EV3-G software that take the exact same value. I thought I would like to have fancy extra features or tweak things (like changing names) to make it "better", but I am finding that in practice, those kinds of changes are getting in the way (with the exception of correcting things that are just plain wrong in EV3-G, like using the name
speedinstead ofpower). So, I would say put this issue on the back burner and move on to something else. Once we have the basics, we can start using the library and find what we are really missing and add more if needed. By then, we should have a better idea of what feels right instead of just trying to guess like we are now.Also...
WasabiFan commentedon Sep 22, 2017
My two cents: in addition to what others have said, I think that we should fully commit to this new API surface if we're going to support it (and I'm pretty darn sure we are). My vote would be to remove the old methods given that the new ones are 1:1 replacements for all the normal use cases (or, if they aren't, add optional arguments to make them so).
ddemidov commentedon Sep 23, 2017
Please lets not do this until we have the new API fully implemented. My feeling is the purpose of the new API is to make translation from EV3-G to our library simple, but not necessary 1:1. When a nicer API is possible, why not use a nicer API?
I am not saying that what I am proposing is nicer, just lets not do this only to have 1:1 mapping with EV3-G. After all, people move to ev3dev because they are not satisfied with EV3-G.
rhempel commentedon Sep 23, 2017
Strongly suggest you implement an EV3G compatible API as a loadable helper module - leave the basic API as is because it's compatible across many scripting languages
dwalton76 commentedon Sep 23, 2017
I don't think it hurts for us to keep the old API around...it is a little bit more code to maintain but it gives us backward compatibility
dwalton76 commentedon Sep 23, 2017
Will close this one for now but we may re-open it in a few months when we've had time to finish and work with the EV3-G 1:1 API.
WasabiFan commentedon Sep 23, 2017
I do. A user has no way of knowing the difference between
run_foreverandon, orrun_timedandon_for_seconds. With both APIs available, code will be fractured between the two and only be confusing. The two types of methods have entirely different calling patterns and behaviors which isn't clear to any user.If we are making choices, I'd prefer they are intentional and deliberate so we make sure we don't add to the confusion.
ddemidov commentedon Sep 23, 2017
To think of it, how does the user know to look for
on_for_secondsas opposed torun_timed? Aren't both of those just arbitrary names we came up for 'green motor block'? Ifon_for_secondsis somehow canonical, @dlech, are you willing to rename the corresponding action in kernel drivers?Creating a EV3G compatibility layer in a separate module sounds like a nice idea. Users can
import * from ev3dev.ev3gand have just the ev3-g API available.rhempel commentedon Sep 23, 2017
Thanks Denis - that's what I was trying to say. We have users that will be a bit annoyed if we change the API again
dlech commentedon Sep 23, 2017
No. At this point, the only kernel changes I am planning on making are things that make it into the mainline kernel. I would really like to avoid these small breaking changes if at all possible because it breaks all existing ev3dev software.
This is exactly what I meant in #348. The current ev3dev-lang-python is the "low level" api because it looks and feels like the hardware drivers. You don't want to change it because people are using it. The new "high level" api should look and feel like the EV3-G software. And most importantly, there should be a clear separation between these two. Putting them in separate modules is very clear separation.
In fact, this suggestion of an
ev3dev.ev3gmodule fits well with what I have been thinking in regards to micropython. I'll post those thoughts in ev3dev/ev3dev#937 since it is off topic here.