set PID speed

General discussion of the MCP motion controller product line
Post Reply
fausto.tromba
Posts: 37
Joined: Wed Jul 31, 2019 7:24 am
set PID speed

Post by fausto.tromba »

I tried to set the PID speed with the method SetM1Velocity(address, p,i,d,qpps)
it looks like that p, i, d and qpps must be an integer otherwise there is the following error:

Code: Select all

pi@raspberrypi:~/Documents/MCP236_py3/TestPrograms $ python3 roboclawPIDspeedpy.py 
'MCP236 2x30A v1.1.8\n'
Traceback (most recent call last):
  File "roboclawPIDspeedpy.py", line 25, in <module>
    rc.SetM1VelocityPID(address,p,i,d,qpps)
  File "/home/pi/Documents/MCP236_py3/TestPrograms/roboclaw_3.py", line 765, in SetM1VelocityPID
    return self._write4444(address,self.Cmd.SETM1PID,d*65536,p*65536,i*65536,qpps)
  File "/home/pi/Documents/MCP236_py3/TestPrograms/roboclaw_3.py", line 534, in _write4444
    self._writelong(val1)
  File "/home/pi/Documents/MCP236_py3/TestPrograms/roboclaw_3.py", line 199, in _writelong
    self._writebyte((val>>24)&0xFF)
TypeError: unsupported operand type(s) for >>: 'float' and 'int'
at the page 65 of the user manual explain that for a value bigger than a byte, it must be broken.
In BasicMicroStudio is possible to set a pid with a floating point

Image

how can I do by serrial command?
Thanks in advance
Attachments
Unbenannt.PNG
Unbenannt.PNG (3.3 KiB) Viewed 18265 times
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: set PID speed

Post by Basicmicro Support »

QPPS is an integer. P,I and D can be any positive values.

Motion studio converts the real numbers in to the proper format for the SetPID commands.

Make sure you have the latest version of Motion Studio.
fausto.tromba
Posts: 37
Joined: Wed Jul 31, 2019 7:24 am
Re: set PID speed

Post by fausto.tromba »

I was speaking about SetM1Velocity(address, p,i,d,qpps) python method.
it does not work, when I pass not an integer, but a float. For example p=7.2.
Ho can I set by serial port a float (P=7.2) ?
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: set PID speed

Post by Basicmicro Support »

The packet serial commands expect integer values. They are calculated as fixed point numbers. You have to do the math. For Velocity PID values they are 16:16 fixed point numbers so you have to multiply your 7.2 * 65536. Convert to an Integer and then give it to the SetM1VelocityPID command. For the SetM#PositionPID command they are 22:10 fixed point so multiply by 2048.
fausto.tromba
Posts: 37
Joined: Wed Jul 31, 2019 7:24 am
Re: set PID speed

Post by fausto.tromba »

Code: Select all

def line2int(line):
	line = line.strip()
	#print(line)
	l1,l2 = line.split(":=")
	return l2
	

def ReadFile():
	f=open("/home/pi/Documents/MCP236_py3/PRG/PIDs.txt")
	lines=f.readlines()
	f.close()
	mpPar = map(line2int,lines)
	prm = list(mpPar)
	
	P = float(prm[0])
	i_P = int(65536*P)
	
	I = float(prm[1])
	i_I = int(65536*I)
	
	D = float(prm[2])
	i_D = int(65536*D)
	Qpps = int(prm[3])
	i_Qpps = int(65536*Qpps)
	return (i_P,i_I,i_D,i_Qpps)
	



if __name__ == "__main__":
	rc = Roboclaw("/dev/ttyS0",38400)
	rc.Open()
	
	#Get version string
	address = 0x80
	version = rc.ReadVersion(address)
	if version[0]==False:
		print ("GETVERSION Failed")
	else:
		print (repr(version[1]))
	
	#Read file PIDs.txt
	p,i,d,qpps = ReadFile()
	print(p,i,d,qpps)
	rc.SetM1VelocityPID(address,p,i,d,qpps)
	time.sleep(2)
	M1speedPID = rc.ReadM1VelocityPID(address)
	print(M1speedPID)
'MCP236 2x30A v1.1.8\n'
441509 36620 0 372899840
[1, 32767.99998474121, 32767.99998474121, 0.0, 372899840]

I tried to set
P:=6.7369
I:=0.55878
D:=0
QPPS:=5690

converting it x 65536 I got:
P: 441509 I:36620 D:0 and QPPS:372899840
I set this values with method SetM1VelocityPID(address,p,i,d,qpps) BUT reading the PID i get
[1, 32767.99998474121, 32767.99998474121, 0.0, 372899840]

What is wrong?
fausto.tromba
Posts: 37
Joined: Wed Jul 31, 2019 7:24 am
Re: set PID speed

Post by fausto.tromba »

shall I receive an answer? I have to insert this feature in the SW and I'm stocked on it.
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: set PID speed

Post by Basicmicro Support »

The problem is we are understaffed right now and very busy. The other problem is I did rush my last response.

Using the Roboclaw.py library you dont need to do the conversion. It is already done for you:

Here is the command in the Python library.
def SetM1VelocityPID(self,address,p,i,d,qpps):
return self._write4444(address,self.Cmd.SETM1PID,long(d*65536),long(p*65536),long(i*65536),qpps)

So P I and D are floating point numbers and qpps is an integer.

Im sorry I didnt have more time to understand your question better last time. Hopefully this covers the issue better.

P.S. For a test program remove the code to parse values from a file. Why over complicate a test program like that?

Post Reply