Problem with new Serial Protocol

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
User avatar
LuisFCorreia
Posts: 32
Joined: Sat Oct 01, 2011 7:04 pm
Location: Portugal
Contact:

Problem with new Serial Protocol

Post by LuisFCorreia »

Hi there,

I've been fighting for the last 4 days with changing my program to cope with new Serial Protocol.

You can check the code here https://github.com/luisfcorreia/bi-copter-wii-remote.
The relevant file is Controller.java.

(this is part on an Android app and a lot of stuff is ommited)

Code: Select all

            /*
             * Values now go from 0-100 for Throttle All others go from -50
             * +50
             */
            mt = mThr;
            my = mYaw - 50;
            mr = mRol - 50;
            mp = mPit - 50;
            ma = (arm * 100) - 50;

            /*
             * factor in the influence in percentage
             */
            mr = (int) (mr * aaRol);
            mp = (int) (mp * aaPit);
            my = (int) (my * aaYaw);

            /*
             * translate values to pulse data
             */
            THROTTLE = THROTTLE + (mt * 6);
            ROLL = ROLL + (mr * 6);
            PITCH = PITCH + (mp * 6);
            YAW = YAW + (my * 6);
            AUX1 = AUX1 + (ma * 6);

            Log.i(TAG, "r " + ROLL);
            Log.i(TAG, "p " + PITCH);
            Log.i(TAG, "y " + YAW);
            Log.i(TAG, "t " + THROTTLE);
            Log.i(TAG, "a " + AUX1);

            /*
             * MultiWii header
             */
            /*
             * comando[0] = '$'; comando[1] = 'M'; comando[2] = '<';
             */
            mw[0] = 0x10;
            mw[1] = 0xc8;

            /*
             * #define ROLL 0
             */
            mw[2] = (char) (ROLL & 0xff);
            mw[3] = (char) (ROLL >> 8);

            /*
             * #define PITCH 1
             */
            mw[4] = (char) (PITCH & 0xff);
            mw[5] = (char) (PITCH >> 8);

            /*
             * #define YAW 2
             */
            mw[6] = (char) (YAW & 0xff);
            mw[7] = (char) (YAW >> 8);

            /*
             * #define THROTTLE 3
             */
            mw[8] = (char) (THROTTLE & 0xff);
            mw[9] = (char) (THROTTLE >> 8);

            /*
             * #define AUX1 4
             */
            mw[10] = (char) (AUX1 & 0xff);
            mw[11] = (char) (AUX1 >> 8);

            /*
             * #define AUX2 5
             */
            mw[12] = (char) (AUX2 & 0xff);
            mw[13] = (char) (AUX2 >> 8);

            /*
             * #define AUX3 6
             */
            mw[14] = (char) (AUX3 & 0xff);
            mw[15] = (char) (AUX3 >> 8);

            /*
             * #define AUX4 7
             */
            mw[16] = (char) (AUX4 & 0xff);
            mw[17] = (char) (AUX4 >> 8);

            /*
             * calculate checksum
             */
            checksum = 0;
            for (int i = 0; i < 17; i++) {
               checksum ^= (mw[i] & 0xFF);
            }
            mw[17] = (char) checksum;

            /*
             * Send string to MultiWii device
             */
            comms = String.valueOf(mw);
            bt.write("$M<" + comms);

            Log.i(TAG, "$M<" + comms);




It is using the 2.1 MultiWii Release code with no changes except config.h

I've read back and forth the multiwiiconf gui code to learn how to create a proper checksum and I'm out of ideas.

I really don't want to dumb down multiwii code to use my old version.

Any help will be apreciated.

Thanks,
Luis

Mis
Posts: 203
Joined: Fri Apr 01, 2011 12:23 am

Re: Problem with new Serial Protocol

Post by Mis »

for (int i = 0; i < 18; i++) {
checksum ^= (mw[i] & 0xFF);
}
mw[18] = (char) checksum;

User avatar
LuisFCorreia
Posts: 32
Joined: Sat Oct 01, 2011 7:04 pm
Location: Portugal
Contact:

Re: Problem with new Serial Protocol

Post by LuisFCorreia »

It would be nice if it was an off-by-one error.

unfortunately it isn't.

and I don't have an reply from multiwii, not even an error.

Mis
Posts: 203
Joined: Fri Apr 01, 2011 12:23 am

Re: Problem with new Serial Protocol

Post by Mis »

Working piece of code from my OSD for sending MSP commands ( u08 = uint8_t type ):

Code: Select all

void MSP_data(u08 cmd, u08* ptr, u08 len)
{
   u08 crc=len;
   TX_ON;
   uart_send('$');
   uart_send('M');
   uart_send('<');
   uart_send(len);
   uart_send(cmd);
   crc ^= cmd;
   while(len--)
   {
      crc ^= *ptr;
      uart_send(*ptr++);
   }
   uart_send(crc);
   TX_OFF;
}

pm1
Posts: 136
Joined: Sun Jan 22, 2012 7:26 pm

Re: Problem with new Serial Protocol

Post by pm1 »

u08 crc=len; => checksum = 18;

Mis
Posts: 203
Joined: Fri Apr 01, 2011 12:23 am

Re: Problem with new Serial Protocol

Post by Mis »

pm1 wrote:u08 crc=len; => checksum = 18;

This is only some simplification. Checksum should be calculated from len field, command field and all payload datas. But, zero ^ len field = len field. In this case i can write "checksum = len" instead "checksum = 0; checksum ^= len"


Post Reply