Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial debug #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ OBJDIR = ./.obj/


# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c motors.c gyros.c receiver.c settings.c pid.c

SRC = $(TARGET).c motors.c gyros.c receiver.c settings.c pid.c soft_serial.c

# List C++ source files here. (C dependencies are automatically generated.)
CPPSRC =
Expand Down Expand Up @@ -273,10 +272,10 @@ LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
# Type: avrdude -c ?
# to get a full listing.
#
AVRDUDE_PROGRAMMER = usbasp-clone
AVRDUDE_PROGRAMMER = usbasp

# com1 = serial port. Use lpt1 to connect to parallel port.
AVRDUDE_PORT = usb # programmer connected to serial device
AVRDUDE_PORT = /dev/ttyUSB0 # programmer connected to serial device

AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
Expand Down
7 changes: 6 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include <util/delay.h>

#include "typedefs.h"

#define SAVE_CENTER
#define SERIAL_PORT
#define BT_SERIAL_PORT

#include "io_cfg.h"

/* Multicopter Type */
Expand All @@ -22,4 +27,4 @@
//#define HEX_COPTER
//#define Y6_COPTER

#endif
#endif
15 changes: 15 additions & 0 deletions io_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,31 @@

#define M1 REGISTER_BIT(PORTB,2)
#define M2 REGISTER_BIT(PORTB,1)
#ifdef SERIAL_PORT
#define M3 REGISTER_BIT(PORTD,6)
#define M4 REGISTER_BIT(PORTD,5)
#define SER_TX REGISTER_BIT(PORTB,0)
#define SER_RX REGISTER_BIT(PORTD,7)
#define SERIAL_RX_M4
#else
#define M3 REGISTER_BIT(PORTB,0)
#define M4 REGISTER_BIT(PORTD,7)
#define M5 REGISTER_BIT(PORTD,6)
#define M6 REGISTER_BIT(PORTD,5)
#endif
#define M1_DIR REGISTER_BIT(DDRB,2)
#define M2_DIR REGISTER_BIT(DDRB,1)
#ifdef SERIAL_PORT
#define M3_DIR REGISTER_BIT(DDRD,6)
#define M4_DIR REGISTER_BIT(DDRD,5)
#define SER_TX_DIR REGISTER_BIT(DDRB,0)
#define SER_RX_DIR REGISTER_BIT(DDRD,7)
#else
#define M3_DIR REGISTER_BIT(DDRB,0)
#define M4_DIR REGISTER_BIT(DDRD,7)
#define M5_DIR REGISTER_BIT(DDRD,6)
#define M6_DIR REGISTER_BIT(DDRD,5)
#endif

#define LED REGISTER_BIT(PORTB,6)
#define LED_DIR REGISTER_BIT(DDRB,6)
Expand Down
91 changes: 82 additions & 9 deletions kk.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "settings.h"
#include "receiver.h"
#include "motors.h"
#include "soft_serial.h"

bool Armed;

Expand All @@ -17,10 +18,30 @@ static void setup()
{
MCUCR = _BV(PUD); // Disable hardware pull-up

settingsSetup();
receiverSetup();
gyrosSetup();
motorsSetup();
settingsSetup();
#ifdef SERIAL_PORT
soft_serial( false );
ss_begin(9600);

ss_write_str( "\r\n\r\nConfig.RollGyroDirection = " );
ss_write_num( Config.RollGyroDirection );
ss_write_str( "\r\nConfig.PitchGyroDirection = " );
ss_write_num( Config.PitchGyroDirection );
ss_write_str( "\r\nConfig.YawGyroDirection = " );
ss_write_num( Config.YawGyroDirection );
ss_write_str( "\r\nConfig.CenterRollValue = " );
ss_write_num( Config.CenterRollValue );
ss_write_str( "\r\nConfig.CenterPitchValue = " );
ss_write_num( Config.CenterPitchValue );
ss_write_str( "\r\nConfig.CenterCollValue = " );
ss_write_num( Config.CenterCollValue );
ss_write_str( "\r\nConfig.CenterYawValue = " );
ss_write_num( Config.CenterYawValue );
ss_write( '\r\n' );
#endif

LED_DIR = OUTPUT;
LED = 0;
Expand All @@ -43,26 +64,68 @@ static void setup()
* Flash the LED once at power on
*/
LED = 1;
_delay_ms(150);
_delay_ms(250);
LED = 0;

sei();

_delay_ms(1500);
_delay_ms(2000);

ReadGainPots();
ReadGainPots();
#ifdef SERIAL_PORT
ReadGyros();
ss_write_str( "\r\nPitch gain = " );
ss_write_num( GainInADC[PITCH] );
ss_write_str( "\r\nRoll gain = " );
ss_write_num( GainInADC[ROLL] );
ss_write_str( "\r\nYaw gain = " );
ss_write_num( GainInADC[YAW] );
ss_write_str( "\r\nPitch gyro = " );
ss_write_num( gyroADC[PITCH] );
ss_write_str( "\r\nRoll gyro = " );
ss_write_num( gyroADC[ROLL] );
ss_write_str( "\r\nYaw gyro = " );
ss_write_num( gyroADC[YAW] );
ss_write( '\r\n' );
#endif

bool pitchMin = (GainInADC[PITCH] < (ADC_MAX * 5) / 100); // 5% threshold
bool rollMin = (GainInADC[ROLL] < (ADC_MAX * 5) / 100); // 5% threshold
bool yawMin = (GainInADC[YAW] < (ADC_MAX * 5) / 100); // 5% threshold

if(pitchMin && rollMin && yawMin) { settingsClearAll(); } // Clear config
else if(pitchMin && yawMin) { motorsIdentify(); } // Motor identification
if(pitchMin && rollMin && yawMin) {
#ifdef SERIAL_PORT
ss_write_str( "\r\nClearing settings" );
#endif
settingsClearAll();
} // Clear config
else if(pitchMin && yawMin) {
#ifdef SERIAL_PORT
ss_write_str( "\r\nMotor Identify" );
#endif
motorsIdentify();
} // Motor identification
// else if(pitchMin && rollMin) { } // Future use
// else if(rollMin && yawMin) { } // Future use
else if(pitchMin) { receiverStickCenter(); } // Stick Centering Test
else if(rollMin) { gyrosReverse(); } // Gyro direction reversing
else if(yawMin) { motorsThrottleCalibration(); } // ESC throttle calibration
else if(pitchMin) {
#ifdef SERIAL_PORT
ss_write_str( "\r\nStick Center" );
#endif
receiverStickCenter();
} // Stick Centering Test
else if(rollMin) {
#ifdef SERIAL_PORT
ss_write_str( "\r\nGyro reverse" );
#endif
gyrosReverse();
} // Gyro direction reversing
else if(yawMin) {
#ifdef SERIAL_PORT
ss_write_str( "\r\nThrottle Calibrate" );
#endif
motorsThrottleCalibration();
} // ESC throttle calibration
}

static inline void loop()
Expand Down Expand Up @@ -446,6 +509,7 @@ static inline void loop()
MotorOut6 = 500;
}
#elif defined(TRI_COPTER)

MotorOut1 = 0;
MotorOut2 = 0;
MotorOut3 = 0;
Expand All @@ -468,13 +532,22 @@ static inline void loop()

LED = 0;
output_motor_ppm();

}


int main()
{
setup();
#ifdef BT_SERIAL_PORT
setLinvorRate( 115200 );
#endif

while(1)
while(1) {
loop();
#ifdef SERIAL_PORT
menu();
#endif
}
return 1;
}
29 changes: 28 additions & 1 deletion motors.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ void motorsSetup()
M2_DIR = OUTPUT;
M3_DIR = OUTPUT;
M4_DIR = OUTPUT;
#ifdef SERIAL_PORT
SER_TX_DIR = OUTPUT;
SER_RX_DIR = INPUT;
#else
M5_DIR = OUTPUT;
M6_DIR = OUTPUT;

#endif

/*
* timer0 (8bit) - run at 8MHz, used to control ESC pulses
* We use 8Mhz instead of 1MHz (1 usec) to avoid alignment jitter.
Expand Down Expand Up @@ -75,6 +80,7 @@ void output_motor_ppm()
MotorOut4 = 0;
else if(MotorOut4 > t)
MotorOut4 = t;
#ifndef SERIAL_PORT
if(MotorOut5 < 0)
MotorOut5 = 0;
else if(MotorOut5 > t)
Expand All @@ -83,33 +89,40 @@ void output_motor_ppm()
MotorOut6 = 0;
else if(MotorOut6 > t)
MotorOut6 = t;
#endif /* SERIAL_PORT */

t = 1000;
MotorOut1+= t;
#ifndef SINGLE_COPTER
MotorOut2+= t;
MotorOut3+= t;
MotorOut4+= t;
#ifndef SERIAL_PORT
MotorOut5+= t;
MotorOut6+= t;
#endif /* SERIAL_PORT */
#endif

MotorOut1<<= 3;
MotorOut2<<= 3;
MotorOut3<<= 3;
MotorOut4<<= 3;
#ifndef SERIAL_PORT
MotorOut5<<= 3;
MotorOut6<<= 3;
#endif /* SERIAL_PORT */

/*
* Mirror M3, M4 to M5, M6, when possible, for hardware PPM
* support. The compiler will throw away the above operations on
* M5 and M6 when it sees these.
*/
#ifndef SERIAL_PORT
#if defined(DUAL_COPTER) || defined(TRI_COPTER) || defined(QUAD_COPTER) || defined(QUAD_X_COPTER) || defined(Y4_COPTER)
MotorOut5 = MotorOut3;
MotorOut6 = MotorOut4;
#endif
#endif /* SERIAL_PORT */

/*
* We can use timer compare output mode to provide jitter-free
Expand Down Expand Up @@ -165,14 +178,25 @@ void output_motor_ppm()
*
* We hope that TCNT0 and TCNT1 are always synchronized.
*/
#ifdef SERIAL_PORT
OCR0A = MotorStartTCNT1 + MotorOut3;
OCR0B = MotorStartTCNT1 + MotorOut4;
#else
OCR0A = MotorStartTCNT1 + MotorOut5;
OCR0B = MotorStartTCNT1 + MotorOut6;
#endif

do {
cli();
t = TCNT1;
sei();
t-= MotorStartTCNT1;
#ifdef SERIAL_PORT
if(t + 0xff >= MotorOut3)
TCCR0A&= ~_BV(COM0A0); /* Clear pin on match */
if(t + 0xff >= MotorOut4)
TCCR0A&= ~_BV(COM0B0); /* Clear pin on match */
#else
if(t >= MotorOut3)
M3 = 0;
if(t >= MotorOut4)
Expand All @@ -181,6 +205,7 @@ void output_motor_ppm()
TCCR0A&= ~_BV(COM0A0); /* Clear pin on match */
if(t + 0xff >= MotorOut6)
TCCR0A&= ~_BV(COM0B0); /* Clear pin on match */
#endif
t-= ((2000 + PWM_LOW_PULSE_US) << 3) - 0xff;
} while(t < 0);

Expand Down Expand Up @@ -259,6 +284,7 @@ void output_motor_ppm()
// TCCR0B = _BV(CS00) | _BV(FOC0A) | _BV(FOC0B);
#endif

#ifndef SERIAL_PORT
/*
* Wait for the on time so we can turn on the software pins.
*/
Expand Down Expand Up @@ -293,6 +319,7 @@ void output_motor_ppm()
#elif defined(QUAD_COPTER) || defined(QUAD_X_COPTER) || defined(Y4_COPTER) || defined(HEX_COPTER) || defined(Y6_COPTER)
M3 = 1;
M4 = 1;
#endif
#endif
/*
* We leave with the output pins ON.
Expand Down
Loading