{"action":"closed","number":502,"pull_request":{"url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls/502","id":213478402,"node_id":"MDExOlB1bGxSZXF1ZXN0MjEzNDc4NDAy","html_url":"https://github.com/ev3dev/ev3dev-lang-python/pull/502","diff_url":"https://github.com/ev3dev/ev3dev-lang-python/pull/502.diff","patch_url":"https://github.com/ev3dev/ev3dev-lang-python/pull/502.patch","issue_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues/502","number":502,"state":"closed","locked":false,"title":"Optimistically check the mode of the sensor before setting it on read","user":{"login":"WasabiFan","id":3310349,"node_id":"MDQ6VXNlcjMzMTAzNDk=","avatar_url":"https://avatars0.githubusercontent.com/u/3310349?v=4","gravatar_id":"","url":"https://api.github.com/users/WasabiFan","html_url":"https://github.com/WasabiFan","followers_url":"https://api.github.com/users/WasabiFan/followers","following_url":"https://api.github.com/users/WasabiFan/following{/other_user}","gists_url":"https://api.github.com/users/WasabiFan/gists{/gist_id}","starred_url":"https://api.github.com/users/WasabiFan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WasabiFan/subscriptions","organizations_url":"https://api.github.com/users/WasabiFan/orgs","repos_url":"https://api.github.com/users/WasabiFan/repos","events_url":"https://api.github.com/users/WasabiFan/events{/privacy}","received_events_url":"https://api.github.com/users/WasabiFan/received_events","type":"User","site_admin":false},"body":"As we've [recently discovered](https://github.com/ev3dev/ev3dev-lang-python/issues/498), some sensors don't respond well to having their mode set repeatedly<sup>1</sup>. Here's my proposed fix, which I think simultaneously improves sensor operation all-around.\r\n\r\n<sup>1: _My guess is that this was intentional on LEGO's part; the beacon button in particular has a delay on it, presumably added on the sensor side to be resilient to momentary losses of line-of-sight. That timer is probably reset every time we write the mode._</sup>\r\n\r\n---\r\n\r\nI did some benchmarking with an IR sensor (UART):\r\n\r\n```\r\nrobot@ev3dev:~/ev3dev-lang-python$ python3 -m timeit -s \"import ev3dev2.sensor; s = ev3dev2.sensor.Sensor(ev3dev2.sensor.INPUT_3)\" \"s.mode\"\r\n1000 loops, best of 3: 975 usec per loop\r\n\r\nrobot@ev3dev:~/ev3dev-lang-python$ python3 -m timeit -s \"import ev3dev2.sensor; s = ev3dev2.sensor.Sensor(ev3dev2.sensor.INPUT_3)\" \"s.mode = 'IR-REMOTE'\"\r\n10 loops, best of 3: 97.8 msec per loop\r\n```\r\n\r\nIn other words, reading the mode takes around 1ms, and writing takes 100x that. This is generally what I would expect, given that setting the mode on a UART sensor always requires writing to the serial line and might even wait for a response... I haven't looked into it past a cursory glance at the top-level \"set\" method. I believe reading is just a memory read (+ sysfs overhead).\r\n\r\nFor comparison, here's an analog sensor (the only one IIRC), which only has one mode and presumably no-ops when setting the mode anyway (I didn't bother looking up the driver code for this one):\r\n\r\n```\r\nrobot@ev3dev:~/ev3dev-lang-python$ python3 -m timeit -s \"import ev3dev2.sensor; s = ev3dev2.sensor.Sensor(ev3dev2.sensor.INPUT_3)\" \"s.mode\"\r\n1000 loops, best of 3: 834 usec per loop\r\n\r\nrobot@ev3dev:~/ev3dev-lang-python$ python3 -m timeit -s \"import ev3dev2.sensor; s = ev3dev2.sensor.Sensor(ev3dev2.sensor.INPUT_3)\" \"s.mode = 'TOUCH'\"\r\n1000 loops, best of 3: 827 usec per loop\r\n````\r\n\r\nSo, this one is around 1ms for both operations. I'm going to guess that this is just sysfs file I/O overhead.\r\n\r\nI didn't see much use in finding an I2C sensor, given that pretty much all of the EV3 ones are UART. It's probably somewhere in between.\r\n\r\n---\r\n\r\nWhat this tells me is that we wait 100ms every time you read a sensor, for no good reason. Given that reading the mode is only 1ms, it seems reasonable to me that we should do the read and only write if we need it to change. In most cases, that's probably the right assumption. Technically, this does introduce a potential race condition, but I think the risk is minor.\r\n\r\n I've included that in this PR, and added some related tests.\r\n\r\nFixes #498 ","created_at":"2018-09-06T02:29:45Z","updated_at":"2018-09-12T23:53:20Z","closed_at":"2018-09-12T23:53:20Z","merged_at":"2018-09-12T23:53:20Z","merge_commit_sha":"be77ea3bf4f4ac219ec03d5da4452fece9729746","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls/502/commits","review_comments_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls/502/comments","review_comment_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues/502/comments","statuses_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/statuses/a65e644c414dd62f1767644f023e22f52ee8212e","head":{"label":"ev3dev:set-mode-when-needed","ref":"set-mode-when-needed","sha":"a65e644c414dd62f1767644f023e22f52ee8212e","user":{"login":"ev3dev","id":6878323,"node_id":"MDEyOk9yZ2FuaXphdGlvbjY4NzgzMjM=","avatar_url":"https://avatars2.githubusercontent.com/u/6878323?v=4","gravatar_id":"","url":"https://api.github.com/users/ev3dev","html_url":"https://github.com/ev3dev","followers_url":"https://api.github.com/users/ev3dev/followers","following_url":"https://api.github.com/users/ev3dev/following{/other_user}","gists_url":"https://api.github.com/users/ev3dev/gists{/gist_id}","starred_url":"https://api.github.com/users/ev3dev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ev3dev/subscriptions","organizations_url":"https://api.github.com/users/ev3dev/orgs","repos_url":"https://api.github.com/users/ev3dev/repos","events_url":"https://api.github.com/users/ev3dev/events{/privacy}","received_events_url":"https://api.github.com/users/ev3dev/received_events","type":"Organization","site_admin":false},"repo":{"id":43603757,"node_id":"MDEwOlJlcG9zaXRvcnk0MzYwMzc1Nw==","name":"ev3dev-lang-python","full_name":"ev3dev/ev3dev-lang-python","owner":{"login":"ev3dev","id":6878323,"node_id":"MDEyOk9yZ2FuaXphdGlvbjY4NzgzMjM=","avatar_url":"https://avatars2.githubusercontent.com/u/6878323?v=4","gravatar_id":"","url":"https://api.github.com/users/ev3dev","html_url":"https://github.com/ev3dev","followers_url":"https://api.github.com/users/ev3dev/followers","following_url":"https://api.github.com/users/ev3dev/following{/other_user}","gists_url":"https://api.github.com/users/ev3dev/gists{/gist_id}","starred_url":"https://api.github.com/users/ev3dev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ev3dev/subscriptions","organizations_url":"https://api.github.com/users/ev3dev/orgs","repos_url":"https://api.github.com/users/ev3dev/repos","events_url":"https://api.github.com/users/ev3dev/events{/privacy}","received_events_url":"https://api.github.com/users/ev3dev/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ev3dev/ev3dev-lang-python","description":"Pure python bindings for ev3dev","fork":false,"url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python","forks_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/forks","keys_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/teams","hooks_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/hooks","issue_events_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues/events{/number}","events_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/events","assignees_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/assignees{/user}","branches_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/branches{/branch}","tags_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/tags","blobs_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/refs{/sha}","trees_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/statuses/{sha}","languages_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/languages","stargazers_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/stargazers","contributors_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/contributors","subscribers_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/subscribers","subscription_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/subscription","commits_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/commits{/sha}","git_commits_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/commits{/sha}","comments_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/comments{/number}","issue_comment_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues/comments{/number}","contents_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/contents/{+path}","compare_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/merges","archive_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/downloads","issues_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues{/number}","pulls_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls{/number}","milestones_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/milestones{/number}","notifications_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/labels{/name}","releases_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/releases{/id}","deployments_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/deployments","created_at":"2015-10-03T15:32:20Z","updated_at":"2018-09-12T23:52:12Z","pushed_at":"2018-09-12T23:53:20Z","git_url":"git://github.com/ev3dev/ev3dev-lang-python.git","ssh_url":"git@github.com:ev3dev/ev3dev-lang-python.git","clone_url":"https://github.com/ev3dev/ev3dev-lang-python.git","svn_url":"https://github.com/ev3dev/ev3dev-lang-python","homepage":null,"size":9715,"stargazers_count":194,"watchers_count":194,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":73,"mirror_url":null,"archived":false,"open_issues_count":38,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":73,"open_issues":38,"watchers":194,"default_branch":"ev3dev-stretch"}},"base":{"label":"ev3dev:ev3dev-stretch","ref":"ev3dev-stretch","sha":"b59dd403fa73a1a28299bf987fb700562fce5876","user":{"login":"ev3dev","id":6878323,"node_id":"MDEyOk9yZ2FuaXphdGlvbjY4NzgzMjM=","avatar_url":"https://avatars2.githubusercontent.com/u/6878323?v=4","gravatar_id":"","url":"https://api.github.com/users/ev3dev","html_url":"https://github.com/ev3dev","followers_url":"https://api.github.com/users/ev3dev/followers","following_url":"https://api.github.com/users/ev3dev/following{/other_user}","gists_url":"https://api.github.com/users/ev3dev/gists{/gist_id}","starred_url":"https://api.github.com/users/ev3dev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ev3dev/subscriptions","organizations_url":"https://api.github.com/users/ev3dev/orgs","repos_url":"https://api.github.com/users/ev3dev/repos","events_url":"https://api.github.com/users/ev3dev/events{/privacy}","received_events_url":"https://api.github.com/users/ev3dev/received_events","type":"Organization","site_admin":false},"repo":{"id":43603757,"node_id":"MDEwOlJlcG9zaXRvcnk0MzYwMzc1Nw==","name":"ev3dev-lang-python","full_name":"ev3dev/ev3dev-lang-python","owner":{"login":"ev3dev","id":6878323,"node_id":"MDEyOk9yZ2FuaXphdGlvbjY4NzgzMjM=","avatar_url":"https://avatars2.githubusercontent.com/u/6878323?v=4","gravatar_id":"","url":"https://api.github.com/users/ev3dev","html_url":"https://github.com/ev3dev","followers_url":"https://api.github.com/users/ev3dev/followers","following_url":"https://api.github.com/users/ev3dev/following{/other_user}","gists_url":"https://api.github.com/users/ev3dev/gists{/gist_id}","starred_url":"https://api.github.com/users/ev3dev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ev3dev/subscriptions","organizations_url":"https://api.github.com/users/ev3dev/orgs","repos_url":"https://api.github.com/users/ev3dev/repos","events_url":"https://api.github.com/users/ev3dev/events{/privacy}","received_events_url":"https://api.github.com/users/ev3dev/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ev3dev/ev3dev-lang-python","description":"Pure python bindings for ev3dev","fork":false,"url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python","forks_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/forks","keys_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/teams","hooks_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/hooks","issue_events_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues/events{/number}","events_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/events","assignees_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/assignees{/user}","branches_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/branches{/branch}","tags_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/tags","blobs_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/refs{/sha}","trees_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/statuses/{sha}","languages_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/languages","stargazers_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/stargazers","contributors_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/contributors","subscribers_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/subscribers","subscription_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/subscription","commits_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/commits{/sha}","git_commits_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/git/commits{/sha}","comments_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/comments{/number}","issue_comment_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues/comments{/number}","contents_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/contents/{+path}","compare_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/merges","archive_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/downloads","issues_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues{/number}","pulls_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls{/number}","milestones_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/milestones{/number}","notifications_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/labels{/name}","releases_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/releases{/id}","deployments_url":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/deployments","created_at":"2015-10-03T15:32:20Z","updated_at":"2018-09-12T23:52:12Z","pushed_at":"2018-09-12T23:53:20Z","git_url":"git://github.com/ev3dev/ev3dev-lang-python.git","ssh_url":"git@github.com:ev3dev/ev3dev-lang-python.git","clone_url":"https://github.com/ev3dev/ev3dev-lang-python.git","svn_url":"https://github.com/ev3dev/ev3dev-lang-python","homepage":null,"size":9715,"stargazers_count":194,"watchers_count":194,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":73,"mirror_url":null,"archived":false,"open_issues_count":38,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":73,"open_issues":38,"watchers":194,"default_branch":"ev3dev-stretch"}},"_links":{"self":{"href":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls/502"},"html":{"href":"https://github.com/ev3dev/ev3dev-lang-python/pull/502"},"issue":{"href":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues/502"},"comments":{"href":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/issues/502/comments"},"review_comments":{"href":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls/502/comments"},"review_comment":{"href":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/pulls/502/commits"},"statuses":{"href":"https://api.github.com/repos/ev3dev/ev3dev-lang-python/statuses/a65e644c414dd62f1767644f023e22f52ee8212e"}},"author_association":"MEMBER","merged":true,"mergeable":null,"rebaseable":null,"mergeable_state":"unknown","merged_by":{"login":"WasabiFan","id":3310349,"node_id":"MDQ6VXNlcjMzMTAzNDk=","avatar_url":"https://avatars0.githubusercontent.com/u/3310349?v=4","gravatar_id":"","url":"https://api.github.com/users/WasabiFan","html_url":"https://github.com/WasabiFan","followers_url":"https://api.github.com/users/WasabiFan/followers","following_url":"https://api.github.com/users/WasabiFan/following{/other_user}","gists_url":"https://api.github.com/users/WasabiFan/gists{/gist_id}","starred_url":"https://api.github.com/users/WasabiFan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WasabiFan/subscriptions","organizations_url":"https://api.github.com/users/WasabiFan/orgs","repos_url":"https://api.github.com/users/WasabiFan/repos","events_url":"https://api.github.com/users/WasabiFan/events{/privacy}","received_events_url":"https://api.github.com/users/WasabiFan/received_events","type":"User","site_admin":false},"comments":0,"review_comments":7,"maintainer_can_modify":false,"commits":5,"additions":145,"deletions":61,"changed_files":3}}
{"actor":{"display_login":"WasabiFan"}}