2

I am new to Raspberry Pi and it's communication. I need to make a project where I want to use a Raspberry pi 3 as a monitoring device for motors and battery system. The Raspberry pi itself communicates with Roboteq controller using USB connection. Below is the simple code I'm trying to send via USB to the motor controller. I want to get motor Amps and show values in the terminal:

import time
import serial

ser = serial.Serial(

    port = '/dev/ttyACM0',
    baudrate = 9600,
    parity = serial.PARITY_NONE,
    stopbits = serial.STOPBITS_ONE,
    bytesize = serial.EIGHTBITS,
    timeout = 1
)

print("connected to: " + ser.portstr)

motor_AMPS = getvalue(_MOTAMPS)

print(motor_AMPS)

In the Roboteq user manual was written that by using getvalue(_MOTAMPS) command I can get motor amps. However when I am trying to run mention code the an error message is occur: "module 'serial' has no attribute 'getvalue'" It seems that I'm sending data to controller wrongly. I'havent found much info about Raspberry pi and Roboteq controller communication. So any help is greatly appreciated.

2

Looks like you need to send a '?A [cc]' command then read the response.

Try the following:

import time
import serial

ser = serial.Serial(

    port = '/dev/ttyACM0',
    baudrate = 9600,
    parity = serial.PARITY_NONE,
    stopbits = serial.STOPBITS_ONE,
    bytesize = serial.EIGHTBITS,
    timeout = 1
)

motorChannel = 1
command = '?A '+str(motorChannel)+' \r'
ser.write(command.encode())
data = ser.readline().decode().strip()
value = 0
if data.startswith('A='):
    value = int(data.split('=')[1])
print(value)
  • 1
    Thank you very much, now it works fine. I get an output: ?A 1 A=0 However it seems, that the output is string. Is it possible to get output as int value so that it was possible to store it after and for example make comparisons with other variables – AlexTsp Sep 27 at 12:36
  • 1
  • 1
    Updated to extract int – CoderMike Sep 27 at 12:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.