BasicMicro - Forums

www.basicmicro.com
It is currently Mon May 21, 2012 9:19 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 73 posts ]  Go to page 1, 2, 3, 4, 5  Next
Author Message
 Post subject: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Fri Apr 23, 2010 1:04 pm 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
Or should I say, "not reading?" I am able to drive the motor using packet serial mode, and I believe I am even able to control speed with the quadrature encoder, but I don't have the tools to verify that. The motor does ramp up and down as would be expected with the accel and decel parameters with quadrature decoder commands. Something it doesn't do when given a simple speed command like (Serout P15, i19200, [128, 0, 127, (255 & 0X7F)]) The program will execute until the Serin command, and at that point it will just hang. No errors, no warnings, just stops. I am only trying this in debug mode, but have tried both animated and not. I have a feeling it's something I'm doing wrong with the serin command. I'm using a dev board, so the hardware serial port is tied to the USB. The following code is from the Roboclaw datasheet, only modified to work on p15 of my nano.

Code:
Encoder1 Var Long
Sts Var Byte
CRC Var Byte

Clear
Pause 2000
serout p15, i19200, [128, 20, (148 & 0x7F)]; Resets encoder registers

Main
   Pause 100

   ReadEncoderM1
      serout p15, i19200, [128, 16]      'highlights here and hangs
      serin p15, i19200, [Encoder1.byte3, Encoder1.Byte2, Encoder1.Byte1, Encoder1.Byte0, Sts, crc]
      debug ["Encoder1: ", DEC Encoder1, 13, "Status Byte: ", BIN Sts]
      pause 500
goto Main


If I use this code, the motor appears to run fine, but does not give me any feedback... which, unfortunately, is required in this case.

Code:
Speed Var Long
Accel Var Byte
CRC Var Byte
Address con 128
CMD Var Byte

Clear
Gosub MixStop ;Stop any previous running command and set speed / accel values
;Program starts here. Each label specifies what the command does.

MixForward
   CMD = 36
   CRC = (Address + CMD + Speed.byte3 + Speed.byte2 + Speed.byte1 + Speed.byte0 + Accel) & 0x7f
   serout p15, i19200, [Address, CMD, Speed.byte3, Speed.byte2, Speed.byte1, Speed.byte0, Accel, CRC]; Drive forward
   Gosub WaitCMD
   Gosub MixStop
   goto MixForward

WaitCMD
   CRC = 0 ;Clear CRC
   Pause 5000 ;Run CMD for 5 seconds
   Return ;Go back to command after gosub

MixStop
   Speed = 0 ;Max pulse per second
   Accel = 30 ;ramp up pulse per second
   CRC = (Address + CMD + Speed.byte3 + Speed.byte2 + Speed.byte1 + Speed.byte0 + Accel) & 0x7f
   serout p15, i19200, [Address, CMD, Speed.byte3, Speed.byte2, Speed.byte1, Speed.byte0, Accel, CRC]; Stop
   Pause 5000
SetValues
   Speed = 4000 ;Top speed / max pulse per second
   Accel = 50 ;Ramp up / down pulse per second
Return

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Fri Apr 23, 2010 2:57 pm 
Offline
Master

Joined: Sun Aug 17, 2008 5:26 pm
Posts: 798
Location: CA bay Area
Hello. The SERIN command is probably waiting for some input, any input. Since I can't find a data sheet for this, I contacted BasicMicro through an email to help you out.
Have a nice project.
kenjj

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Fri Apr 23, 2010 3:03 pm 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 528
I believe the problem you are having is you are doing the SERIN on the wrong IO pin. I just started playing with the roboclaw again (I fried my encoders when I accidentaly plugged the power plugs in backwords... Got replacements yesterday).

I have a simple rover (actually a Lynxmotion Tri-track) that I use a PS2 remote control to play with. I have it such that when I press 1 button, it resets the encoders and if I press another button it prints them out.

The program has if defs to try out different modes of the Roboclaw, but some of the code looks like:
Code:
#ifdef ROBOCLAW_PACKET_SERIAL
RCLAWSeroutPin   con   P11
RClawSerinPin   con P10
RClAW_BAUD      con i38400
RCLAW_ADDRESS   con 128
#endif
...
#ifdef ROBOCLAW_PACKET_SERIAL
   IF (DualShock(2).bit1 = 0) and LastButton(1).bit1 THEN   ;R2 Button test
      ;lets print out Encoder information.
      serout RCLAWSeroutPin, RCLAW_BAUD, [RCLAW_ADDRESS, 16] ;read command for M1 encoder
      serin RCLAWSerinPin, RCLAW_BAUD, [REncoderCnt.Byte3, REncoderCnt.Byte2, REncoderCnt.Byte1, REncoderCnt.Byte0, REncoderStat, CRC]
      serout RCLAWSeroutPin, RCLAW_BAUD, [RCLAW_ADDRESS, 17] ;read command for M1 encoder
      serin RCLAWSerinPin, RCLAW_BAUD, [LEncoderCnt.Byte3, LEncoderCnt.Byte2, LEncoderCnt.Byte1, LEncoderCnt.Byte0, LEncoderStat, CRC]
      
      serout s_out, i9600, [   "Right Encoder: ", dec REncoderCnt, " ", hex REncoderStat, |
                        " Left Encoder: ", dec LEncoderCnt, " ", hex LEncoderStat, 13];
   ENDIF

   IF (DualShock(2).bit3 = 0) and LastButton(1).bit3 THEN   ;R1 Button test
      ; Reset the encoder counts
      serout RCLAWSeroutPin, RCLAW_BAUD, [RCLAW_ADDRESS, 20, (RCLAW_ADDRESS + 20) & 0x7f] ;Reset both encoders
   ENDIF
#endif   

Again notice that there are two different IO pins used.

Kurt


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Fri Apr 23, 2010 6:46 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 903
Location: Temecula, CA
Heres some code with several subroutines for all the roboclaw functions. I'm using the ARC32 to drive the roboclaw so I'm using the hardware serial ports. You'll have to modify the code if your using a different module.

Code:
Address con 128

version var byte(32)
status var byte
m1pos var long
m2pos var long
mainbat var word
logicbat var word

ENABLEHSERIAL
ENABLEHSERIAL2

SetHSerial H38400,H8DATABITS,HNOPARITY,H1STOPBITS
SetHSerial2 H38400,H8DATABITS,HNOPARITY,H1STOPBITS

hserout ["Starting...",13]
gosub getversion
hserout ["Versions:",str version\32\0,13]

gosub set0[20]
gosub get2[24],mainbat
gosub get2[25],logicbat
end

;Dont change code below this line

cmd var byte
arg var byte
accel var byte
deccel var byte
speed var long
distance var long
crc var byte
options var byte
value var long

;Command 21   0x15   Read Device Version
getversion
   hserout2 [address,0x15]
   hserin2 [str version\32\0,crc]
   return
   
;Command 24   0x18   Read Main batt voltage
;Command 25   0x19   Read Logic batt voltage
get2[cmd]
   value=0
   hserout2 [address,cmd]
   hserin2 [value.byte1,value.byte0,crc]
   return value

;Command 16   0x10   Read Tick counter M1
;Command 17   0x11   Read Tick counter M2
;Command 18   0x12   Read Speed M1 (encoder)
;Command 19   0x13   Read Speed M2 (encoder)
get5[cmd]
   hserout2 [address,cmd]
   hserin2 [value.byte3,value.byte2,value.byte1,value.byte0,status,crc]
   return value
   
;Command 20   0x14   Reset Quad encoder counters
;Command 22   0x16   Turn On CRC
;Command 23   0x17   Turn Off CRC
;Command 29   0x1D   Stop executing Script/AdvScript
;Command 30   0x1E   Execute Script/AdvScript
set0[cmd]
   crc = (address+cmd)&0x7F
   hserout2 [address,cmd,crc]
   hserin2 2000,set0_finish,[status]
set0_finish
   return
   
;Command 0   0x00   Drive forward M1
;Command 1   0x01   Drive backwards M1
;Command 2   0x02   set Min voltage (Main Batt)
;Command 3   0x03   set Max voltage (Main Batt)
;Command 4   0x04   Drive forward M2
;Command 5   0x05   Drive backwards M2
;Command 6   0x06   Drive M1   7 Bit
;Command 7   0x07   Drive M2   7 Bit
;Command 8   0x08   Drive forward @Mixing mode
;Command 9   0x09   Drive backwards @Mixing mode
;Command 10   0x0A   Turn right @Mixing mode
;Command 11   0x0B   Turn left @Mixing mode
;Command 12   0x0C   Drive forwards/back 7 bit
;Command 13   0x0D   Turn 7 bit
;Command 26   0x1A   set Min voltage (Logic Batt)
;Command 27   0x1B   set Max voltage (Logic Batt)
set1[cmd,arg]
   crc = (address+cmd+arg)&0x7F
   hserout2 [address,cmd,arg,crc]
   return

;Command 32   0x20   Drive forward M1
;Command 33   0x21   Drive backwards M1
;Command 34   0x22   Drive forward M2
;Command 35   0x23   Drive backwards M2
;Command 36   0x24   Drive forward
;Command 37   0x25   Drive backwards
;Command 38   0x26   Turn Right
;Command 39   0x27   Turn Left
set5[cmd,accel,speed]
   crc = (address+cmd+accel+speed.byte3+speed.byte2+speed.byte1+speed.byte0)&0x7F
   hserout2 [address,cmd,speed.byte3,speed.byte2,speed.byte1,speed.byte0,accel,crc]
   return

;Command40   0x28   Drive forward M1
;Command41   0x29   Drive backwards M1
;Command42   0x2A   Drive forward M2
;Command43   0x2B   Drive backwards M2
;Command44   0x2C   Drive forward
;Command45   0x2D   Drive backwards
;Command46   0x2E   Turn Right
;Command47   0x2F   Turn Left
set11[cmd,accel,speed,distance,deccel,options]
   crc = (address + cmd + accel + speed.byte3 + speed.byte2 + speed.byte1 + speed.byte0 + distance.byte3 + distance.byte2 + distance.byte1 + distance.byte0 + deccel + options) & 0x7f
   hserout2 [address, cmd, accel, speed.byte3, speed.byte2, speed.byte1, speed.byte0, distance.byte3, distance.byte2, distance.byte1, distance.byte0, deccel, options, crc]; Drive forward
   return

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Mon Apr 26, 2010 6:44 am 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
Kurt,

What pins on the Roboclaw are you connecting to? I couldn't find documentation for the 2x5A, so I'm using the documentation for the 2x10A. I will go through it again, but I didn't see anything stating that anything more than S1 was to be used in packet serial mode. I have the spare pins, so it can be done, but even the examples were using a single pin.

My P15 is connected to S1 on the roboclaw, as well as +5V and GND via a servo extension cable.

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Mon Apr 26, 2010 6:54 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 528
I am using p10 and P11 as I am using P12-P15 for PS2 control.

If you look at I think page 34 in the document you will see two wires going from the roboclaw to Bap28 (in this case P14/15).

If you look at the example on Page 38 to read the encoder you will see that they do an HSEROUT followed by an HSERIN which is tied to hardware serial port on P14 and P15.

Kurt


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Mon Apr 26, 2010 7:35 am 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
Kurt,

Thanks for the quick reply. However, I was asking what pins on the roboclaw were used. i.e. is P11 (serout) going to S1 on the roboclaw and P10 (serin) going to S2?

edit: I just went to page 34 and saw that the second pin is going to S2. I need to brush up on HSerout/Hserin, as I thought both were using the same pin. Thanks again for the help everyone.

It appears I was confusing the programming pin with the EUSART pins. Therefore, I was mentally relating HSerout/in to that pin, not pins 14/15. A few program changes and an additional wire should do the trick. I will update with results...

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Mon Apr 26, 2010 1:39 pm 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
I am apparently missing something. Does the Nano not have the ability to set the hardware serial port outside of baud rate? This line gives me errors if I put anything after H19200
Code:
SetHSerial H19200, H8DATABITS, HNOPARITY, H1STOPBITS

I commented out everything after H19200 and commanded the motor to run using Hserout. It does nothing now.

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Tue Apr 27, 2010 12:26 pm 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
I'm still plugging away, but still not anywhere close to making this thing behave correctly. I decided to go back to the BS2. I had to reduce the BAUD on the Roboclaw to 2400, and did so by switching off DIP 5. 2400/8/None/1 in the BS2 Serin/out is 396. I can command the motor, and I'm getting some kind of feedback from the encoder. At first glance, it even appears to make sense. BS2 code

Code:
' {$STAMP BS2}
' {$PBASIC 2.5}
Encoder1 VAR Byte
Encoder2 VAR Byte
Encoder3 VAR Byte
Encoder4 VAR Byte
Status VAR Byte
CRC VAR Byte
x VAR Byte

PAUSE 2000
CRC = %11001100 & %01111111
SEROUT 15, 396, [128, 0, 76, CRC]    'M1 Forward
PAUSE 2000

FOR x = 1 TO 100
  PAUSE 50
  SEROUT 15, 396, [128, 18]          'Get Speed
  SERIN 0, 396, [Encoder1, Encoder2, Encoder3, Encoder4, Status, CRC]
  DEBUG ? x, ? Encoder1, ? Encoder2, ? Encoder3, ? Encoder4, ? Status, CR
NEXT

CRC = %10000000 & %01111111
SEROUT 15, 396, [128, 0, 0, CRC]    'M1 Stop


The encoder is 100 PPR per it's documentation. The gearbox is 127.7:1 from the motor to the output shaft. The two upper bytes of the encoder don't appear to be reading at all. What confuses me further is that I'm not sure the motor is spinning fast enough to even need them. I compiled a quick table

Code:
Speed CMD      Byte1  Byte0  Out RPM Motor RPM   PPM   PPS
40              120   157      18   2298.6   229860   3831
60              188   162      29   3703.3   370330   6172.166667
68              216   155      32   4086.4   408640   6810.666667
72              230   122      35   4469.5   446950   7449.166667
76              244   149      36   4597.2   459720   7662
80              0     129      39   4980.3   498030   8300.5
100             67    146      48   6129.6   612960   10216


Byte2 and Byte 3 of the encoder are always 0. The above numbers were taken by averaging 100 readings for each speed CMD. At a speed CMD of 100, the theoretical PPS of the encoder is just over 10,000. This wouldn't require Byte3 at all, but it's pretty clear there is a rollover between speed 76 and 80.

My calculation is PPS = Out RPM * 127.7 gear * 100 PPR / 60 sec. The received pulse count doesn't match this at all. Any ideas?

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Tue Apr 27, 2010 2:13 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 903
Location: Temecula, CA
The nano sethserial only has a baud rate setting. The PICmicros don't have all the features the Renesas processors have so there is no parity,stop bits etc... settings.

Nano/Atom sethserial syntax

sethserial baudraate

AtomPro sethserial syntax

sethserial baudrate,databits,parity,stopbits

The popup syntax help only shows the AtomPro syntax currently. On the Atom/Nano the LCD commands and sethserial command have different syntax. All other commands are identical syntax IIRC.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Wed Apr 28, 2010 8:32 am 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
Well then...

What are the defaults for the Nano Hserial, 8/None/1/Inverted? i.e. Can I SetHSerial H19200, and it should readily work with the roboclaw?
If not, is it possible to use serin/serout instead to get quadrature encoder data from the roboclaw? What format should this be in. Serin p14, i19200... doesn't seem to be working.

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Wed Apr 28, 2010 8:52 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 528
I don't have any Nanos so can not answer all of this. But I do use my Roboclaw on non-hserial IO pins and I do use standard serial input/output. I suppose there could be a race condition if your code can not enter the serin command quick enough, but it has worked for me on the pro.

Kurt


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Wed Apr 28, 2010 11:40 am 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
Kurt,

The Nano's speed is a valid issue. The pro's have a much faster scan time. Nathan at Basic Micro is going to run a few sample programs with a nano and see if that is it.

I have moved on to reading distance, and it is working like a charm on the BS2. The magic factor is 4 for a QUADrature decoder. Credit to Nathan once again.

My todo list:
Check CRC after a read speed command with the BS2 to see if I am losing data. - CHECKED. Assuming that only the returned data is to be used to calculate the CRC (not including the address or command) none of my CRC's checkout. The BS2 must be missing the beginning of the transmission, but still fills what data it can and then resumes.

Trying getting data from the roboclaw at 2400 baud using the Nano 40

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Last edited by litebrite2001 on Wed Apr 28, 2010 11:54 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Wed Apr 28, 2010 11:49 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 903
Location: Temecula, CA
I think with the Nanos it's not so much reading the data at speed but getting into the serin command fast enough not to loose any data the RoboClaw is sending back. I'll let you know more once I've had the chance to work with the Nano and a RoboClaw.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Reading Quadrature Encoder with Roboclaw 2x5A
PostPosted: Wed Apr 28, 2010 1:15 pm 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
Progress by leaps and bounds...

Can someone point me to an explanation of S_OUT? I found examples, but no details. I was able to make it work so that I could run the program without debug mode and still get data on my computer screen. Really, it was just fumbling around though.

I now have hserin AND serin working on the Nano 40. Debug was the culprit. Thanks to Dave K for that info. Now I just need to run through and check validity of my readings.

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 73 posts ]  Go to page 1, 2, 3, 4, 5  Next

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO