1
0
Fork 0
mirror of https://github.com/iNavFlight/inav.git synced 2025-07-24 16:55:29 +03:00

Remove serial_cli.c's dependency on mw.h/board.h.

Rename yawdeadband to yaw_deadband to match other variables/cli
commands.

Fix a couple of usages of MAX_MOTORS where MAX_SUPPORTED_MOTORS should
be used instead.
This commit is contained in:
Dominic Clifton 2014-04-23 20:42:38 +01:00
parent bcc55abcc8
commit a5f0999c26
12 changed files with 250 additions and 220 deletions

View file

@ -1,16 +1,42 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "board.h"
#include "mw.h"
#include "platform.h"
#include "common/axis.h"
#include "common/printf.h"
#include "common/typeconversion.h"
#include "drivers/system_common.h"
#include "drivers/accgyro_common.h"
#include "drivers/serial_common.h"
#include "drivers/bus_i2c.h"
#include "serial_common.h"
#include "flight_common.h"
#include "flight_mixer.h"
#include "rc_controls.h"
#include "boardalignment.h"
#include "telemetry_common.h"
#include "gps_common.h"
#include "rx_common.h"
#include "battery.h"
#include "sensors_common.h"
#include "sensors_acceleration.h"
#include "sensors_gyro.h"
#include "sensors_barometer.h"
#include "failsafe.h"
#include "runtime_config.h"
#include "config.h"
#include "config_profile.h"
#include "config_master.h"
#include "serial_cli.h"
// we unset this on 'exit'
extern uint8_t cliMode;
@ -31,14 +57,7 @@ static void cliSet(char *cmdline);
static void cliStatus(char *cmdline);
static void cliVersion(char *cmdline);
// from sensors.c
extern uint8_t batteryCellCount;
// from config.c RC Channel mapping
extern const char rcChannelLetters[];
// from mixer.c
extern int16_t motor_disarmed[MAX_MOTORS];
uint16_t cycleTime; // FIXME dependency on mw.c
// signal that we're in cli mode
uint8_t cliMode = 0;
@ -47,9 +66,6 @@ uint8_t cliMode = 0;
static char cliBuffer[48];
static uint32_t bufferIndex = 0;
static float _atof(const char *p);
static char *ftoa(float x, char *floatString);
// sync this with MultiType enum from mw.h
static const char * const mixerNames[] = {
"TRI", "QUADP", "QUADX", "BI",
@ -169,7 +185,7 @@ const clivalue_t valueTable[] = {
{ "gyro_cmpfm_factor", VAR_UINT16, &masterConfig.gyro_cmpfm_factor, 100, 1000 },
{ "pid_controller", VAR_UINT8, &currentProfile.pidController, 0, 1 },
{ "deadband", VAR_UINT8, &currentProfile.deadband, 0, 32 },
{ "yawdeadband", VAR_UINT8, &currentProfile.yawdeadband, 0, 100 },
{ "yawdeadband", VAR_UINT8, &currentProfile.yaw_deadband, 0, 100 },
{ "alt_hold_throttle_neutral", VAR_UINT8, &currentProfile.alt_hold_throttle_neutral, 1, 250 },
{ "alt_hold_fast_change", VAR_UINT8, &currentProfile.alt_hold_fast_change, 0, 1 },
{ "throttle_correction_value", VAR_UINT8, &currentProfile.throttle_correction_value, 0, 150 },
@ -248,184 +264,6 @@ static void cliPrintVar(const clivalue_t *var, uint32_t full);
static void cliPrint(const char *str);
static void cliWrite(uint8_t ch);
#ifndef HAVE_ITOA_FUNCTION
/*
** The following two functions together make up an itoa()
** implementation. Function i2a() is a 'private' function
** called by the public itoa() function.
**
** itoa() takes three arguments:
** 1) the integer to be converted,
** 2) a pointer to a character conversion buffer,
** 3) the radix for the conversion
** which can range between 2 and 36 inclusive
** range errors on the radix default it to base10
** Code from http://groups.google.com/group/comp.lang.c/msg/66552ef8b04fe1ab?pli=1
*/
static char *i2a(unsigned i, char *a, unsigned r)
{
if (i / r > 0)
a = i2a(i / r, a, r);
*a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % r];
return a + 1;
}
char *itoa(int i, char *a, int r)
{
if ((r < 2) || (r > 36))
r = 10;
if (i < 0) {
*a = '-';
*i2a(-(unsigned)i, a + 1, r) = 0;
} else
*i2a(i, a, r) = 0;
return a;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// String to Float Conversion
///////////////////////////////////////////////////////////////////////////////
// Simple and fast atof (ascii to float) function.
//
// - Executes about 5x faster than standard MSCRT library atof().
// - An attractive alternative if the number of calls is in the millions.
// - Assumes input is a proper integer, fraction, or scientific format.
// - Matches library atof() to 15 digits (except at extreme exponents).
// - Follows atof() precedent of essentially no error checking.
//
// 09-May-2009 Tom Van Baak (tvb) www.LeapSecond.com
//
#define white_space(c) ((c) == ' ' || (c) == '\t')
#define valid_digit(c) ((c) >= '0' && (c) <= '9')
static float _atof(const char *p)
{
int frac = 0;
float sign, value, scale;
// Skip leading white space, if any.
while (white_space(*p) ) {
p += 1;
}
// Get sign, if any.
sign = 1.0f;
if (*p == '-') {
sign = -1.0f;
p += 1;
} else if (*p == '+') {
p += 1;
}
// Get digits before decimal point or exponent, if any.
value = 0.0f;
while (valid_digit(*p)) {
value = value * 10.0f + (*p - '0');
p += 1;
}
// Get digits after decimal point, if any.
if (*p == '.') {
float pow10 = 10.0f;
p += 1;
while (valid_digit(*p)) {
value += (*p - '0') / pow10;
pow10 *= 10.0f;
p += 1;
}
}
// Handle exponent, if any.
scale = 1.0f;
if ((*p == 'e') || (*p == 'E')) {
unsigned int expon;
p += 1;
// Get sign of exponent, if any.
frac = 0;
if (*p == '-') {
frac = 1;
p += 1;
} else if (*p == '+') {
p += 1;
}
// Get digits of exponent, if any.
expon = 0;
while (valid_digit(*p)) {
expon = expon * 10 + (*p - '0');
p += 1;
}
if (expon > 308)
expon = 308;
// Calculate scaling factor.
// while (expon >= 50) { scale *= 1E50f; expon -= 50; }
while (expon >= 8) { scale *= 1E8f; expon -= 8; }
while (expon > 0) { scale *= 10.0f; expon -= 1; }
}
// Return signed and scaled floating point result.
return sign * (frac ? (value / scale) : (value * scale));
}
///////////////////////////////////////////////////////////////////////////////
// FTOA
///////////////////////////////////////////////////////////////////////////////
static char *ftoa(float x, char *floatString)
{
int32_t value;
char intString1[12];
char intString2[12] = { 0, };
char *decimalPoint = ".";
uint8_t dpLocation;
if (x > 0) // Rounding for x.xxx display format
x += 0.0005f;
else
x -= 0.0005f;
value = (int32_t) (x * 1000.0f); // Convert float * 1000 to an integer
itoa(abs(value), intString1, 10); // Create string from abs of integer value
if (value >= 0)
intString2[0] = ' '; // Positive number, add a pad space
else
intString2[0] = '-'; // Negative number, add a negative sign
if (strlen(intString1) == 1) {
intString2[1] = '0';
intString2[2] = '0';
intString2[3] = '0';
strcat(intString2, intString1);
} else if (strlen(intString1) == 2) {
intString2[1] = '0';
intString2[2] = '0';
strcat(intString2, intString1);
} else if (strlen(intString1) == 3) {
intString2[1] = '0';
strcat(intString2, intString1);
} else {
strcat(intString2, intString1);
}
dpLocation = strlen(intString2) - 3;
strncpy(floatString, intString2, dpLocation);
floatString[dpLocation] = '\0';
strcat(floatString, decimalPoint);
strcat(floatString, intString2 + dpLocation);
return floatString;
}
static void cliPrompt(void)
{
cliPrint("\r\n# ");
@ -474,7 +312,7 @@ static void cliCMix(char *cmdline)
if (len == 0) {
cliPrint("Custom mixer: \r\nMotor\tThr\tRoll\tPitch\tYaw\r\n");
for (i = 0; i < MAX_MOTORS; i++) {
for (i = 0; i < MAX_SUPPORTED_MOTORS; i++) {
if (masterConfig.customMixer[i].throttle == 0.0f)
break;
num_motors++;
@ -497,7 +335,7 @@ static void cliCMix(char *cmdline)
return;
} else if (strncasecmp(cmdline, "reset", 5) == 0) {
// erase custom mixer
for (i = 0; i < MAX_MOTORS; i++)
for (i = 0; i < MAX_SUPPORTED_MOTORS; i++)
masterConfig.customMixer[i].throttle = 0.0f;
} else if (strncasecmp(cmdline, "load", 4) == 0) {
ptr = strchr(cmdline, ' ');
@ -519,25 +357,25 @@ static void cliCMix(char *cmdline)
} else {
ptr = cmdline;
i = atoi(ptr); // get motor number
if (--i < MAX_MOTORS) {
if (--i < MAX_SUPPORTED_MOTORS) {
ptr = strchr(ptr, ' ');
if (ptr) {
masterConfig.customMixer[i].throttle = _atof(++ptr);
masterConfig.customMixer[i].throttle = fastA2F(++ptr);
check++;
}
ptr = strchr(ptr, ' ');
if (ptr) {
masterConfig.customMixer[i].roll = _atof(++ptr);
masterConfig.customMixer[i].roll = fastA2F(++ptr);
check++;
}
ptr = strchr(ptr, ' ');
if (ptr) {
masterConfig.customMixer[i].pitch = _atof(++ptr);
masterConfig.customMixer[i].pitch = fastA2F(++ptr);
check++;
}
ptr = strchr(ptr, ' ');
if (ptr) {
masterConfig.customMixer[i].yaw = _atof(++ptr);
masterConfig.customMixer[i].yaw = fastA2F(++ptr);
check++;
}
if (check != 4) {
@ -546,7 +384,7 @@ static void cliCMix(char *cmdline)
cliCMix("");
}
} else {
printf("Motor number must be between 1 and %d\r\n", MAX_MOTORS);
printf("Motor number must be between 1 and %d\r\n", MAX_SUPPORTED_MOTORS);
}
}
}
@ -569,7 +407,7 @@ static void cliDump(char *cmdline)
// print custom mix if exists
if (masterConfig.customMixer[0].throttle != 0.0f) {
for (i = 0; i < MAX_MOTORS; i++) {
for (i = 0; i < MAX_SUPPORTED_MOTORS; i++) {
if (masterConfig.customMixer[i].throttle == 0.0f)
break;
thr = masterConfig.customMixer[i].throttle;
@ -795,8 +633,8 @@ static void cliMotor(char *cmdline)
pch = strtok(NULL, " ");
}
if (motor_index < 0 || motor_index >= MAX_MOTORS) {
printf("No such motor, use a number [0, %d]\r\n", MAX_MOTORS);
if (motor_index < 0 || motor_index >= MAX_SUPPORTED_MOTORS) {
printf("No such motor, use a number [0, %d]\r\n", MAX_SUPPORTED_MOTORS);
return;
}
@ -952,7 +790,7 @@ static void cliSet(char *cmdline)
eqptr++;
len--;
value = atoi(eqptr);
valuef = _atof(eqptr);
valuef = fastA2F(eqptr);
for (i = 0; i < VALUE_COUNT; i++) {
val = &valueTable[i];
if (strncasecmp(cmdline, valueTable[i].name, strlen(valueTable[i].name)) == 0) {