Saturday, April 24, 2021

Tips when starting Python programming V plotter in Raspberry

Two errors I encountered and How I googled them out of existence.

  1.  Initially when importing numpy in python after installing it using python3 -m pip install numpy, i got error that "Importing numpy shared c extensions failed.". It got solved after running "sudo apt-get install libatlas-base-dev". Answer from " https://github.com/numpy/numpy/issues/16012"
  2. While importing serial and running serial.Serial(), i got error that serial has nothing called Serial. Solution was to uninstall serial and install pyserial. Answer from "https://duckduckgo.com/?q=serial+has+no+attribute+serial&t=raspberrypi&ia=web"

pip uninstall serial
pip uninstall pyserial
pip install pyserial 

Working code in Raspbian 32 bit Os on Raspberry 4 B, with arduino cnc shield , 28BYJ motors for V plotter:

import numpy as np
import matplotlib
# ensure pyserial and serial is installed
import serial
import time

ser = serial.Serial("/dev/ttyUSB0")
ser.baudrate = 9600

# Existing steps per mm was 250 for x and y axis. It was found using $$ command.
# Measured actual travel of thread and set the $0 and $1 variable to correct values.
#$0 = 138.8 = 139 steps per mm
#$1 = 138.8 = 139 steps per mm
if (ser.is_open):
    print ("\n Serial port has been opened")
    print("\n Initialising communication with GRBL on the arduino")
    print("\n Using ")
    print(ser.name)
else:
    print("\n Could not open serial port for communicating with GRBL on the arduino")

# Communicating for codes
print("Stripping all end of characters for safety before encoding to byte string.")
ser.write("\r\n\r\n".strip().encode())
time.sleep(2);
ser.flushInput();

print("Using G01 command for a basic move command")
command= "G01X{}Y{}".format(10,10)
ser.write((command.strip()+'\n').encode());
#reading reply
grbl_reply = ser.readline().strip()
print("GRBL replied that")
print(grbl_reply)
time.sleep(4)
ser.flushInput();

print("\n Closing serial port ,finally.")        
ser.close();