I am building an underwater drone with a gimbled camera, I thought I had the X-Box controller working last night, but today the same code seems to fail. I see I am using pin6 twice opps
Here is a link to a ROV with the same motor setup:
http://www.robotshop.com/en/hydroview-plus-remote-underwater-vehicle.html
The goal is to run the three motors in forward and reverse and run two servos for the camera.
This ran last night, now gives me no data. and any little changes change the available returns fron the joystick. very anoying.
port = "COM4"
servoPin = 6
joystickIndex = 7
servoSpeed = 0.7
servoDirection = 0
servoMin = 0
servoMax = 180
joystick = Runtime.createAndStart("joystick", "Joystick")
arduino = Runtime.createAndStart("arduino", "Arduino")
servoY = Runtime.createAndStart("servoY", "Servo")
servoZ = Runtime.createAndStart("servoZ", "Servo")
rightMotor = Runtime.createAndStart("rightMotor", "Motor")
leftMotor = Runtime.createAndStart("lefttMotor", "Motor")
rearMotor = Runtime.createAndStart("rearMotor", "Motor")
arduino.motorAttach("rightMotor",3,4)
arduino.motorAttach("leftMotor",6,7)
arduino.motorAttach("rearMotor",8,9)
# start the service
joystick = Runtime.start("joystick","Joystick")
print(joystick.getControllers())
python.subscribe("joystick","publishJoystickInput")
joystick.setController(5)
def onJoystickInput(data):
print(data, data.id, data.value)
if data.id == u'6':
print("button 6 is ", data.value)
if data.id == u'z':
print("axis z is ", data.value)
sometimes this works, sometimes it don't, any ideas?
making progress I think
This seems to work 100% of the time as long as I restart mrl every time. It took a while to get rx to work, but now all 5 .data's are captured properly. Now to drive servo's and motors with them.
can not write to a closed port?
Theres a new one. When I connected to motor controller I get an error "can not write to a closed port"
You can try to use a code
You can try to use a code similar to this, it worked on 2105 last tested:
https://github.com/MyRobotLab/pyrobotlab/blob/master/home/kakadu31/Cont…
thanks, question
what does the abs in: "if (abs(joyX) <= 5):" do?
when i reckon right it
when i reckon right it jchecks if the position of the X-joystick as an absolute value is <= 5, so the direction doesnt matter but only the deflection.
PS: the code definately needs some clean up, i think i messed around with definitions of variables etc
fixed closed com port problem
arduino
=
Runtime.start(
"arduino"
,
"Arduino"
)
arduino.connect(
"COM4"
)
setPwmPins vs setPwrDirPins / pwm enabled pins
Hi
What type of Arduino are you using ? Not all pins can be used for PWM.
On the Arduino Uno pins 3,5,6,9,10 and 11 can be used.
On the Arduino Mega pins 2-13 and 44-46 can be used.
For other boards please check the reference below
References
https://www.arduino.cc/en/Reference/AnalogWrite
If you use setPwmPins(pinA, PinB) both pins needs to be able to send pwm.
Depending on the motordiriver you use, you can alternativley use
setPwrDirPins(pinA, PinB)
In this case the pinA needs to be a pwm pin, but pinB can be a digital pin.
error
I am using the right pins, when I add Dir to the command I got an error saying service.motorObject has no attribute setPwmDirPins
thank
setPwrDirPins
My misstake
the alternative should be
setPwrDirPins(pinA,pinB)
I don't know why one uses pwm ( like in PulseWidth Modultation ) and the other pwr ( like in Power )
One little letter can make some differnence
no error now but motor still not running
no error now but motor still not running
fixed closed com port problem, fixed setPwrDirPins, still not mo
just the basics, motor still does not turn
direct arduino control fails, if my code is right.
Example python code to move a motor
The latest build works with the following python to move a motor:
found this on youtube
I ordered 2, the one I am testing is indeed missing r&, but the other has it, going to try it now
OK this works!
Had a bad board and while testing it over wrote mrl, so took some time, but this code does work as intended!
The joystick controls both speed and direction of first motor!....
OK trying to turn!
github is for code, not blog posts.
i've moved your code to github , added comments , and fixed the syntax. I did not test it.. so maybe i had a typo.. but it should give you enough to go on (hopefully)
You can find the script here:
https://github.com/MyRobotLab/pyrobotlab/blob/master/home/wkinne/rov.py
the link goes to a site that
the link goes to a site that asks for 7000 dollars!!!!! for the thing - I assume you get a seat inside it then and need no joystick????????
yes they are expensive!
thats why I'm building one.
lagg
hey kw, your code to do the x y math causes lagg, a lot of it. Might have to find another way : (
it can get two and three seconds behind.
update frequency
So I was thnking about it.. the issue is definitley not the "math".. but rather how many updates are being sent to the motors. Every time any button is pushed in this code, it was updating both the motor speeds. This was quite ineffecient and as a result, the messages were getting backed up when being sent to the arduino.
the fix is to either downsample the updates to the motor to a rate that the arduino can handle, or some other logic that says only update when you see that the values for x and y have changed....
I checked in a change that will only update the motor speeds when the x or y values were updated.. i didn't test, but i suspect this will do the trick for you..
Thanks!
Yup, that got rid of the lagg, thank you!
I think this all works!