User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Python to Raspberry Pi to Arduino to Motors...

Thu Oct 11, 2012 9:52 am

I have a software question based robotics question.

I know very little about Ardunio and the coding for it. I have written some of the code that I require but I am now stuck at the Arduino Sketch. There is two motors, a left and right motor which their speeds are defined as "a" and "b" respectivly. All I know is that I have to use the analogWrite() command to get this to work with values between 0 and 255. I have converted all values to work with this (ie. values between 0 - 255).

This is what I have so far:

Code: Select all

 
/*
 * Robot Motor Controller script with PWM speed control
 */
//---------------------- Motors
int motorL[] = {6, 7}
int motorR[] = {8, 9}
int ledPin = 13;
 
 

if y > 0:
  digitalWrite(motorL[0], HIGH);
  digitalWrite(motorL[1], LOW);
  digitalWrite(motorR[0], HIGH);
  digitalWrite(motorR[1], LOW);
  analogWrite(9, motor_a);
  analogWrite(6, motor_b);
 
 
if y < 0:
  digitalWrite(motorL[0], LOW);
  digitalWrite(motorL[1], HIGH);
  digitalWrite(motorR[0], LOW);
  digitalWrite(motorR[1], HIGH);
  analogWrite(9, motor_a);
  analogWrite(6, motor_b);

Can someone please fix any Syntex errors and other errors that may be present in my code please. I would really like this project to work as it is the most complicated I have tried with the Raspberry Pi.

What I need this to do is:
- decide whether the motor is going forwards or backwards
- (Based on the above) set the pins for that direction
- Interpit the values that are given to it from the TCP server.
- loop back continuly until the program is terminaled.


Any help will be appreciated.

For anyone who wants to know how the motors are controlled read on...
I will be controlling the robot by using a joystick on a remote computer using the pygame module. The code for the below is below:
import pygame

Code: Select all

pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
 
print 'Initialized Joystick : %s' % j.get_name()
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 = 100
    sy = f*(y)
    sx = f*(x)
    #PWM = 255

#############################################
## 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 = f*abs(x)            # Convert to percentage and positive value!#
                a = (sy) - (c)          ############################################
                b = (sy)
                                        #############
            elif 0 < x < 1:             # Top Right!#
                                        ############################################
                d = f*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 = f*abs(x)            # Convert to percentage and positive value!#
                a = (sy) - (c)          ############################################
                b = (sy)
                                        ################
            elif 0 < x < 1:             # Bottom Right!#
                                        ############################################
                d = f*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)*f
                a = 0
            elif x > 0:
                a = abs(x)*f
                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)           #
#######################################

    else:
        a = abs(sy)
        b = abs(sy)

        
####################################################################################               

######################################
### PWM convetion for AnalogueWrite()#
######################################
##    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)

###################################################################################
###################################################################################
###################################################################################
So far, I have a TCP server and client working but I have not incorporated it in this script yet.

I only want help on the Arduino Sketch. The Joystick control and TCP are working nicely but the Arduino code is not working (as seen above).

Any help would be appreciated.

User avatar
joan
Posts: 14476
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Python to Raspberry Pi to Arduino to Motors...

Thu Oct 11, 2012 11:07 am

You've only posted a snippet of the Arduino sketch which I thought needed to include a setup and a loop.

Could you post all the sketch?

edited to add:

I now see your snippet is in (I guess Python) so I guess is a sort of pseudo code.

Why not just copy from one of the example Arduinio skteches for driving a motor?

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Python to Raspberry Pi to Arduino to Motors...

Thu Oct 11, 2012 11:40 am

joan wrote:You've only posted a snippet of the Arduino sketch which I thought needed to include a setup and a loop.

Could you post all the sketch?

edited to add:

I now see your snippet is in (I guess Python) so I guess is a sort of pseudo code.

Why not just copy from one of the example Arduinio skteches for driving a motor?
This all of the code that I have at the moment, which is obviously my problem.

I don't want to copy any of the Motor Arduino sketches because they do not have PWM, which I want to use. This was the best site I could find: http://arduino.cc/forum/index.php/topic,42939.0.html
...but this is still not going to be enough for me to get motors working.

User avatar
joan
Posts: 14476
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Python to Raspberry Pi to Arduino to Motors...

Thu Oct 11, 2012 1:30 pm

There are different sorts of motors and different sorts of motor drivers. It will be helpful if you could point to the specs of the motors and driver boards you plan to use.

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Python to Raspberry Pi to Arduino to Motors...

Thu Oct 11, 2012 9:01 pm

joan wrote:There are different sorts of motors and different sorts of motor drivers. It will be helpful if you could point to the specs of the motors and driver boards you plan to use.
I am going to be using DC motors that will be connected to a H-bridge (that I will make).

The motors are 12v, 55kg/[email protected]
They use a maximum of 14A but I will only get upto 4A out of each motor.

User avatar
wallarug
Posts: 460
Joined: Mon May 14, 2012 8:21 am
Location: Sydney, Australia

Re: Python to Raspberry Pi to Arduino to Motors...

Thu Oct 11, 2012 9:51 pm

This is the updated sketch:

Code: Select all

/*
 * Robot Motor Controller script with PWM speed control
 */
//---------------------- Motors
int motorL[] = {6, 7};
int motorR[] = {8, 9};
int ledPin = 13;

void setup() {
Serial.begin(9600);
int i;
for(i =0; i > 0; i < 0){
pinMode(motorL[i], OUTPUT);
pinMode(motorR[i], OUTPUT);
pinMode(ledPin, OUTPUT);
}

}

void loop() {

drive_forward();
if y > 0:
	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();
if y < 0:
	digitalWrite(motorL[0], LOW);
	digitalWrite(motorL[1], HIGH);
	digitalWrite(motorR[0], LOW);
	digitalWrite(motorR[1], HIGH);
	analogWrite(9, motor_a);
	analogWrite(6, motor_b);
	
}
This is an improvement on what I had before.

Return to “Automation, sensing and robotics”