# Setting pololu speed & acceleration via serial

Hello,

I’m a bit confused on how to set the speed and acceleration via ttl.

The manual states:

Set Speed
Compact protocol: 0x87, channel number, speed low bits, speed high bits
Set Acceleration
Compact protocol: 0x89, channel number, acceleration low bits, acceleration high bits

How do I calculate the low / high bits? That’s confusing me.

I’ve successfully got the set target working with decimal to hex conversion, example in vb.net:

ServoAction = “&H” & Hex\$(“255”)
ServoNumber = “&H” & Hex\$(“0”)
ServoPosition = “&H” & Hex\$("127)
Dim xmtBuf() As Byte = {ServoAction, ServoNumber, ServoPosition}

Can someone help me out with a statement on how to set speed / acceleration?

Hello,

Did you look at the Set Target (Compact/Pololu protocol) command in the User’s Guide? There is an example there about how to calculate the low and high bits. The example is in C, but many languages have a similar syntax.

-Paul

Hello Paul,

Yes, I did look at the manual but it’s not really helpful.

For example, if we want to set the target of servo 0 to 1500 µs, we could send the following byte sequence:
in decimal: 132, 0, 112, 46

I just need to know how to calculate the 112/46 digits, that part I don’t really understand.

In the manual for c# it shows:
serialBytes[2] = target & 0x7F; // Second byte holds the lower 7 bits of target.
serialBytes[3] = (target >> 7) & 0x7F; // Third data byte holds the bits 7-13 of target.

0x7f = 127 in decimal, but I don’t understand the rest.

I’m not an engineer or anything, just an amateur programmer

Hello.

If you want to better understand what those computations are doing, here is a good resource for learning about bitwise operations:

en.wikipedia.org/wiki/Bitwise_operation

I can’t tell you how to write the VB equivalents of the C operators in the manual, but it shouldn’t be hard to find a VB reference online that has such information.

The whole point of those computations is to break a 14-bit number up into two 7-bit numbers, each of which can be sent as a separate data byte in your serial command packet. To do this, you need to set the first data byte equal to the lowest seven bits of your number:

serialBytes[2] = target & 0x7F;

In binary, 0x7F is 01111111. Bitwise-ANDing (&) this value with target preserves the low seven bits while clearing all the others, since 1 & x = x while 0 & x = 0.

To get the appropriate bits in to the second data byte, we need to bit-shift (>>) our target appropriately and once again mask the desired bits with a bitwise AND:

serialBytes[3] = (target >> 7) & 0x7F;

By the way, please make sure you specify which product you are using, especially when we have multiple similar products with different protocols. I know you said it in another thread, but it’s helpful to see it directly in the thread one is responding to.

- Ben

Hello,

Sorry about that, this is for the maestro 18 channel.

Someone on stackoverflow helped me out, here is the code for vb.net in case anyone else needs it:

serialBytes(2) = CType(value And &H7F, Byte)
serialBytes(3) = CType((value >> 7) And &H7F, Byte)

Thanks for posting what you’ve discovered. I’m sure that will be helpful to others trying to use VB to interact with our controllers.

- Ben

No problem.

The project I’m working on will no doubt impress all, I’ll post up a video once it’s completed and maybe just maybe open source it

That sounds exciting. I look forward to seeing your video and hearing more about your project!

- Ben