MrYsLab / PyMata

A Python client class library for Interaction with Standard Firmata
GNU Affero General Public License v3.0
95 stars 40 forks source link

Difference between python and arduino code? #27

Closed gunthercox closed 7 years ago

gunthercox commented 7 years ago

I'm trying to replicate a small arduino sketch using PyMata. I was wondering if you could offer any insight into the following two programs for a motor controller. The code is suppose to set a speed using analog pin 10 and then set a direction for the motor by setting pins 12 and 13 on or off. The first one (arduino) works correctly. However, the second one in python is only partially working (the forwards and reverse LEDs on the motor controller light up correctly but the motors do not move). Because the lights are toggling, my theory is that there is that the issue resides in my analog write to pin 10.

int rm1 = 12;
int rm2 = 13;
int rms = 10;

void setup() {
  pinMode(rm1,OUTPUT);
  pinMode(rm2,OUTPUT);
  pinMode(rms,OUTPUT);

  analogWrite(rms, 255);
}

void loop() {
  digitalWrite(rm1, LOW);
  digitalWrite(rm2, HIGH);

  delay(2000);

  digitalWrite(rm1, HIGH);
  digitalWrite(rm2, LOW);

  delay(2000);
}
from PyMata.pymata import PyMata
import signal
import time
import sys

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!!!!')
    if board is not None:
        board.reset()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
board = PyMata('/dev/ttyACM0')

rm1 = 12
rm2 = 13
rms = 10

board.set_pin_mode(rms, board.OUTPUT, board.ANALOG)
board.set_pin_mode(rm1, board.OUTPUT, board.DIGITAL)
board.set_pin_mode(rm2, board.OUTPUT, board.DIGITAL)

board.analog_write(rms, 255);

while True:
    board.digital_write(rm1, 0);
    board.digital_write(rm2, 1);

    time.sleep(1)

    board.digital_write(rm1, 1);
    board.digital_write(rm2, 0);

    time.sleep(1)
gunthercox commented 7 years ago

I figured it out. I should have been using PWM instead of analog when I switched over to PyMata.

board.set_pin_mode(rms, board.PWM, board.DIGITAL)