0

I have been working on a small project of mine. It's pretty much a robot that goes forward using a gyroscope. I am aiming for very high accuracy in this case.

I have two commands, which stop the motors. The problem is that there's a small delay between the two and I constantly get 3-5mm errors on the same motor (the one that gets stopped one line later).

Due to the nature of my program, I cannot tell that specific motor to 'do 3-5 mm less'. Is there a way I could speed this up in any way?

def stop_motors():
    p1 = Process(target=stop_left());
    p2 = Process(target=stop_right());
    p1.start();
    p2.start();

def stop_left():
    MotorLeft.stop(stop_action='hold');
    MotorLeft.reset();

def stop_right():
    MotorRight.stop(stop_action='hold');
    MotorRight.reset();

I tried this, same results.

  • How are you sending those two commands? – John Anderson May 14 '18 at 21:36
  • 1
    you could perform the motor stop in 2 threads, synchronized on an event. That'd be better if the GIL doesn't get in the way – Jean-François Fabre May 14 '18 at 21:36
  • 1
    [This](https://stackoverflow.com/a/18865028/1586200) could help. – Autonomous May 14 '18 at 21:36
  • 5
    If you need real-time responsiveness, Python may not be the right language for the job. Yes, it supports threading, but no, using threading won't *guarantee* that your timing is what you want it to be. – Charles Duffy May 14 '18 at 21:37
  • Currently for testing I import a .py file directly into the python interpreter (using `;` to split the lines, why? because I am too impatient to wait for that frustrating 7 seconds in between syncing, and all the startup phase of my program) – user9549355 May 14 '18 at 21:38
  • 1
    @ParagS.Chandakkar yes, but the overhead of spawning 2 new processes may aggravate the delay for both motors. Threads would be better (same principle) – Jean-François Fabre May 14 '18 at 21:38
  • 1
    How much work is done in these two commands? They likely do some I/O which usually releases the GIL and lets another thread run. In that case, threads would likely work. – tdelaney May 14 '18 at 21:38
  • add some code/your attempt to your question to make it more accurate, and you'll have my vote (and maybe answers) – Jean-François Fabre May 14 '18 at 21:39
  • @Jean-FrançoisFabre I am working on what Parag S. ... said, that looks promising. – user9549355 May 14 '18 at 21:40
  • Do these two commands need to be sent in order? – tdelaney May 14 '18 at 21:41
  • yes, try with threads too, response time will be better – Jean-François Fabre May 14 '18 at 21:41
  • 1
    (For that matter, *Linux* isn't a RTOS, and doesn't offer real-time guarantees even from the kernel layer; there's a reason people pay money for vxWorks when building embedded systems where timing is mission- or safety-critical). – Charles Duffy May 14 '18 at 21:45
  • What is eating up the several ms? If you are doing more work than just sending the commands, then you may need to prestage everything then send the commands. `cmd1 = gen_stage1_cmd();cmd2 = gen_stage2_cmd();send_cmds((cmd1, cmd2))`. – tdelaney May 14 '18 at 21:47
  • @tdelaney No, I really just want to stop both at the same time. – user9549355 May 14 '18 at 21:47
  • err `target=stop_left()` should be `target=stop_left` else it calls the function instead of passing the function pointer. A classic... also drop the semicolons, python doesn't need them – Jean-François Fabre May 14 '18 at 21:48
  • @Jean-FrançoisFabre Yeah I know about the semi colons, I pass all of that through the console so that's why I need them. Maybe using the console is also slowing things down? – user9549355 May 14 '18 at 21:50
  • never mind that, the most important thing is to _drop the parentheses_ read my comment again. – Jean-François Fabre May 14 '18 at 21:50
  • @Jean-FrançoisFabre Just tried that, and it works! Thanks to everyone! – user9549355 May 14 '18 at 21:52

0 Answers0