Hallo, i have a little probelm with Raspberry Pi connected with Arduino Uno via Serial USB.
Here is my Arduino Sketch:
#include <stdlib.h>
#include <stdio.h>
int RV = 5;
int RZ = 6;
int LV = 3;
int LZ = 11;
char incoming[2];
void setup() {
Serial.begin(115200);
//---------------------------------------------- Set PWM frequency for D5 & D6 -------------------------------
//TCCR0B = TCCR0B & B11111000 | B00000001; // set timer 0 divisor to 1 for PWM frequency of 62500.00 Hz
//TCCR0B = TCCR0B & B11111000 | B00000010; // set timer 0 divisor to 8 for PWM frequency of 7812.50 Hz
TCCR0B = TCCR0B & B11111000 | B00000011; // set timer 0 divisor to 64 for PWM frequency of 976.56 Hz (The DEFAULT)
//TCCR0B = TCCR0B & B11111000 | B00000100; // set timer 0 divisor to 256 for PWM frequency of 244.14 Hz
//TCCR0B = TCCR0B & B11111000 | B00000101; // set timer 0 divisor to 1024 for PWM frequency of 61.04 Hz
//---------------------------------------------- Set PWM frequency for D9 & D10 ------------------------------
//TCCR1B = TCCR1B & B11111000 | B00000001; // set timer 1 divisor to 1 for PWM frequency of 31372.55 Hz
//TCCR1B = TCCR1B & B11111000 | B00000010; // set timer 1 divisor to 8 for PWM frequency of 3921.16 Hz
// TCCR1B = TCCR1B & B11111000 | B00000011; // set timer 1 divisor to 64 for PWM frequency of 490.20 Hz (The DEFAULT)
//TCCR1B = TCCR1B & B11111000 | B00000100; // set timer 1 divisor to 256 for PWM frequency of 122.55 Hz
//TCCR1B = TCCR1B & B11111000 | B00000101; // set timer 1 divisor to 1024 for PWM frequency of 30.64 Hz
//---------------------------------------------- Set PWM frequency for D3 & D11 ------------------------------
//TCCR2B = TCCR2B & B11111000 | B00000001; // set timer 2 divisor to 1 for PWM frequency of 31372.55 Hz
//TCCR2B = TCCR2B & B11111000 | B00000010; // set timer 2 divisor to 8 for PWM frequency of 3921.16 Hz
TCCR2B = TCCR2B & B11111000 | B00000011; // set timer 2 divisor to 32 for PWM frequency of 980.39 Hz
//TCCR2B = TCCR2B & B11111000 | B00000100; // set timer 2 divisor to 64 for PWM frequency of 490.20 Hz (The DEFAULT)
//TCCR2B = TCCR2B & B11111000 | B00000101; // set timer 2 divisor to 128 for PWM frequency of 245.10 Hz
//TCCR2B = TCCR2B & B11111000 | B00000110; // set timer 2 divisor to 256 for PWM frequency of 122.55 Hz
//TCCR2B = TCCR2B & B11111000 | B00000111; // set timer 2 divisor to 1024 for PWM frequency of 30.64 Hz
}
void loop() {
if(Serial.available() > 6){
for (char i = 0; i < 5; i++){
incoming[i] = Serial.read();
}
//intalisieren von linken joystick VOR sowie ZURüCK
int a1 = incoming[0];
short m2;
m2 = a1*2.55; //set Value from 0-100(joystick) to 0-255 (arduino PWM)
//intalisieren von linken joystick rechts und links
int b1 = incoming[1];
short n2;
n2 = b1*2.55; //set Value from 0-100(joystick) to 0-255 (arduino PWM)
if ((n2 > 1 && n2 < 257) &&( m2 == 0))//&& (m2 == 0)) //rechtsdreh ohne gasgeben
{
n2=+n2;
analogWrite(LV,n2);
analogWrite(LZ,0);
analogWrite(RV,0);
analogWrite(RZ,n2);
}
else if ((n2 < 0 && n2 > -256) && (m2 == 0)) //linksdreh ohne gasgeben
{
n2=-n2;
analogWrite(LV,0);
analogWrite(LZ,n2);
analogWrite(RV,n2);
analogWrite(RZ,0);
}
else if (m2 < -1 && m2 > -256) //vorne 0-255
{
m2=-m2;
analogWrite(RV, m2);
}
else if (m2 > + 0 )//&& m2 < 256) //zurueck 0-255
{
analogWrite(RV,m2);
analogWrite(RZ, 0);
analogWrite(LV, 0);
analogWrite(LZ, 0);
}
else
{
analogWrite(RZ, 0);
analogWrite(RV, 0);
analogWrite(LV,0);
analogWrite(LZ,0);
}
}
}
Here is my Python Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys, pygame
from pygame import locals
import time
import RPi.GPIO as GPIO
import serial
import binascii
import struct
arduino=serial.Serial(port='/dev/ttyACM0',baudrate = 115200)
os.environ["SDL_VIDEODRIVER"] = "dummy"
pygame.init()
pygame.joystick.init() # main joystick device system
try:
j = pygame.joystick.Joystick(0) # create a joystick instance
j.init() # init instance
print 'Enabled joystick: ' + j.get_name()
except pygame.error:
print 'no joystick found.'
def senden():
arduino.write(struct.pack('<bb',b2,a2))
while 1:
for e in pygame.event.get(): # iterate over event stack
if e.type == pygame.locals.JOYAXISMOTION: # Read Analog Joystick Axis
x1 , y1 = j.get_axis(0), j.get_axis(1) # Left Stick
y2 , x2 = j.get_axis(2), j.get_axis(3) # Right Stick
print x1
print y1
print x2
print y2
#linker joystik rechts sowie links
a1 = x1*100
a2=int(a1)
print'neuer wert x1'
print a2
#int2 = a2
#linker joystick vorwaerts sowie zurück
#beachte den faktor 100
b1 = y1*100
b2=int(b1)
print'neuer wert y1'
print b2
senden()
The Raspberry Pi is connected with a PS3 Controller.
And the Arduino Uno ist connected with a BTS 7960 to supply my Motor.
But my Motor don't work like the ps3 Controller say.
Can anybody help me?
Many Thanks!