parent
b6c71580c5
commit
f4f6e08130
7 changed files with 1878 additions and 5 deletions
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,179 @@ |
||||
/*--------------------------------------------------------------------
|
||||
This file is part of the Adafruit NeoPixel library. |
||||
|
||||
NeoPixel is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU Lesser General Public License as |
||||
published by the Free Software Foundation, either version 3 of |
||||
the License, or (at your option) any later version. |
||||
|
||||
NeoPixel is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU Lesser General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU Lesser General Public |
||||
License along with NeoPixel. If not, see |
||||
<http://www.gnu.org/licenses/>.
|
||||
--------------------------------------------------------------------*/ |
||||
|
||||
#ifndef ADAFRUIT_NEOPIXEL_H |
||||
#define ADAFRUIT_NEOPIXEL_H |
||||
|
||||
#if (ARDUINO >= 100) |
||||
#include <Arduino.h> |
||||
#else |
||||
#include <WProgram.h> |
||||
#include <pins_arduino.h> |
||||
#endif |
||||
|
||||
// The order of primary colors in the NeoPixel data stream can vary
|
||||
// among device types, manufacturers and even different revisions of
|
||||
// the same item. The third parameter to the Adafruit_NeoPixel
|
||||
// constructor encodes the per-pixel byte offsets of the red, green
|
||||
// and blue primaries (plus white, if present) in the data stream --
|
||||
// the following #defines provide an easier-to-use named version for
|
||||
// each permutation. e.g. NEO_GRB indicates a NeoPixel-compatible
|
||||
// device expecting three bytes per pixel, with the first byte
|
||||
// containing the green value, second containing red and third
|
||||
// containing blue. The in-memory representation of a chain of
|
||||
// NeoPixels is the same as the data-stream order; no re-ordering of
|
||||
// bytes is required when issuing data to the chain.
|
||||
|
||||
// Bits 5,4 of this value are the offset (0-3) from the first byte of
|
||||
// a pixel to the location of the red color byte. Bits 3,2 are the
|
||||
// green offset and 1,0 are the blue offset. If it is an RGBW-type
|
||||
// device (supporting a white primary in addition to R,G,B), bits 7,6
|
||||
// are the offset to the white byte...otherwise, bits 7,6 are set to
|
||||
// the same value as 5,4 (red) to indicate an RGB (not RGBW) device.
|
||||
// i.e. binary representation:
|
||||
// 0bWWRRGGBB for RGBW devices
|
||||
// 0bRRRRGGBB for RGB
|
||||
|
||||
// RGB NeoPixel permutations; white and red offsets are always same
|
||||
// Offset: W R G B
|
||||
#define NEO_RGB ((0 << 6) | (0 << 4) | (1 << 2) | (2)) |
||||
#define NEO_RBG ((0 << 6) | (0 << 4) | (2 << 2) | (1)) |
||||
#define NEO_GRB ((1 << 6) | (1 << 4) | (0 << 2) | (2)) |
||||
#define NEO_GBR ((2 << 6) | (2 << 4) | (0 << 2) | (1)) |
||||
#define NEO_BRG ((1 << 6) | (1 << 4) | (2 << 2) | (0)) |
||||
#define NEO_BGR ((2 << 6) | (2 << 4) | (1 << 2) | (0)) |
||||
|
||||
// RGBW NeoPixel permutations; all 4 offsets are distinct
|
||||
// Offset: W R G B
|
||||
#define NEO_WRGB ((0 << 6) | (1 << 4) | (2 << 2) | (3)) |
||||
#define NEO_WRBG ((0 << 6) | (1 << 4) | (3 << 2) | (2)) |
||||
#define NEO_WGRB ((0 << 6) | (2 << 4) | (1 << 2) | (3)) |
||||
#define NEO_WGBR ((0 << 6) | (3 << 4) | (1 << 2) | (2)) |
||||
#define NEO_WBRG ((0 << 6) | (2 << 4) | (3 << 2) | (1)) |
||||
#define NEO_WBGR ((0 << 6) | (3 << 4) | (2 << 2) | (1)) |
||||
|
||||
#define NEO_RWGB ((1 << 6) | (0 << 4) | (2 << 2) | (3)) |
||||
#define NEO_RWBG ((1 << 6) | (0 << 4) | (3 << 2) | (2)) |
||||
#define NEO_RGWB ((2 << 6) | (0 << 4) | (1 << 2) | (3)) |
||||
#define NEO_RGBW ((3 << 6) | (0 << 4) | (1 << 2) | (2)) |
||||
#define NEO_RBWG ((2 << 6) | (0 << 4) | (3 << 2) | (1)) |
||||
#define NEO_RBGW ((3 << 6) | (0 << 4) | (2 << 2) | (1)) |
||||
|
||||
#define NEO_GWRB ((1 << 6) | (2 << 4) | (0 << 2) | (3)) |
||||
#define NEO_GWBR ((1 << 6) | (3 << 4) | (0 << 2) | (2)) |
||||
#define NEO_GRWB ((2 << 6) | (1 << 4) | (0 << 2) | (3)) |
||||
#define NEO_GRBW ((3 << 6) | (1 << 4) | (0 << 2) | (2)) |
||||
#define NEO_GBWR ((2 << 6) | (3 << 4) | (0 << 2) | (1)) |
||||
#define NEO_GBRW ((3 << 6) | (2 << 4) | (0 << 2) | (1)) |
||||
|
||||
#define NEO_BWRG ((1 << 6) | (2 << 4) | (3 << 2) | (0)) |
||||
#define NEO_BWGR ((1 << 6) | (3 << 4) | (2 << 2) | (0)) |
||||
#define NEO_BRWG ((2 << 6) | (1 << 4) | (3 << 2) | (0)) |
||||
#define NEO_BRGW ((3 << 6) | (1 << 4) | (2 << 2) | (0)) |
||||
#define NEO_BGWR ((2 << 6) | (3 << 4) | (1 << 2) | (0)) |
||||
#define NEO_BGRW ((3 << 6) | (2 << 4) | (1 << 2) | (0)) |
||||
|
||||
// Add NEO_KHZ400 to the color order value to indicate a 400 KHz
|
||||
// device. All but the earliest v1 NeoPixels expect an 800 KHz data
|
||||
// stream, this is the default if unspecified. Because flash space
|
||||
// is very limited on ATtiny devices (e.g. Trinket, Gemma), v1
|
||||
// NeoPixels aren't handled by default on those chips, though it can
|
||||
// be enabled by removing the ifndef/endif below -- but code will be
|
||||
// bigger. Conversely, can disable the NEO_KHZ400 line on other MCUs
|
||||
// to remove v1 support and save a little space.
|
||||
|
||||
#define NEO_KHZ800 0x0000 // 800 KHz datastream
|
||||
#ifndef __AVR_ATtiny85__ |
||||
#define NEO_KHZ400 0x0100 // 400 KHz datastream
|
||||
#endif |
||||
|
||||
// If 400 KHz support is enabled, the third parameter to the constructor
|
||||
// requires a 16-bit value (in order to select 400 vs 800 KHz speed).
|
||||
// If only 800 KHz is enabled (as is default on ATtiny), an 8-bit value
|
||||
// is sufficient to encode pixel color order, saving some space.
|
||||
|
||||
#ifdef NEO_KHZ400 |
||||
typedef uint16_t neoPixelType; |
||||
#else |
||||
typedef uint8_t neoPixelType; |
||||
#endif |
||||
|
||||
class Adafruit_NeoPixel { |
||||
|
||||
public: |
||||
|
||||
// Constructor: number of LEDs, pin number, LED type
|
||||
Adafruit_NeoPixel(uint16_t n, uint8_t p=6, neoPixelType t=NEO_GRB + NEO_KHZ800); |
||||
Adafruit_NeoPixel(void); |
||||
~Adafruit_NeoPixel(); |
||||
|
||||
void |
||||
begin(void), |
||||
show(void), |
||||
setPin(uint8_t p), |
||||
setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b), |
||||
setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w), |
||||
setPixelColor(uint16_t n, uint32_t c), |
||||
setBrightness(uint8_t), |
||||
clear(), |
||||
updateLength(uint16_t n), |
||||
updateType(neoPixelType t); |
||||
uint8_t |
||||
*getPixels(void) const, |
||||
getBrightness(void) const; |
||||
uint16_t |
||||
numPixels(void) const; |
||||
static uint32_t |
||||
Color(uint8_t r, uint8_t g, uint8_t b), |
||||
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w); |
||||
uint32_t |
||||
getPixelColor(uint16_t n) const; |
||||
inline bool |
||||
canShow(void) { return (micros() - endTime) >= 50L; } |
||||
|
||||
private: |
||||
|
||||
boolean |
||||
#ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled...
|
||||
is800KHz, // ...true if 800 KHz pixels
|
||||
#endif |
||||
begun; // true if begin() previously called
|
||||
uint16_t |
||||
numLEDs, // Number of RGB LEDs in strip
|
||||
numBytes; // Size of 'pixels' buffer below (3 or 4 bytes/pixel)
|
||||
int8_t |
||||
pin; // Output pin number (-1 if not yet set)
|
||||
uint8_t |
||||
brightness, |
||||
*pixels, // Holds LED color values (3 or 4 bytes each)
|
||||
rOffset, // Index of red byte within each 3- or 4-byte pixel
|
||||
gOffset, // Index of green byte
|
||||
bOffset, // Index of blue byte
|
||||
wOffset; // Index of white byte (same as rOffset if no white)
|
||||
uint32_t |
||||
endTime; // Latch timing reference
|
||||
#ifdef __AVR__ |
||||
volatile uint8_t |
||||
*port; // Output PORT register
|
||||
uint8_t |
||||
pinMask; // Output PORT bitmask
|
||||
#endif |
||||
|
||||
}; |
||||
|
||||
#endif // ADAFRUIT_NEOPIXEL_H
|
@ -0,0 +1,57 @@ |
||||
#include <arduino.h> |
||||
#include "comm.h" |
||||
|
||||
struct SENSORS sensor = {0}; |
||||
|
||||
void shift_reg_init(void) |
||||
{ |
||||
pinMode(8, OUTPUT); //H165 Lock
|
||||
digitalWrite(8, 1); |
||||
pinMode(12, INPUT); //H165 MISO
|
||||
pinMode(13, OUTPUT); //H165/595 CLK
|
||||
digitalWrite(13, 0); |
||||
} |
||||
|
||||
void reload_shift_reg(void) |
||||
{ |
||||
char i = 0; |
||||
CLK_0; |
||||
LOAD; |
||||
sensor.reg0 = 0; |
||||
sensor.reg1 = 0; |
||||
|
||||
for (i = 0; i < 8; i++) |
||||
{ |
||||
sensor.reg0 <<= 1; |
||||
if (MISO) |
||||
sensor.reg0 |= 0x01; |
||||
CLK_1; |
||||
CLK_0; |
||||
} |
||||
for (i = 0; i < 8; i++) |
||||
{ |
||||
sensor.reg1 <<= 1; |
||||
if (MISO) |
||||
sensor.reg1 |= 0x01; |
||||
CLK_1; |
||||
CLK_0; |
||||
} |
||||
|
||||
sensor.ir_left_3 = !!(sensor.reg0&(1<<0)); |
||||
sensor.ir_left_2 = !!(sensor.reg0&(1<<1)); |
||||
sensor.ir_left_1 = !!(sensor.reg0&(1<<2)); |
||||
sensor.ir_mid = !!(sensor.reg0&(1<<3)); |
||||
sensor.ir_right_1 = !!(sensor.reg0&(1<<4)); |
||||
sensor.ir_right_2 = !!(sensor.reg0&(1<<5)); |
||||
sensor.ir_right_3 = !!(sensor.reg0&(1<<6)); |
||||
|
||||
sensor.switcher_front_left_2 = (!(sensor.reg1&(1<<0))); |
||||
sensor.switcher_front_left_1 = (!(sensor.reg1&(1<<1))); |
||||
sensor.switcher_front_right_1 = (!(sensor.reg1&(1<<2))); |
||||
sensor.switcher_front_right_2 = (!(sensor.reg1&(1<<3))); |
||||
sensor.switcher_back_left = (!(sensor.reg1&(1<<4))); |
||||
sensor.switcher_back_right = (!(sensor.reg1&(1<<5))); |
||||
|
||||
sensor.key_1 = (!(sensor.reg1&(1<<6))); |
||||
sensor.key_2 = (!(sensor.reg1&(1<<7))); |
||||
} |
@ -0,0 +1,49 @@ |
||||
#ifndef __COMM_H___ |
||||
#define __COMM_H___ |
||||
|
||||
|
||||
#define LOAD digitalWrite(8, 0); delay(1);digitalWrite(8, 1) |
||||
#define CLK_0 digitalWrite(13, 0) |
||||
#define CLK_1 digitalWrite(13, 1) |
||||
#define MISO digitalRead(12) |
||||
|
||||
struct SENSORS |
||||
{ |
||||
//移位寄存器读取值
|
||||
unsigned char reg0; |
||||
unsigned char reg1; |
||||
|
||||
//下方红外传感器
|
||||
// ir_mid
|
||||
// ir_left_1 ir_right_1
|
||||
// ir_left_2 ir_right_2
|
||||
//ir_left_3 ir_right_3
|
||||
char ir_left_1; |
||||
char ir_left_2; |
||||
char ir_left_3; |
||||
char ir_mid; |
||||
char ir_right_1; |
||||
char ir_right_2; |
||||
char ir_right_3; |
||||
//周围碰撞开关
|
||||
// switcher_front_left1 switcher_front_right1
|
||||
//switcher_front_left2 switcher_front_right2
|
||||
//
|
||||
//
|
||||
// switcher_back_left switcher_back_right
|
||||
char switcher_front_left_1; |
||||
char switcher_front_left_2; |
||||
char switcher_front_right_1; |
||||
char switcher_front_right_2; |
||||
char switcher_back_left; |
||||
char switcher_back_right; |
||||
//板载按键
|
||||
char key_1; |
||||
char key_2; |
||||
}; |
||||
extern struct SENSORS sensor; |
||||
|
||||
void shift_reg_init(void); //初始化
|
||||
void reload_shift_reg(void); //刷新移位寄存器值
|
||||
#endif |
||||
|
@ -0,0 +1,145 @@ |
||||
#include <arduino.h> |
||||
#include "motor.h" |
||||
|
||||
extern unsigned int left_pulse = 0; |
||||
extern unsigned int right_pulse = 0; |
||||
int left_velocity = 0; |
||||
int right_velocity = 0; |
||||
int left_update = 0; |
||||
int right_update = 0; |
||||
|
||||
void left_tri()//Left velocity sensor
|
||||
{ |
||||
static unsigned long t = 0, left_interval = 0; |
||||
left_pulse++; |
||||
left_interval = millis() - t; |
||||
left_velocity = 5 * 1000 / float(left_interval); |
||||
t = millis(); |
||||
left_update = 0; |
||||
} |
||||
|
||||
void right_tri()//right velocity sensor
|
||||
{ |
||||
static unsigned long t = 0, right_interval = 0; |
||||
right_pulse++; |
||||
right_interval = millis() - t; |
||||
right_velocity = 5 * 1000 / float(right_interval); |
||||
t = millis(); |
||||
right_update = 0; |
||||
} |
||||
|
||||
void motor_init(void) |
||||
{ |
||||
pinMode(6, OUTPUT); //Motor Left Backward
|
||||
pinMode(7, OUTPUT); //Motor Left Forward
|
||||
|
||||
pinMode(9, OUTPUT); //Motor Right Backward
|
||||
pinMode(10, OUTPUT); //Motor Right Forward
|
||||
|
||||
attachInterrupt(0, left_tri, RISING); |
||||
attachInterrupt(1, right_tri, RISING); |
||||
} |
||||
|
||||
void motor_set_PWM(int left, int right) |
||||
{ |
||||
if (left >= 0) |
||||
{ |
||||
analogWrite(6, left); |
||||
digitalWrite(7, 0); |
||||
} |
||||
else |
||||
{ |
||||
analogWrite(6, 255 + left); |
||||
digitalWrite(7, 1); |
||||
} |
||||
|
||||
if (right >= 0) |
||||
{ |
||||
analogWrite(9, 255 - right); |
||||
digitalWrite(10, 1); |
||||
} |
||||
else |
||||
{ |
||||
analogWrite(9, - right); |
||||
digitalWrite(10, 0); |
||||
} |
||||
} |
||||
|
||||
void motor_step(int left, int right, int step_left, int step_right) |
||||
{ |
||||
static int left_tar = 0, right_tar = 0; |
||||
static int pwm_l = 0; |
||||
static int pwm_r = 0; |
||||
static float balance_l = 1; |
||||
static float balance_r = 1; |
||||
|
||||
if (left_tar != left) //设置初始pwm
|
||||
{ |
||||
pwm_l = left; |
||||
left_tar = left; |
||||
} |
||||
if (right_tar != right) |
||||
{ |
||||
pwm_r = right; |
||||
right_tar = right; |
||||
} |
||||
|
||||
if((left_tar || right_tar) && step_left == 0 && step_right == 0) //设定速度但不指定距离
|
||||
{ |
||||
if (left_velocity > abs(left_tar)) pwm_l>0 ? pwm_l-- : pwm_l++; else left_tar>0 ? pwm_l++ : pwm_l--; |
||||
if (right_velocity > abs(right_tar)) pwm_r>0 ? pwm_r-- : pwm_r++; else right_tar>0 ? pwm_r++ : pwm_r--; |
||||
|
||||
if (pwm_l > 255) pwm_l = 255; |
||||
if (pwm_r > 255) pwm_r = 255; |
||||
if (pwm_l < -255) pwm_l = -255; |
||||
if (pwm_r < -255) pwm_r = -255; |
||||
|
||||
if (left_tar != 0) |
||||
{ |
||||
balance_l = abs(float(pwm_l) / float(left_tar)); |
||||
if (balance_l > 1.5) balance_l = 1.5; |
||||
if (balance_l < 0.66) balance_l = 0.66; |
||||
} |
||||
if (right_tar != 0) |
||||
{ |
||||
balance_r = abs(float(pwm_r) / float(right_tar)); |
||||
if (balance_r > 1.5) balance_r = 1.5; |
||||
if (balance_r < 0.66) balance_r = 0.66; |
||||
} |
||||
|
||||
motor_set_PWM(pwm_l, pwm_r); |
||||
|
||||
left_update++; |
||||
right_update++; |
||||
if (left_update >= 10) |
||||
{ |
||||
left_velocity = 0; |
||||
left_update = 1; |
||||
} |
||||
if (right_update >= 10) |
||||
{ |
||||
right_velocity = 0; |
||||
right_update = 1; |
||||
}
|
||||
delay(10); |
||||
} |
||||
else if((left_tar || right_tar) && (step_left || step_right))//设定速度且指定距离
|
||||
{ |
||||
unsigned int t_pulse_r = right_pulse; |
||||
unsigned int t_pulse_l = left_pulse; |
||||
|
||||
motor_set_PWM(left_tar * balance_l, right_tar * balance_r); |
||||
|
||||
if(!left_tar) //左侧不转
|
||||
while(right_pulse - t_pulse_r <= step_right) |
||||
delay(5); |
||||
else if(!right_tar) //右侧不转
|
||||
while(left_pulse - t_pulse_l <= step_left) |
||||
delay(5); |
||||
else //都转
|
||||
while(right_pulse - t_pulse_r <= step_right || left_pulse - t_pulse_l <= step_left) |
||||
delay(5); |
||||
} |
||||
else//停止
|
||||
motor_set_PWM(0, 0); |
||||
} |
@ -0,0 +1,11 @@ |
||||
#ifndef __MOTOR_H__ |
||||
#define __MOTOR_H__ |
||||
|
||||
extern unsigned int left_pulse; |
||||
extern unsigned int right_pulse; |
||||
|
||||
void motor_init(void); //初始化
|
||||
void motor_set_PWM(int left, int right); //设定电机PWM
|
||||
void motor_step(int left, int right, int step_left = 0, int step_right = 0); //设定小车运行状态
|
||||
|
||||
#endif |
Loading…
Reference in new issue