Sample
Below is a sample of the serial monitor output window.
Cycle #89
Clear distance to front: 0-In 0-Cm
Temperature: 21-C 71-F
System Arming State: Disarmed
Transmission State: Neutral
Turn State: Going Straight
Spin State: Not Spinning
Throttle State: 0%
Right Stick Horizontal: Remote CH1 1496-126
Right Stick Verticle: Remote CH2 1512-130
Left Stick Horizontal: Remote CH4 1444-113
Left Stick Verticle: Remote CH3 1000-0
Arming switch signal: Remote CH5 1992-252
Mode Dial(Delay time): Remote CH6 1992-252
Gyro Motion-Acceleration: AX-4148 AY-448 AZ17456
Gyro Motion-Rotation: GX-1034 GY361 GZ-105
Gyro Acceleration: AX-4116 AY-568 AZ17408
Gyro Rotation: GX-1023 GY363 GZ-53
#include <EnableInterrupt.h>
#include <NewPing.h>
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
#define SERIAL_PORT_SPEED 57600
#define TRIGGER_PIN 3
#define ECHO_PIN 4
#define MAX_DISTANCE 500
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
#define OUTPUT_READABLE_ACCELGYRO
MPU6050 accelgyro;
#define heartbeat_pin 7
int heartbeatDelay=10;
int delayTime=0;
int count = 0;
int delayMultiplier=2;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t ax1, ay1, az1;
int16_t gx1, gy1, gz1;
int x, x1;
int y, y1;
int z, z1;
int temp=0;
int tempC=0;
int tempF=0;
#define RC_NUM_CHANNELS 6
#define RC_CH1 0
#define RC_CH2 1
#define RC_CH3 2
#define RC_CH4 3
#define RC_CH5 4
#define RC_CH6 5
#define RC_CH1_INPUT A0
#define RC_CH2_INPUT A1
#define RC_CH3_INPUT A2
#define RC_CH4_INPUT A3
#define RC_CH5_INPUT 8
#define RC_CH6_INPUT 9
uint16_t rc_values[RC_NUM_CHANNELS];
uint32_t rc_start[RC_NUM_CHANNELS];
volatile uint16_t rc_shared[RC_NUM_CHANNELS];
bool left_turn = false;
bool right_turn = false;
bool left_spin = false;
bool right_spin = false;
bool not_spinning=true;
bool straight = true;
bool reverse = false;
bool forward = false;
bool brakes = false;
bool neutral = true;
bool armed = false;
int rc_input_Deadzone = 20;
int rc_input_MIN = 1000;
int rc_input_MAX = 2000;
int pwm_MIN = 0;
int pwm_MAX = 255;
int pwm_ch1 = 0;
int pwm_ch2 = 0;
int pwm_ch3 = 0;
int pwm_ch4 = 0;
int pwm_ch5 = 0;
int pwm_ch6 = 0;
int arming_MIN = 20;
int arming_MAX = 230;
int forward_AT = 150;
int reverse_AT = 90;
int left_AT = 150;
int right_AT = 90;
int throttle=0;
int LF_motor_pin = 5;
int LR_motor_pin = 6;
int RF_motor_pin = 10;
int RR_motor_pin = 11;
int L_motor_speed = 0;
int R_motor_speed = 0;
int outMax = 255;
int outMin = 0;
float lastInput = 0;
double ITerm = 0;
double kp = 2; // Proportional value
double ki = 0; // Integral value
double kd = 0; // Derivative value
double Setpoint = 0; // Initial setpoint is 0
double Compute(double input) {
double error = Setpoint - input;
ITerm += (ki * error);
if (ITerm > outMax) ITerm = outMax;
else if (ITerm < outMin) ITerm = outMin;
double dInput = (input - lastInput);
// Compute PID Output
double output = kp * error + ITerm + kd * dInput;
if (output > outMax) output = outMax;
else if (output < outMin) output = outMin;
// Remember some variables for next time
lastInput = input;
return output;
}
void SetSetpoint(double d) {
Setpoint = d;
}
double GetSetPoint() {
return Setpoint;
}
void rc_read_values() {
noInterrupts();
memcpy(rc_values, (const void *)rc_shared, sizeof(rc_shared));
interrupts();
}
void calc_input(uint8_t channel, uint8_t input_pin) {
if (digitalRead(input_pin) == HIGH) {
rc_start[channel] = micros();
} else {
uint16_t rc_compare = (uint16_t)(micros() - rc_start[channel]);
rc_shared[channel] = rc_compare;
}
}
void calc_ch1() {
calc_input(RC_CH1, RC_CH1_INPUT);
if (rc_values[RC_CH1] < rc_input_MIN) {
rc_values[RC_CH1] = rc_input_MIN;
}
if (rc_values[RC_CH1] > rc_input_MAX) {
rc_values[RC_CH1] = rc_input_MAX;
}
}
void calc_ch2() {
calc_input(RC_CH2, RC_CH2_INPUT);
if (rc_values[RC_CH2] < rc_input_MIN) {
rc_values[RC_CH2] = rc_input_MIN;
}
if (rc_values[RC_CH2] > rc_input_MAX) {
rc_values[RC_CH2] = rc_input_MAX;
}
}
void calc_ch3() {
calc_input(RC_CH3, RC_CH3_INPUT);
...
Read more »