I have two reversable 12volt motors that I would like to control through PWM. I want the speed to be constantly updated by the python code that will be sent through a TCP client-server. I have the python piece of code working very well. I need some help with the Arduino Sketch please. I have all the other pieces of code working except the Arduino Sketch.
I know everything about the hardware required (H-bridge, etc) but I need help with the sketch.
This is the Joystick Control and TCP client:
[code]import pygame
import socket
import sys
import os
pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
print 'Initialized Joystick : %s' % j.get_name()
username = os.name
if username == "nt":
username = "Windows"
print "Welcome commander, you are using " + username
a = 0
b = 0
aa = a
bb = b
i = raw_input("What do you want to detect?")
while i == "button":
pygame.event.pump()
#print j.get_axis(1)
if j.get_button(1):
print "Left"
if j.get_button(0):
print "Kill"
if j.get_button(2):
print "Right"
while i == "pos":
pygame.event.pump()
y = j.get_axis(1) ######################################
y = round(y,2) # Increase to (x, 12) when using PWM!#
x = j.get_axis(0) # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #
x = round(x,2) # Increase to (y, 12) when using PWM!#
z = j.get_axis(2) ######################################
z = round(z,1)
#print(x,y,z)
if y < 0: ############################
y = abs(y) #New Y-axes inversion code!#
elif y > 0: ############################
y = y*-1
#############################################
# Formulas for working out the speed of the #
# motor in certain areas of the Joystick! #
#############################################
f = 1
sy = f*(y)
sx = f*(x)
#############################################
## Whenever the ROBOT is moving and the #
## Co-ordinates are not (0,0) #
#############################################
if x != 0 and y != 0:
sy = abs(sy) #######
if y > abs(x) and y > 0: # Top!#
############
if -1 < x < 0: # Top Left!#
############################################
c = abs(x) # Convert to percentage and positive value!#
a = (sy) - (c) ############################################
b = (sy)
#############
elif 0 < x < 1: # Top Right!#
############################################
d = abs(x) # Convert to percentage and positive value!#
b = (sy) - (d) ############################################
a = (sy)
else:
a = abs(x)
b = abs(x)
##########
elif y < -abs(x) and y < 0: # Bottom!#
###############
if -1 < x < 0: # Bottom Left!#
############################################
c = abs(x) # Convert to percentage and positive value!#
a = (sy) - (c) ############################################
b = (sy)
################
elif 0 < x < 1: # Bottom Right!#
############################################
d = abs(x) # Convert to percentage and positive value!#
b = (sy) - (d) ############################################
a = (sy)
else: ############################################
a = abs(sy) # So that values "a" and "b" are regonised #
b = abs(sy) # by python reader (I had problems)! #
############################################
#################################
# Special formula for when you #
# leave the "allowed" area #
#################################
elif y < abs(x) and y > -abs(x):
if x < 0:
b = abs(x)
a = 0
elif x > 0:
a = abs(x)
b = 0
else:
a = 0
b = 0
##################################
# Special Formula for all values #
# on the x-axis for immediate #
# turning. #
##################################
elif y == 0:
if x < 0:
a = 0
b = abs(sx)
elif x > 0:
a = abs(sx)
b = 0
else:
a = 0
b = 0
#######################################
# Anything else that is not defined #
# (Only when x and y = 0) #
#######################################
elif x == 0:
a = abs(sy)
b = abs(sy)
else:
a = 0
b = 0
######################################
### PWM convetion for AnalogueWrite()#
######################################
PWM = 255
a = PWM*a
a = round(a,0)
a = int(a)
b = PWM*b
b = round(b,0)
b = int(b)
####################################
# Printing Output - Only used in #
# development of Joystick Control. #
####################################
#print (a,b)
########################
# TCP client is below. #
########################
data = (a,b)
HOST, PORT = "192.168.12.12", 9999
#HOST2, PORT2 = "10.88.112.174", 9998 # Change to server when necessary
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT)) ## CIAN HAS CHANGED!!##
##sock.sendall(str(data) + "\n")
##print "Sent: {}".format(data)
if a != aa or b != bb:
sock.sendall(str(data) + "\n")
print "Sent: {}".format(data)
## elif a == aa and :
## print (int(a),int(b))
aa = a
bb = b
#####################################################
##@@@@@@@@@@@@@@@@@@@@@@ END @@@@@@@@@@@@@@@@@@@@@ ##
#####################################################[/code]
This seems to work ok, I am working on getting the TCP Client quicker - so that is a bit of a mess right here.
This is how far I have gotten with the Arduino Sketch:
[code]/*
* Robot Motor Controller script with PWM speed control
*/
//---------------------- Motors
int motorL[] = {6, 7};
int motorR[] = {8, 9};
int ledPin = 13;
int motor_a = 0;
int motor_b = 0;
int dir_y = 5;
void setup() {
Serial.begin(9600);
int i;
for(i = 0; i < 2; i++){
pinMode(motorL[i], OUTPUT);
pinMode(motorR[i], OUTPUT);
pinMode(ledPin, OUTPUT);
}
}
void loop() {
drive_forward(); {
digitalWrite(motorL[0], HIGH);
digitalWrite(motorL[1], LOW);
digitalWrite(motorR[0], HIGH);
digitalWrite(motorR[1], LOW);
analogWrite(9, motor_a);
analogWrite(6, motor_b);
}
drive_backwards(); {
digitalWrite(motorL[0], LOW);
digitalWrite(motorL[1], HIGH);
digitalWrite(motorR[0], LOW);
digitalWrite(motorR[1], HIGH);
analogWrite(9, motor_a);
analogWrite(6, motor_b);
}
}
void if(y < 0) drive_foreward()
void if(y > 0) drive_backwards()[/code]
No surprise that it doesn't work. I want it to read three values from the Python script which are the speed of the left motor (defined as "a"), speed of the right motor (defined as "b"), direction of motor (which is defined as "y" - when it is <0 it goes backwards or >0 goes forward).
For the [b]varying speed controls[/b], I will be using PWM (values between 0 -> 255) which uses the "analogWrite(Pin, value)" Syntax. These values will be changing via the "a" and "b" values from the python script.
Is there anyway that you can help me with this. I am really struggling.
Thanks in advance to anyone who can help.