BaseFlight CLI - FabLabSeoul/WingProject GitHub Wiki

Multiwii BaseFlight CLI (Command Line Interface)

CLI๋Š” ์งง์€ ๋ช…๋ น์–ด๋กœ ์ฟผ๋“œ์ฝฅํ„ฐ์˜ ์ƒํƒœ๋ฅผ ์•Œ์•„์˜ค๊ณ , ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ์šฉ๋„๋กœ ์“ฐ์ธ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด, ์˜ˆ์ „ DOS์ฐฝ ํ„ฐ๋ฏธ๋„์—์„œ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ๊ณผ ๋˜‘๊ฐ™๋‹ค.

Naze32 ๋ณด๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, CleanFlight๋‚˜ BaseFlight ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•ด์„œ ๋ณด๋“œ์˜ ์ƒํƒœ๋ฅผ ์•Œ๋ฉด๋˜๋‹ˆ, ๊ตณ์ด CLI๋ฅผ ๊ฑด๋“ค ์ผ์ด ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ BaseFlight ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜, ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์„ ๋„ฃ๊ณ ์ž ํ•œ๋‹ค๋ฉด, ๊ธฐ์กด BaseFlight๋ชจ๋‹ˆํ„ฐ๋ง ํ”„๋กœ๊ทธ๋žจ์œผ๋กœ๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค.

๋‚ ๊ฐœ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” Naze32 ๋ณด๋“œ์˜ ์ƒํƒœ(์ž์„ธ์ •๋ณด, ๋ชจํ„ฐ์ถ”๋ ฅ์ •๋ณด)๋ฅผ ์•Œ์•„์˜ค๊ณ , ์ถ”๊ฐ€ํ•œ ์†Œ์Šค์ฝ”๋“œ์˜ ๋ณ€์ˆ˜๋ฅผ ์ฒดํฌํ•˜๊ธฐ์œ„ํ•ด CLI ํ”„๋กœํ† ์ฝœ์„ ์ˆ˜์ •ํ•ด์•ผ ํ–ˆ๋‹ค.

์šฐ์„  BaseFlight์˜ CLI ๋ถ„์„ํ•ด๋ณด์ž.

Naze32์—์„œ CLI๋Š” ์‹œ๋ฆฌ์–ผํ†ต์‹ ์œผ๋กœ ์ •๋ณด๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š”๋‹ค. ๋ณด๋“œ์— USB๊ฐ€ ๊ผฝํ˜€์žˆ๋‹ค๋ฉด, USB๋ฅผ ํ†ตํ•ด ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์œผ๋กœ CLI๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Baseflight์—์„œ๋Š” serial.h, serial.c, cli.h, cli.c ์—์„œ ์ด ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค. ๋ณด๋“œ์™€ CLIํ†ต์‹ ์„ ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋Š” WingProject\VisualStudio\Naze32SerialCom ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ณด๋“œ์— USB๋ฅผ ๊ผฝ์ง€ ์•Š๊ณ , CLI ํ†ต์‹ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” BaseFlight External Serial ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์ž.

๋ช…๋ น์–ด๋ฅผ ์ „์†กํ•˜๋ฉด, ๊ทธ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณด๋“œ์—์„œ ๋ณด๋‚ด์ค€๋‹ค.

๋ช…๋ น์–ด ์ „์†ก

  • Protocol
    • $
    • M
    • <
    • ์ „์†กํ•  ๋ฐ์ดํƒ€ ํฌ๊ธฐ (๋ฐ”์ดํŠธ๋‹จ์œ„)
    • ๋ช…๋ น์–ด BaseFlight CLI Command
    • ์ „์†กํ•  ๋ฐ์ดํƒ€1
    • ์ „์†กํ•  ๋ฐ์ดํƒ€2
    • ...
    • ์ „์†กํ•  ๋ฐ์ดํƒ€n (์ „์†กํ•  ๋ฐ์ดํƒ€ ๊ฐœ์ˆ˜๋งŒํผ)
    • ์ฒดํฌ์„ฌ
      • ์ „์†กํ•  ๋ฐ์ดํƒ€ ํฌ๊ธฐ ๊ฐ’ ๋ถ€ํ„ฐ ์ดํ›„ ๋ชจ๋“  ์ •๋ณด๋ฅผ xor ํ•œ ๊ฐ’.
void SendCommand(CBufferedSerial &serial, const unsigned char cmd)
{
	unsigned char packet[64];
	int checksum = 0;
	int idx = 0;
	packet[idx++] = '$';
	packet[idx++] = 'M';
	packet[idx++] = '<';
	packet[idx++] = 0;
	checksum ^= 0;
	packet[idx++] = cmd;
	checksum ^= cmd;
	packet[idx++] = checksum;
	serial.SendData((char*)packet, idx);
}

๋ช…๋ น์–ด ์ˆ˜์‹ 

  • Protocol
    • $
    • M
    • ์ˆ˜์‹ ํ•  ๋ฐ์ดํƒ€ ํฌ๊ธฐ (๋ฐ”์ดํŠธ๋‹จ์œ„)
    • ๋ช…๋ น์–ด BaseFlight CLI Command
    • ์ˆ˜์‹  ๋ฐ์ดํƒ€1
    • ์ˆ˜์‹  ๋ฐ์ดํƒ€2
    • ...
    • ์ˆ˜์‹  ๋ฐ์ดํƒ€n (์ˆ˜์‹  ๋ฐ์ดํƒ€ ๊ฐœ์ˆ˜๋งŒํผ)
    • ์ฒดํฌ์„ฌ
      • ์ˆ˜์‹ ํ•œ ๋ฐ์ดํƒ€ ํฌ๊ธฐ ๊ฐ’ ๋ถ€ํ„ฐ ์ดํ›„ ๋ชจ๋“  ์ •๋ณด๋ฅผ xor ํ•œ ๊ฐ’.
// ๊ฐ„๋‹จํ•œ ์Šคํ…Œ์ดํŠธ ๋จธ์‹ ์œผ๋กœ ๊ตฌํ˜„๋˜์—ˆ๋‹ค.
void RecvCommand(CBufferedSerial &serial)
{
	int state = 0;
	int len = 0;
	int readLen = 0;
	int msp = 0;
	int noDataCnt = 0;
	int checkSum = 0;
	while (1)
	{
		unsigned char c;
		if (serial.ReadData(&c, 1) <= 0)
		{
			Sleep(1);
			++noDataCnt;
			if (noDataCnt > 500)
				break; // exception
			continue;
		}

		switch (state)
		{
		case 0:
		{
			state = (c == '$') ? 1 : 0;
			cout << c;
		}
		break;
		
		case 1:
		{
			state = (c == 'M') ? 2 : 0;
			cout << c;
		}
		break;

		case 2:
		{
			state = (c == '>') ? 3 : 0;
			cout << c;
		}
		break;

		case 3:
		{
			len = c;
			cout << (int)c;
			checkSum ^= c;
			state = 4;
		}
		break;

		case 4:
		{
			msp = c;
			cout << (int)c << " ";
			checkSum ^= c;
			state = 5;
		}
		break;

		case 5:
		{
			if (len > readLen)
			{
				checkSum ^= c;
				cout << (int)c << " ";
			}
			else
			{
				if (checkSum == c)
				{
					cout << "ok" << endl;
					return; // end;
				}
				else
				{
					cout << "error!!" << endl;
					return; // end;
				}
			}

			++readLen;
		}
		break;

		default:
			break;
		}
	}
}