MrYsLab / telemetrix-aio

A user-extensible asyncio replacement for StandardFirmata. All without the complexity of Firmata!
GNU Affero General Public License v3.0
11 stars 5 forks source link

Stepper absolute problem #21

Closed Binh2542 closed 3 months ago

Binh2542 commented 3 months ago

Hi, im using arduino nano with tmc2208 and nema17, when i use arduino code my stepper work perfectly But when i upload Telemetrix4arduino to my arduino, and run Stepper absolute example it always show: Telemetrix4AIO found and connected to COM3 Telemetrix4Arduino Version Number: 5.4.0 The motor IS NOT running. Starting motor... The motor IS NOT running. Motor 0 absolute motion completed at: 2024-06-05 13:58:23. I already change pin1=7, pin2=6, exactly like my wiring now

MrYsLab commented 3 months ago

Hi, thanks for reporting this. There is a typo in the absolute example. Please modify the call to set_pin_mode_stepper to change the interface to 1. Also, please remember that pin1 is the pulse pin, and pin2 is the direction pin. It should look something like this:

motor = await the_board.set_pin_mode_stepper(interface=1, pin1=7, pin2=6)

Please verify that the pulse and direction pins are correct for your setup.

Please let me know if this solves your issue.

Binh2542 commented 3 months ago

oh, i never use TB6600 before but t though it's same as TMC so i used interface=1 "motor = await the_board.set_pin_mode_stepper(interface=1, pin1=7, pin2=6)" That seems exactly like my code. I'll try again tomorrow. Thanks for very quick respond

MrYsLab commented 3 months ago

I retested here this morning using the TB6600, and the example works.

If you are still having issues, please post your Python script.

Binh2542 commented 3 months ago

it still doesnt work, i get that messenge " the motor is not running" Here is my Python script

import asyncio
import sys
import time

from telemetrix_aio import telemetrix_aio

"""
Run a motor to an absolute position. Server will send a callback notification 
when motion is complete.
"""

async def the_callback(data):
    date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[2]))
    print(f'Motor {data[1]} absolute motion completed at: {date}.')

async def running_callback(data):
    if data[1]:
        print('The motor is running.')
    else:
        print('The motor IS NOT running.')

async def step_absolute(the_board):

    # create an accelstepper instance for a TB6600 motor driver
    motor = await the_board.set_pin_mode_stepper(interface=1, pin1=7, pin2=6)

    # if you are using a 28BYJ-48 Stepper Motor with ULN2003
    # comment out the line above and uncomment out the line below.
    # motor = the_board.set_pin_mode_stepper(interface=4, pin1=5, pin2=4, pin3=14,
    # pin4=12)

    await the_board.stepper_is_running(motor, callback=running_callback)
    await asyncio.sleep(.2)

    # set the max speed and acceleration
    await the_board.stepper_set_max_speed(motor, 400)
    await the_board.stepper_set_acceleration(motor, 800)

    # set the absolute position in steps
    await the_board.stepper_move_to(motor, -2000)

    # run the motor
    print('Starting motor...')
    await the_board.stepper_run(motor, completion_callback=the_callback)
    await asyncio.sleep(.2)
    await the_board.stepper_is_running(motor, callback=running_callback)
    await asyncio.sleep(.2)

    # keep application running
    while True:
        try:
            await asyncio.sleep(.2)

        except KeyboardInterrupt:
            await the_board.shutdown()
            sys.exit(0)

# get the event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

# instantiate telemetrix_aio
board = telemetrix_aio.TelemetrixAIO()
try:
    # start the main function
    loop.run_until_complete(step_absolute(board))
    loop.run_until_complete(board.shutdown())

except KeyboardInterrupt:
    loop.run_until_complete(board.shutdown())
    sys.exit(0)
and i'm using this version of Telemetrix4arduino (arduino library) :

https://github.com/MrYsLab/Telemetrix4Arduino

MrYsLab commented 3 months ago

Thanks for providing the code. I see no issues with it. I am unfamiliar with the TMC2208. The Arduino examples I found on the web for the TMC2208 connect the enable pin to an Arduino pin, and then the pin is driven low. To test if the AccelStepper code works with the TMC2208, please try this AccelStepper sketch.

You will need to change this line of the sketch to:

AccelStepper stepper(1, 7, 6);

If this also fails to run the motor, you may wish to connect the TMC2208 enable pin to an Arduino pin, and in the setup of the sketch:

#define ENABLE_PIN 5 // change this to anything you wish
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW);

Please let me know the results of using the sketch and if you need to drive the enable pin low, Thanks.

Binh2542 commented 3 months ago

It works if i use enable pin, thank a lot, now i'll use enable pin and test in python

`// Bounce.pde // -- mode: C++ -- // // Make a single stepper bounce from one limit to another // // Copyright (C) 2012 Mike McCauley // $Id: Random.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

include

define ENABLE_PIN 10 // change this to anything you wish

// Define a stepper and the pins it will use AccelStepper stepper(1, 7, 6); void setup() {
// Change these to suit your stepper if you want pinMode (ENABLE_PIN, OUTPUT); digitalWrite(ENABLE_PIN, LOW); stepper.setMaxSpeed(100); stepper.setAcceleration(20); stepper.moveTo(500); }

void loop() { // If at the end of travel go to the other end if (stepper.distanceToGo() == 0) stepper.moveTo(-stepper.currentPosition());

stepper.run();

}`

Binh2542 commented 3 months ago

My code works perfectly now, thank you for your useful respond

MrYsLab commented 3 months ago

There are two possible ways to do this. 1:

ENABLE_PIN = 6
# Turn motor ON
await my_board.set_pin_mode_digital_output(ENABLE_PIN)
await my_board.digital_write(pin, 0)

# Turn motor OFF
await my_board.digital_write(pin, 0)

2:

# MOTOR_ID is the value returned when set_pin_mode_stepper is called
await myboard.stepper_set_enable_pin(MOTOR_ID, ENABLE_PIN)

# To enable the motor
await myboard.stepper_enable_outputs(MOTOR_ID)

# To turn off the motor
await myboard.stepper_disable_outputs(MOTOR_ID)

Hopefully, that should solve the problem.

Binh2542 commented 3 months ago

thank you for your help and your useful library

MrYsLab commented 3 months ago

You are very welcome. I am closing the issue. If you need to open it again, leave a message here, and I will reopen it.