dewy721 / EMC-2-Arduino

Arduino + LinuxCNC = Your very own FREE high-end CNC controller.
116 stars 73 forks source link

Getting this to work #3

Open siredmar opened 10 years ago

siredmar commented 10 years ago

Hi,

i'm triying to get HAL2Arduino to work and i'm stuck at some point here.

What i have done is download HAL2Arduino-0.5 and change the pinning and stepsPerInchX/Y/Z for my machine (modAccelStepper ) and the limit switches (modLimSwitch). I compiled it and flashed it to my arduino nano (with Atmega 328p). I connected my cnc to the configured pins. Then i went to linuxcnc and get the Ubuntu 10.04 package with all linuxcnc related stuff included and installed it in a virtualbox. I followed the instrucions.txt from your github repo and created a new config 'my-mill', changed the path for the python script and modified the config that linuxcnc will find the script. The Arduino is connected to the virtualbox and it's been recgnized with /dev/ttyUSB0.

When i try to start linuxcnc with the shortcut on the desktop for my configuration i receive the following errors: Can not find -sec MOT -var MOD -num 1 Can not find -sec IO - var IO -num 1 Can not find -sec LINUXCNC -var NML_FILE -num 1 Can not find -sec EMC -var NML_FILE -num 1 Waiting for component 'Hal2Arduino' to become ready......................................Traceback )most recent call last): File "/opt/HAL2Arduino-0.5.py", line 2020, in ser.close() NameError: global name 'ser' is not defined

custom.hal:2. /opt/HAL2Arduino-0.5.py exited without becoming ready 1474 PID TTY STAT TIME COMMAND Stopping realtime threads Unloading hal components

Can anyone tell me what's going on there?

siredmar commented 10 years ago

I got one step further. I modified the python script to set the device "/dev/ttyUSB0" as port... But the message is still there: Waiting for component 'Hal2Arduino' to become ready......................................Traceback )most recent call last):

But one thing seems good: the red uart led flashes on startup of linuxcnc.

rene-dev commented 10 years ago

On 02 Jun 2014, at 21:42, siredmar notifications@github.com wrote:

I got one step further. I modified the python script to set the device "/dev/ttyUSB0" as port... But the message is still there: Waiting for component 'Hal2Arduino' to become ready......................................Traceback )most recent call last):

I had to modify this line as well. This is how far I get:

halcmd: loadusr -Wn ard /home/emc/HAL2Arduino-0.5.py Waiting for component 'ard' to become ready............Found device on /dev/ttyUSB0 Asking for firmware info. Firmware: HAL-2-Arduino Unit: '0' Version: 0.5 .comThread: port=/dev/ttyUSB0 arrayPos:0 Queue:0x8d22b2c .....................................makePins: creating: 'axis_0_home-sw-in' makePins: creating: 'axis_0_joint-pos-cmd' makePins: creating: 'axis_0_pos-lim-sw-in' makePins: creating: 'axis_0_neg-lim-sw-in' makePins: creating: 'axis_1_home-sw-in' makePins: creating: 'axis_1_joint-pos-cmd' makePins: creating: 'axis_1_pos-lim-sw-in' makePins: creating: 'axis_1_neg-lim-sw-in' makePins: creating: 'axis_2_home-sw-in' makePins: creating: 'axis_2_joint-pos-cmd' makePins: creating: 'axis_2_pos-lim-sw-in' makePins: creating: 'axis_2_neg-lim-sw-in' commandList=[''] linkData='' listOfPorts=['/dev/ttyUSB0'] listOfCommands=['215 224 231 233'] listOfAxis=['0 1 2'] listOfUnits=['0'] linkMsg='' comService: started. comThread: Sending 996 0 0 996; to /dev/ttyUSB0 ………………………………………………..

hangs here, printing dots

Rene

dewy721 commented 10 years ago

Looks like to .py file is not returning the .ready back to hal to signal that its up and running. On Jun 3, 2014 4:43 AM, "Rene Hopf" notifications@github.com wrote:

On 02 Jun 2014, at 21:42, siredmar notifications@github.com wrote:

I got one step further. I modified the python script to set the device "/dev/ttyUSB0" as port... But the message is still there: Waiting for component 'Hal2Arduino' to become ready......................................Traceback )most recent call last):

I had to modify this line as well. This is how far I get:

halcmd: loadusr -Wn ard /home/emc/HAL2Arduino-0.5.py Waiting for component 'ard' to become ready............Found device on /dev/ttyUSB0 Asking for firmware info. Firmware: HAL-2-Arduino Unit: '0' Version: 0.5 .comThread: port=/dev/ttyUSB0 arrayPos:0 Queue:0x8d22b2c .....................................makePins: creating: 'axis_0_home-sw-in' makePins: creating: 'axis_0_joint-pos-cmd' makePins: creating: 'axis_0_pos-lim-sw-in' makePins: creating: 'axis_0_neg-lim-sw-in' makePins: creating: 'axis_1_home-sw-in' makePins: creating: 'axis_1_joint-pos-cmd' makePins: creating: 'axis_1_pos-lim-sw-in' makePins: creating: 'axis_1_neg-lim-sw-in' makePins: creating: 'axis_2_home-sw-in' makePins: creating: 'axis_2_joint-pos-cmd' makePins: creating: 'axis_2_pos-lim-sw-in' makePins: creating: 'axis_2_neg-lim-sw-in' commandList=[''] linkData='' listOfPorts=['/dev/ttyUSB0'] listOfCommands=['215 224 231 233'] listOfAxis=['0 1 2'] listOfUnits=['0'] linkMsg='' comService: started. comThread: Sending 996 0 0 996; to /dev/ttyUSB0 ………………………………………………..

hangs here, printing dots

Rene

— Reply to this email directly or view it on GitHub https://github.com/dewy721/EMC-2-Arduino/issues/3#issuecomment-44943881.

siredmar commented 10 years ago

I got one step further (by accident)... When i flash the HAL2Arduino hexfile and try directly after that, the python script detects the hal2arduino firmware. After that the firmware version should be sent, but never returns. It seems like the pairing will not work as planned... btw i use the arduino nano with just moAccelStepper and modLimitSwitch mods...

dewy721 commented 10 years ago

Ah. I see. The python file expects the arduino to respond in a preset amount of time. If its just a tiny bit too slow it won't connect. On Jun 3, 2014 3:49 PM, "siredmar" notifications@github.com wrote:

I got one step further (by accident)... When i flash the HAL2Arduino hexfile and try directly after that, the python script detects the hal2arduino firmware. After that the firmware version should be sent, but never returns. It seems like the pairing will not work as planned... btw i use the arduino nano with just moAccelStepper and modLimitSwitch mods...

— Reply to this email directly or view it on GitHub https://github.com/dewy721/EMC-2-Arduino/issues/3#issuecomment-45018382.

siredmar commented 10 years ago

So can you give me a hint where to tune the timings? Should i modify it in the python script? Or can be a proper handshake mechanism implemented in the arduino/python code?

dewy721 commented 10 years ago

Near line#121 of the .py file.... Change the serial packet timout value to timeout=0.05 or higher. Note: the higher the value the easier to connect but also more latency of the serial link. On Jun 3, 2014 4:01 PM, "siredmar" notifications@github.com wrote:

So can you give me a hint where to tune the timings? Should i modify it in the python script? Or can be a proper handshake mechanism implemented in the arduino/python code?

— Reply to this email directly or view it on GitHub https://github.com/dewy721/EMC-2-Arduino/issues/3#issuecomment-45019860.

siredmar commented 10 years ago

I tried timeout=0.06 and 0.5 and 1 and 2 but no effect. The only thing that is working is after plugging in the usb cable directly start linuxcnc. It seems to search the firmware, but nothing is received... Starting LinuxCNC... Waiting for component 'Hal2Arduino' to become ready...........Found device on /dev/ttyUSB0 Asking for firmware info. Firmware: Failed. HAL-2-Arduino not found on this device ...............................................................Wait, something is not right here.

But if i try again without reconnecting the arduino, it won't work again

dewy721 commented 10 years ago

It will also do that if the computer has another arduino that is not loaded with hal2arduino. I really need to fix that. :-/ On Jun 3, 2014 4:19 PM, "siredmar" notifications@github.com wrote:

I tried timeout=0.06 and 0.5 and 1 and 2 but no effect. The only thing that is working is after plugging in the usb cable directly start linuxcnc. It seems to find the arduino firmware, but the firmware version is not received: Starting LinuxCNC... Waiting for component 'Hal2Arduino' to become ready...........Found device on /dev/ttyUSB0 Asking for firmware info. Firmware: Failed. HAL-2-Arduino not found on this device ...............................................................Wait, something is not right here.

— Reply to this email directly or view it on GitHub https://github.com/dewy721/EMC-2-Arduino/issues/3#issuecomment-45022013.

siredmar commented 10 years ago

Btw: there is a fork of your work (https://github.com/CCHS-Melbourne/HackCNC) that hast the same goal as you... i tried that one and could connect to it pretty well and without problems... the code seems pretty stripped down from yours... don't know if thats a bug or a feature :) Still i'd like to see hal2arduino running because imho its the better approach.

dewy721 commented 10 years ago

I remember them, nice bunch. School kids actually. Its not so much as stripped down. Just older, less developed version of my work. I'm guessing that it's probably over a year ago that I helped them, smart group too. On Jun 3, 2014 4:31 PM, "siredmar" notifications@github.com wrote:

Btw: there is a fork of your work ( https://github.com/CCHS-Melbourne/HackCNC) that hast the same goal as you... i tried that one and could connect to it pretty well and without problems... the code seems pretty stripped down from yours... don't know if thats a bug or a feature :) Still i'd like to see hal2arduino running because imho its the better approach.

— Reply to this email directly or view it on GitHub https://github.com/dewy721/EMC-2-Arduino/issues/3#issuecomment-45023391.

siredmar commented 10 years ago

Did you ever experience such problems like i do connecting the hal2arduino?

dewy721 commented 10 years ago

Since I wrote the code from scratch, yes. Many times. On Jun 3, 2014 4:44 PM, "siredmar" notifications@github.com wrote:

Did you ever experience such problems like i do connecting the hal2arduino?

— Reply to this email directly or view it on GitHub https://github.com/dewy721/EMC-2-Arduino/issues/3#issuecomment-45024747.

siredmar commented 10 years ago

Hi,

i debugged the code a little bit. The 'ok' message seems to be received. What is not working is the comSetup function as it won't pass the line ser.write(lastCommand). I'm no python expert, but i think it is somehow connected to pyserial... I use python 2.7.5 with pyserial 2.7. Can you give me info on your setup?

siredmar commented 10 years ago

I stripped the python program down to the minimum and added some wait loop for handshake and i get the expected results:

#!/usr/bin/python
import sys, string
import serial
import time

firmware = 'HAL-2-Arduino'
firmwareVersion = 0.5
port = '/dev/ttyUSB0'

def comSetup(ser, command, data1, data2):
    lastChecksum=command + data1 + data2
    lastCommand="%s %s %s %s%s" % (str(command), str(data1), str(data2), str(lastChecksum), ";")
    ser.flush()
    ser.write(lastCommand)
    for i in range(0,6): # attemp to send a command, but no more then 5 times.
        linkMsg = ser.readline()
        if linkMsg.find('resend') > -1:
            print "resend"
            ser.flushInput()
            ser.write(lastCommand)
        else: # Command sent successfully.
            returnvalue = linkMsg.strip('\r\n')
            return returnvalue

ser = serial.Serial(port, 115200, timeout=1)
print "Found device on %s\nAsking for firmware info." % port
time.sleep(2)
linkMsg = 'notok'

while linkMsg.find('ok') != -1:
   linkMsg = ser.readline()
   time.sleep(0.5)

if comSetup(ser, 990, 0, 0) == firmware:
   print "firmware %s" % firmware
   unitId=comSetup(ser, 992, 0, 0)
   if unitId != "":
      print "Unit: %r" % unitId
      version=float(comSetup(ser, 991, 0, 0))
      print "version: %s" % version
      if version >= firmwareVersion:
         codesAccepted = comSetup(ser, 993, 0, 0)
         print "codesAccepted: %s" % codesAccepted
ser.close()

This is the output: $ ./testserial.py Found device on /dev/ttyUSB0 Asking for firmware info. firmware HAL-2-Arduino Unit: '0' version: 0.5 codesAccepted:

So the communication seems to work with pyserial and my version of python. With this wait routine the arduino can be reset with the normal reset switch an the python script waits until the "ok" comes in. When i insert this exact same wait loop in the original py script, nothin happens. Maybe it's related to those threading functions it is using?

siredmar commented 10 years ago

I tested further, it is definitely a problem regarding the threads. i commented out the thread generation (i only use one arduino and probably will never need a second or even a 10th one) and called the comThread function manually. The communications seems to work fine but the program wont continue and prints only dots after printing all firmware specific things.

siredmar commented 10 years ago

Ah btw: in order to make the if else construct work i needed to disable in the arduino firmware the #define commandsRequested

siredmar commented 10 years ago

Ok, now i got the original py file and added the while loop for proper handshake... Now i hang with the same printing dots like Rene (second post on this issue) when loading the py file directly with halrun... I suspect its because linuxcnc does not run. When i run linuxcnc this shows up:

$ linuxcnc
LINUXCNC - 2.5.4
Machine configuration directory is '/home/armin/linuxcnc/configs/my-mill'
Machine configuration file is 'my-mill.ini'
Starting LinuxCNC...
while loop
linkMsg: ok

while loop
Waiting for component 'Hal2Arduino' to become ready..........................linkMsg: 
.Found device on /dev/ttyUSB0
Asking for firmware info.
Firmware: HAL-2-Arduino
Unit: '0'
Version:  0.5
comThread: port=/dev/ttyUSB0    arrayPos:0      Queue:0x9a98acc
......................comService: started.
 commandList=['']
linkData=''
listOfPorts=['/dev/ttyUSB0']
listOfCommands=['']
listOfAxis=['0 1 2']
listOfUnits=['0']
linkMsg=''
....................
custom.hal:21: Pin 'Hal2Arduino.axis_0_joint-pos-cmd' does not exist
Shutting down and cleaning up LinuxCNC...
Traceback (most recent call last):
  File "/opt/HAL2Arduino-0.5.py", line 2021, in <module>
    commandHandler(str(listOfCommands), str(listOfAxis))
  File "/opt/HAL2Arduino-0.5.py", line 854, in commandHandler
    if codesAccepted.find(str(command)) > -1 and axisesRequested.find(str(thisAxis)) > -1:
KeyboardInterrupt
Unhandled exception in thread started by 
sys.excepthook is missing
lost sys.stderr
Cleanup done
LinuxCNC terminated with an error.  You can find more information in the log:
    /home/armin/linuxcnc_debug.txt
and
    /home/armin/linuxcnc_print.txt
as well as in the output of the shell command 'dmesg' and in the terminal

Seems like it wont connect signals provided from the py script or something? It seems to me that the arduino part seems to work. Can you give me working config for linuxcnc for 3 axis, 3 homing switches and in mm so i can test it?

dewy721 commented 10 years ago

Oic, look a the line that starts with custom.hal:21 the hal file is expecting a pin. Joint position for X.

If you removed it from the arduino, you'll also need to remove the corresponding entry from the custom.hal file too. That's why it crashed.

Your getting closer, and gaining speed I may add. :) On Jun 4, 2014 7:50 AM, "siredmar" notifications@github.com wrote:

Ok, now i got the original py file and added the while loop for proper handshake... Now i hang with the same printing dots like Rene (second post on this issue) when loading the py file directly with halrun... I suspect its because linuxcnc does not run. When i run linuxcnc this shows up:

$ linuxcnc LINUXCNC - 2.5.4 Machine configuration directory is '/home/armin/linuxcnc/configs/my-mill' Machine configuration file is 'my-mill.ini' Starting LinuxCNC... while loop linkMsg: ok

while loop Waiting for component 'Hal2Arduino' to become ready..........................linkMsg: .Found device on /dev/ttyUSB0 Asking for firmware info. Firmware: HAL-2-Arduino Unit: '0' Version: 0.5 comThread: port=/dev/ttyUSB0 arrayPos:0 Queue:0x9a98acc ......................comService: started. commandList=[''] linkData='' listOfPorts=['/dev/ttyUSB0'] listOfCommands=[''] listOfAxis=['0 1 2'] listOfUnits=['0'] linkMsg='' .................... custom.hal:21: Pin 'Hal2Arduino.axis_0_joint-pos-cmd' does not exist Shutting down and cleaning up LinuxCNC... Traceback (most recent call last): File "/opt/HAL2Arduino-0.5.py", line 2021, in commandHandler(str(listOfCommands), str(listOfAxis)) File "/opt/HAL2Arduino-0.5.py", line 854, in commandHandler if codesAccepted.find(str(command)) > -1 and axisesRequested.find(str(thisAxis)) > -1: KeyboardInterrupt Unhandled exception in thread started by sys.excepthook is missing lost sys.stderr Cleanup done LinuxCNC terminated with an error. You can find more information in the log: /home/armin/linuxcnc_debug.txt and /home/armin/linuxcnc_print.txt as well as in the output of the shell command 'dmesg' and in the terminal

— Reply to this email directly or view it on GitHub https://github.com/dewy721/EMC-2-Arduino/issues/3#issuecomment-45085971.

siredmar commented 10 years ago

thanks for the information but i'm not quite sure how to configure that... as far as i understand this whole system correct the arduino receives some commands over uart and does something with the information. This part seems to work just fine and for the first approach it shouldn't matter at all how the arduino is configured... what i don't quite understand is the linuxcnc configuration... the custom.hal maps nets given by the python script to commands eg. xpos-cmd. Those commands are remapped in my-mill.hal to stepgen commands. Is that correct so far? btw: what is meant by "joint position"? But i think i understand it now and know how to configure it. Unfortunately i can't it until monday.

siredmar commented 10 years ago

So i am testing and have no idea how to configure it right. Maybe you can give me a hint by have a look at my configuration files... here is the arduino project using only stepper drivers for x, y and z and 3 homing switches. No other mods are active... https://github.com/siredmar/EMC-2-Arduino/tree/master/Downloads/HAL2Arduino/HAL2Arduino-0.5/HAL2Arduino_0_5

and here is my linuxcnc config: https://github.com/siredmar/EMC-2-Arduino/tree/master/configs/my-mill-0.5

g1ra commented 10 years ago

I uploaded the HAL2Arduino_0_5 to Arduino Due using Arduino 1.5.7 But it seem I cant read firmware.

sudo python HAL2Arduino-0.5.py 
Serial<id=0x838ab6c, open=True>(port='/dev/ttyACM0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=0.95999999999999996, xonxoff=0, rtscts=0, dsrdtr=0)
Found device on /dev/ttyACM0
Asking for firmware info.
Firmware: Failed. HAL-2-Arduino not found on this device

Wait, something is not right here.
Traceback (most recent call last):
  File "HAL2Arduino-0.5.py", line 2020, in <module>
    ser.close()
NameError: name 'ser' is not defined
g1ra commented 10 years ago

EncoderServo seem not working. But I downloaded the zip and imported in library.

define useEncoderServo false

Now I found firmware.