mitchellhansen 5 years ago
parent c1737fc0a8
commit 5ddea02652

@ -3,9 +3,11 @@ project(battletank)
cmake_minimum_required(VERSION 2.6) cmake_minimum_required(VERSION 2.6)
set(REMOTE_PORT "/dev/ttyUSB0")
set(DOZER_PORT "/dev/ttyUSB1")
set(MCU "atmega328p" ) set(MCU "atmega328p" )
set(CPU_SPEED "16000000" ) set(CPU_SPEED "16000000" )
set(PORT "/dev/ttyUSB0")
set(PORT_SPEED "57600") set(PORT_SPEED "57600")
set(PIN_VARIANT "standard") set(PIN_VARIANT "standard")
set(ARDUINO_PATH "arduino") set(ARDUINO_PATH "arduino")
@ -14,7 +16,8 @@ set(COMPILE_FLAGS "")
# Set own source files # Set own source files
# Simply list all your C / C++ source (not header!) files here # Simply list all your C / C++ source (not header!) files here
set(SRC_FILES main.cpp) set(REMOTE_SRC_FILES remote_main.cpp)
set(DOZER_SRC_FILES dozer_main.cpp)
# Include directories # Include directories
include_directories( include_directories(
@ -55,10 +58,17 @@ set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
set(CMAKE_EXE_LINKER_FLAGS "-Os -Wl,--gc-sections -mmcu=${MCU}") set(CMAKE_EXE_LINKER_FLAGS "-Os -Wl,--gc-sections -mmcu=${MCU}")
add_executable(${PROJECT_NAME} ${ARDUINO_CORE_SRC} ${SRC_FILES}) add_executable(${PROJECT_NAME}-dozer ${ARDUINO_CORE_SRC} ${DOZER_SRC_FILES})
add_executable(${PROJECT_NAME}-remote ${ARDUINO_CORE_SRC} ${REMOTE_SRC_FILES})
add_custom_target(flash-dozer
COMMAND ${CMAKE_OBJCOPY} -j .text -j .data -O ihex ${PROJECT_NAME}-dozer main.hex
COMMAND avrdude -F -p${MCU} -carduino -P${DOZER_PORT} -b${PORT_SPEED} -D -Uflash:w:main.hex:i
DEPENDS ${PROJECT_NAME}-dozer
)
add_custom_target(flash add_custom_target(flash-remote
COMMAND ${CMAKE_OBJCOPY} -j .text -j .data -O ihex ${PROJECT_NAME} main.hex COMMAND ${CMAKE_OBJCOPY} -j .text -j .data -O ihex ${PROJECT_NAME}-remote main.hex
COMMAND avrdude -F -p${MCU} -carduino -P${PORT} -b${PORT_SPEED} -D -Uflash:w:main.hex:i COMMAND avrdude -F -p${MCU} -carduino -P${REMOTE_PORT} -b${PORT_SPEED} -D -Uflash:w:main.hex:i
DEPENDS ${PROJECT_NAME} DEPENDS ${PROJECT_NAME}-remote
) )

@ -1,20 +0,0 @@
The MIT License (MIT)
Copyright (c) 2014 Brett Hagman
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ -1,137 +0,0 @@
# SoftPWM Library #
----
## What's New? ##
Version 1.0.1
* Changes from Paul Stoffregen
* Use IntervalTimer on Teensy 3.x
* Use LED_BUILTIN for WLED in example
Version 1.0.0
* Initial release
## Description ##
A Wiring Framework (and Arduino) Library to produce PWM signals on any arbitrary pin.
It was originally designed for use controlling the brightness of LEDs, but could be modified to control servos and other low frequency PWM controlled devices as well.
It uses a single hardware timer (Timer 2 on AVR, or IntervalTimer on Teensy 3.x) on the microcontroller to generate up to 20 PWM channels.
----
## Features ##
* Arbitrary output pins
* Up to 20 different channels can be created
* True zero level, i.e. off == off
* Separate fade rates for on and off
----
## Download and Installation ##
You can use the Arduino Library Manager (Sketch -> Include Library -> Manage Libraries...) to download the library.
Alternatively, you can download the library directly, and install it yourself.
* [SoftPWM Library - Latest Version](https://github.com/bhagman/SoftPWM/archive/master.zip)
Unzip the folder and rename it to `SoftPWM`, then move it to your `arduinosketchfolder/libraries/` folder.
----
## Usage Example ##
```
#include "SoftPWM.h"
void setup()
{
// Initialize
SoftPWMBegin();
// Create and set pin 13 to 0 (off)
SoftPWMSet(13, 0);
// Set fade time for pin 13 to 100 ms fade-up time, and 500 ms fade-down time
SoftPWMSetFadeTime(13, 100, 500);
}
void loop()
{
// Turn on - set to 100%
SoftPWMSetPercent(13, 100);
// Wait for LED to turn on - you could do other tasks here
delay(100);
// Turn off - set to 0%
SoftPWMSetPercent(13, 0);
// Wait for LED to turn off
delay(500);
}
```
----
## Function Descriptions ##
`SoftPWMBegin([defaultPolarity])`
* Initializes the library - sets up the timer and other tasks.
* optional `defaultPolarity` allows all newly defined pins to take on this polarity.
* Values: `SOFTPWM_NORMAL`, `SOFTPWM_INVERTED`
`SoftPWMSet(pin,value)`
* `pin` is the output pin.
* `value` is a value between 0 and 255 (inclusive).
`SoftPWMSetPercent(pin,percent)`
* `pin` is the output pin.
* `percent` is a value between 0 and 100 (inclusive).
`SoftPWMSetFadeTime(pin,fadeUpTime,fadeDownTime)`
* `pin` is the output pin.
* `fadeuptime` is the time in milliseconds that it will take the channel to fade from 0 to 255.
* Range: 0 to 4000
* `fadedowntime` is the time in milliseconds that it will take the channel to fade from 255 to 0.
* Range: 0 to 4000
`SoftPWMSetPolarity(pin,polarity)`
* `pin` is the output pin.
* `polarity` is the polarity for the given pin.
### Notes ###
* You can use `ALL` in place of the pin number to have the function act on all currently set channels.
* e.g. `SoftPWMSetFadeTime(ALL, 100, 400)` - this will set all created channels to have a fade-up time of 100 ms and a fade-down time of 400.
* The polarity setting of the pin is as follows:
* `SOFTPWM_NORMAL` means that the pin is LOW when the PWM value is 0, whereas `SOFTPWM_INVERTED` indicates the pin should be HIGH when the PWM value is 0.
----
## Demonstrations ##
Arduino Duemilanove LED Blink example - available as library example:
[![Arduino SoftPWM example](https://img.youtube.com/vi/9tTd7aLm9aQ/0.jpg)](https://www.youtube.com/watch?v=9tTd7aLm9aQ)
rDuino LEDHead Bounce example - available as library example:
[![rDuino LEDHead SoftPWM example](https://img.youtube.com/vi/jE7Zw1zNL6c/0.jpg)](https://www.youtube.com/watch?v=jE7Zw1zNL6c)
More demos:
https://www.youtube.com/view_play_list?p=33BB5D2E20609C52
----

@ -96,42 +96,24 @@ ISR(TIMER1_COMPA_vect)
// set all channels high - let's start again // set all channels high - let's start again
// and accept new checkvals // and accept new checkvals
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) { for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
if (_softpwm_channels[i].fadeuprate > 0 || _softpwm_channels[i].fadedownrate > 0) {
// we want to fade to the new value
direction = _softpwm_channels[i].pwmvalue - _softpwm_channels[i].checkval;
// we will default to jumping to the new value
newvalue = _softpwm_channels[i].pwmvalue;
if (direction > 0 && _softpwm_channels[i].fadeuprate > 0) {
newvalue = _softpwm_channels[i].checkval + _softpwm_channels[i].fadeuprate;
if (newvalue > _softpwm_channels[i].pwmvalue)
newvalue = _softpwm_channels[i].pwmvalue;
} else if (direction < 0 && _softpwm_channels[i].fadedownrate > 0) {
newvalue = _softpwm_channels[i].checkval - _softpwm_channels[i].fadedownrate;
if (newvalue < _softpwm_channels[i].pwmvalue)
newvalue = _softpwm_channels[i].pwmvalue;
}
_softpwm_channels[i].checkval = newvalue; // check on pwm, 0-255
} else // just set the channel to the new value
_softpwm_channels[i].checkval = _softpwm_channels[i].pwmvalue; _softpwm_channels[i].checkval = _softpwm_channels[i].pwmvalue;
// now set the pin high (if not 0) // now set the pin high (if not 0)
if (_softpwm_channels[i].checkval > 0) // don't set if checkval == 0 if (_softpwm_channels[i].checkval > 0)
{ {
if (_softpwm_channels[i].polarity == SOFTPWM_NORMAL) if (_softpwm_channels[i].polarity == SOFTPWM_NORMAL)
*_softpwm_channels[i].outport |= _softpwm_channels[i].pinmask; *_softpwm_channels[i].outport |= _softpwm_channels[i].pinmask;
else else
*_softpwm_channels[i].outport &= ~(_softpwm_channels[i].pinmask); *_softpwm_channels[i].outport &= ~(_softpwm_channels[i].pinmask);
} }
} }
} }
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) for (i = 0; i < SOFTPWM_MAXCHANNELS; i++)
{ {
if (_softpwm_channels[i].pin >= 0) // if it's a valid pin if (_softpwm_channels[i].pin >= 0) // Only set un-flagged pins
{ {
if (_softpwm_channels[i].checkval == _isr_softcount) // if we have hit the width if (_softpwm_channels[i].checkval == _isr_softcount) // if we have hit the width
{ {
@ -154,32 +136,17 @@ void SoftPWMBegin(uint8_t defaultPolarity) {
// At these settings on a 16 MHz part, we will get a PWM period of // At these settings on a 16 MHz part, we will get a PWM period of
// approximately 60Hz (~16ms). // approximately 60Hz (~16ms).
uint8_t i; // Hand init
cli(); // Disable interrupts while setting registers
//#ifdef WIRING TCCR1A = 0; // Reset the timer
// Timer2.setMode(0b010); // CTC TCCR1B = 0; // Reset the timer
// Timer2.setClockSource(CLOCK_PRESCALE_8);
// Timer2.setOCR(CHANNEL_A, SOFTPWM_OCR);
// Timer2.attachInterrupt(INTERRUPT_COMPARE_MATCH_A, SoftPWM_Timer_Interrupt);
//#else
// SOFTPWM_TIMER_INIT(SOFTPWM_OCR);
//#endif
cli(); //Disable interrupts while setting registers
TCCR1A = 0; // Make sure it is zero
TCCR1B = 0; // Make sure it is zero
//TCCR1B = (1 << WGM21); // Configure for CTC mode (Set it; don't OR stuff into it)
//TCCR1B |= (1 << CS21); // Prescaler @ 1024
TIMSK1 = (1 << OCIE1A); // Enable interrupt TIMSK1 = (1 << OCIE1A); // Enable interrupt
OCR1A = SOFTPWM_OCR; // compare value = 1 sec (16MHz AVR)
TCCR1B = (1 << CS21); /* start timer (ck/8 prescalar) */ \ TCCR1B = (1 << CS21); /* start timer (ck/8 prescalar) */ \
TCCR1A = (1 << WGM21); /* CTC mode */ \ TCCR1A = (1 << WGM21); /* CTC mode */ \
OCR1A = SOFTPWM_OCR; // compare value
sei(); sei();
for (uint8_t i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
_softpwm_channels[i].pin = -1; _softpwm_channels[i].pin = -1;
_softpwm_channels[i].polarity = SOFTPWM_NORMAL; _softpwm_channels[i].polarity = SOFTPWM_NORMAL;
_softpwm_channels[i].outport = 0; _softpwm_channels[i].outport = 0;
@ -207,24 +174,20 @@ void SoftPWMSetPolarity(int8_t pin, uint8_t polarity) {
} }
void SoftPWMSetPercent(int8_t pin, uint8_t percent, uint8_t hardset) {
SoftPWMSet(pin, ((uint16_t) percent * 255) / 100, hardset);
}
void SoftPWMSet(uint8_t pin, uint8_t value, uint8_t hardset) {
void SoftPWMSet(int8_t pin, uint8_t value, uint8_t hardset) {
int8_t firstfree = -1; // first free index
uint8_t i;
if (hardset) { if (hardset) {
SOFTPWM_TIMER_SET(0); SOFTPWM_TIMER_SET(0);
_isr_softcount = 0xff; _isr_softcount = 0xff;
} }
int8_t firstfree = -1; // first free index
// If the pin isn't already set, add it // If the pin isn't already set, add it
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) { for (uint8_t i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
if ((pin < 0 && _softpwm_channels[i].pin >= 0) || // ALL pins if (_softpwm_channels[i].pin == pin)
(pin >= 0 && _softpwm_channels[i].pin == pin)) // individual pin
{ {
// set the pin (and exit, if individual pin) // set the pin (and exit, if individual pin)
_softpwm_channels[i].pwmvalue = value; _softpwm_channels[i].pwmvalue = value;
@ -238,7 +201,7 @@ void SoftPWMSet(int8_t pin, uint8_t value, uint8_t hardset) {
firstfree = i; firstfree = i;
} }
if (pin >= 0 && firstfree >= 0) { if (firstfree >= 0) {
// we have a free pin we can use // we have a free pin we can use
_softpwm_channels[firstfree].pin = pin; _softpwm_channels[firstfree].pin = pin;
_softpwm_channels[firstfree].polarity = _softpwm_defaultPolarity; _softpwm_channels[firstfree].polarity = _softpwm_defaultPolarity;
@ -257,23 +220,34 @@ void SoftPWMSet(int8_t pin, uint8_t value, uint8_t hardset) {
} }
} }
void SoftPWMEnd(int8_t pin) { void SoftPWMEnd(uint8_t pin) {
uint8_t i;
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) { for (uint8_t i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
if ((pin < 0 && _softpwm_channels[i].pin >= 0) || // ALL pins if (_softpwm_channels[i].pin == pin) {
(pin >= 0 && _softpwm_channels[i].pin == pin)) // individual pin
{ // Disable pin output
// now disable the pin (put it into INPUT mode)
digitalWrite(_softpwm_channels[i].pin, 1); digitalWrite(_softpwm_channels[i].pin, 1);
pinMode(_softpwm_channels[i].pin, INPUT); pinMode(_softpwm_channels[i].pin, INPUT);
// remove the pin // Flag as removed
_softpwm_channels[i].pin = -1; _softpwm_channels[i].pin = -1;
} }
} }
} }
void SoftPWMEndAll() {
for (uint8_t i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
// Disable pin output
digitalWrite(_softpwm_channels[i].pin, 1);
pinMode(_softpwm_channels[i].pin, INPUT);
// Flag as removed
_softpwm_channels[i].pin = -1;
}
}
void SoftPWMSetFadeTime(int8_t pin, uint16_t fadeUpTime, uint16_t fadeDownTime) { void SoftPWMSetFadeTime(int8_t pin, uint16_t fadeUpTime, uint16_t fadeDownTime) {
int16_t fadeAmount; int16_t fadeAmount;

@ -51,8 +51,6 @@ void SoftPWMBegin(uint8_t defaultPolarity = SOFTPWM_NORMAL);
void SoftPWMSet(int8_t pin, uint8_t value, uint8_t hardset = 0); void SoftPWMSet(int8_t pin, uint8_t value, uint8_t hardset = 0);
void SoftPWMSetPercent(int8_t pin, uint8_t percent, uint8_t hardset = 0);
void SoftPWMEnd(int8_t pin); void SoftPWMEnd(int8_t pin);
void SoftPWMSetFadeTime(int8_t pin, uint16_t fadeUpTime, uint16_t fadeDownTime); void SoftPWMSetFadeTime(int8_t pin, uint16_t fadeUpTime, uint16_t fadeDownTime);

@ -1,13 +0,0 @@
SoftPWM Library
Version Modified By Date Comments
------- ------------ ---------- --------
0001 B Hagman 2010-03-14 Initial coding (Pi Day!)
0002 B Hagman 2010-03-21 License updates, minor fixes
0003 B Hagman 2010-04-21 Added hardset control for syncing to matrix scan lines
0004 B Hagman 2010-06-27 Fixed: pin 0 could not be used.
0005 B Hagman 2011-05-27 Added polarity, and full Wiring support.
B Hagman 2012-02-13 Fixed Arduino 1.0+ mess.
1.0.0 B Hagman 2017-01-13 Initial release conforming to library standard.
1.0.1 P Stoffregen 2017-08-30 Use IntervalTimer on Teensy 3.x and use LED_BUILTIN for WLED

@ -1,18 +0,0 @@
#include <SoftPWM.h>
void setup()
{
SoftPWMBegin();
SoftPWMSet(13, 0);
SoftPWMSetFadeTime(13, 1000, 1000);
}
void loop()
{
SoftPWMSet(13, 255);
delay(1000);
SoftPWMSet(13, 0);
delay(1000);
}

@ -1,38 +0,0 @@
#include <SoftPWM.h>
#define DELAY 100
uint8_t leds[8] = {22, 23, 26, 27, 28, 29, 30, 31};
void setup()
{
SoftPWMBegin();
for (int i = 0; i < 8; i++)
SoftPWMSet(leds[i], 0);
SoftPWMSetFadeTime(ALL, 50, 400);
}
void loop()
{
int i;
for (i = 0; i < 7; i++)
{
SoftPWMSet(leds[i+1], 255);
SoftPWMSet(leds[i], 0);
delay(DELAY);
}
delay(400);
for (i = 7; i > 0; i--)
{
SoftPWMSet(leds[i-1], 255);
SoftPWMSet(leds[i], 0);
delay(DELAY);
}
delay(400);
}

@ -1,43 +0,0 @@
#include <SoftPWM.h>
#define DELAY 40
uint8_t leds[8] = {22, 23, 26, 27, 28, 29, 30, 31};
void setup()
{
SoftPWMBegin();
for (int i = 0; i < 8; i++)
SoftPWMSet(leds[i], 0);
SoftPWMSetFadeTime(ALL, 30, 200);
}
void loop()
{
int i;
for (i = 0; i < 3; i++)
{
SoftPWMSet(leds[i+1], 255);
SoftPWMSet(leds[6-i], 255);
SoftPWMSet(leds[i], 0);
SoftPWMSet(leds[7-i], 0);
delay(DELAY);
}
delay(250);
for (i = 3; i > 0; i--)
{
SoftPWMSet(leds[i-1], 255);
SoftPWMSet(leds[8-i], 255);
SoftPWMSet(leds[i], 0);
SoftPWMSet(leds[7-i], 0);
delay(DELAY);
}
delay(250);
}

@ -1,24 +0,0 @@
#include <SoftPWM.h>
#define DELAY 100
uint8_t leds[8] = {22, 23, 26, 27, 28, 29, 30, 31};
void setup()
{
SoftPWMBegin();
for (int i = 0; i < 8; i++)
SoftPWMSet(leds[i], 0);
SoftPWMSetFadeTime(ALL, 50, 400);
}
void loop()
{
uint8_t pin = random(8);
SoftPWMSet(leds[pin], 255);
delay(50);
SoftPWMSet(leds[pin], 0);
delay(random(DELAY));
}

@ -1,30 +0,0 @@
#include <SoftPWM.h>
#ifndef WLED
#define WLED LED_BUILTIN
#endif
void setup()
{
SoftPWMBegin();
// Sets the PWM value to 0 for the built-in LED (WLED).
SoftPWMSet(WLED, 0);
// Sets the default fade time for WLED to
// 100 ms fade-up and 550 ms to fade-down.
SoftPWMSetFadeTime(WLED, 100, 550);
}
void loop()
{
// Turn on WLED
SoftPWMSet(WLED, 255);
// Wait for the fade-up and some extra delay.
delay(250);
// Turn off WLED
SoftPWMSet(WLED, 0);
// Wait for the fade-down, and some extra delay.
delay(650);
}

@ -1,36 +0,0 @@
########################################
# Syntax Coloring Map For SoftPWM
########################################
#
# Fields (Data Members) - KEYWORD2
# Methods (Member Functions)
# - FUNCTION2 (or KEYWORD2)
# Constants - LITERAL2
# Datatypes - KEYWORD5 (or KEYWORD1)
########################################
########################################
# Datatypes (KEYWORD5 or KEYWORD1)
########################################
SoftPWM KEYWORD1
########################################
# Methods and Functions
# (FUNCTION2 or KEYWORD2)
########################################
SoftPWMSet KEYWORD2
SoftPWMSetPercent KEYWORD2
SoftPWMSetFadeTime KEYWORD2
SoftPWMBegin KEYWORD2
SoftPWMEnd KEYWORD2
SoftPWMSetPolarity KEYWORD2
########################################
# Constants (LITERAL2)
########################################
ALL LITERAL2
SOFTPWM_NORMAL LITERAL2
SOFTPWM_INVERTED LITERAL2

@ -1,9 +0,0 @@
name=SoftPWM
version=1.0.1
author=Brett Hagman <bhagman@wiring.org.co>
maintainer=Brett Hagman <bhagman@wiring.org.co>
sentence=A software library to produce a 50 percent duty cycle PWM signal on arbitrary pins.<br />
paragraph=A Wiring Framework (and Arduino) Library, for Atmel AVR8 bit series microcontrollers and Teensy 3.x, to produce PWM signals on any arbitrary pin.<br />It was originally designed for controlling the brightness of LEDs, but could be adapted to control servos and other low frequency PWM controlled devices as well.<br />It uses a single hardware timer (Timer 2) on an Atmel AVR 8 bit microcontroller (or IntervalTimer on Teensy 3.x) to generate up to 20 PWM channels (your mileage may vary).<br /><br />Issues or questions: <a href="https://github.com/bhagman/SoftPWM/issues">https://github.com/bhagman/SoftPWM/issues</a><br />
category=Signal Input/Output
url=https://github.com/bhagman/SoftPWM
architectures=avr,arm

@ -0,0 +1,102 @@
#include <util/delay.h>
#include <avr/io.h>
#include <stdio.h>
#include <stdarg.h>
#include <util/setbaud.h>
#include <Arduino.h>
#include <lib.h>
#include "SoftPWM.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 3
#define CS_PIN 2
#define RED 4
#define GREEN 5
class Radio {
public:
RF24 instance;
};
class Motor {
public:
int power;
};
const uint64_t dozer_pipe = 0xF0F0F0F0D2LL;
const uint64_t remote_pipe = 0xF0F0F0F0E1LL;
struct StickValues {
uint32_t stick_1_a;
uint32_t stick_1_b;
uint32_t stick_2_a;
uint32_t stick_2_b;
};
int main() {
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
uart_init();
SoftPWMBegin();
RF24 radio(CE_PIN, CS_PIN);
radio.begin();
radio.openWritingPipe(dozer_pipe);
radio.openReadingPipe(1, remote_pipe);
radio.startListening();
//digitalWrite(18, HIGH);
// SoftPWMSet(18, 255, 1);
digitalWrite(GREEN, HIGH);
_delay_ms(2000);
digitalWrite(GREEN, LOW);
bool breaking = false;
int cycles = 0;
while (!breaking) {
//unsigned long start_wait = micros();
bool timeout = false;
while (!radio.available()) {
if (cycles++ > 10) {
digitalWrite(RED, HIGH);
_delay_ms(50);
digitalWrite(RED, LOW);
_delay_ms(50);
timeout = true;
cycles = 0;
break;
}
}
if (timeout) {
// SoftPWMSet(17, 0);
// SoftPWMSet(18, 0);
} else {
StickValues stick_values;
radio.read(&stick_values, sizeof(uint32_t)*4);
radio.flush_rx();
radio.flush_tx();
// SoftPWMSet(17, stick_values.stick_1_a);
// SoftPWMSet(18, stick_values.stick_1_b);
}
SoftPWMSet(17, 80);
SoftPWMSet(18, 120);
}
}

@ -1,169 +0,0 @@
#include <util/delay.h>
#include <avr/io.h>
#include <stdio.h>
#include <stdarg.h>
#include <util/setbaud.h>
#include <Arduino.h>
#include <lib.h>
#include "SoftPWM.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 3
#define CS_PIN 2
#define RED 4
#define GREEN 5
class Radio {
public:
RF24 instance;
};
class Motor {
public:
int power;
};
const uint64_t pipes[2] = {0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL};
int radio_number = ROLE;
int role = ROLE;
int main(void) {
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
uart_init();
SoftPWMBegin();
// SoftPWMSetFadeTime(4, 2000, 2000);
// pinMode(15, OUTPUT);
//
// pinMode(16, OUTPUT);
// pinMode(17, OUTPUT);
//
// pinMode(18, OUTPUT);
// pinMode(19, OUTPUT);
RF24 radio(CE_PIN, CS_PIN);
radio.begin();
if (radio_number) {
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1, pipes[0]);
} else {
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
}
radio.startListening();
SoftPWMSet(17, 255);
SoftPWMSet(18, 255);
//digitalWrite(18, HIGH);
// SoftPWMSet(18, 255, 1);
// digitalWrite(19, LOW);
// digitalWrite(14, LOW);
// digitalWrite(15, HIGH);
digitalWrite(GREEN, HIGH);
_delay_ms(200);
digitalWrite(GREEN, LOW);
int accum = 20;
int i = 5;
for (;;) {
unsigned long start_time = micros();
// ping
if (role == 1) {
if (accum > 230) {
i = -5; accum = 230;
} else if (accum < 100) {
i = 5; accum = 100;
}
accum += i;
// SoftPWMSet(14, accum);
// SoftPWMSet(19, accum);
_delay_ms(100);
//
//// digitalWrite(GREEN, HIGH);
//// _delay_ms(200);
//// digitalWrite(GREEN, LOW);
// radio.stopListening();
//
//
// if (!radio.write(&start_time, sizeof(unsigned long))) {
//// digitalWrite(RED, HIGH);
//// _delay_ms(1000);
//// digitalWrite(RED, LOW);
// radio.startListening();
// continue;
// }
//
// radio.startListening();
// unsigned long started_waiting_at = micros();
// boolean timeout = false;
//
// _delay_ms(390);
// if (!radio.available()) {
// timeout = true;
// }
//// while (!radio.available()) {
//// if (micros() - started_waiting_at > 20) {
//// timeout = true;
//// break;
//// }
//// }
// if (timeout) {
//
//// digitalWrite(RED, HIGH);
//// _delay_ms(200);
//// digitalWrite(RED, LOW);
//
// } else {
// unsigned long got_time;
// radio.read(&got_time, sizeof(unsigned long));
// unsigned long end_time = micros();
//
//// digitalWrite(GREEN, HIGH);
//// _delay_ms(200);
//// digitalWrite(GREEN, LOW);
// }
//_delay_ms(500);
}
// ping bag
if (role == 0) {
_delay_ms(100);
unsigned long got_time;
if (radio.available()) {
while (radio.available()) {
radio.read(&got_time, sizeof(unsigned long));
}
radio.stopListening();
radio.write(&got_time, sizeof(unsigned long));
radio.startListening();
}
}
}
}

@ -0,0 +1,81 @@
#include <util/delay.h>
#include <avr/io.h>
#include <stdio.h>
#include <stdarg.h>
#include <util/setbaud.h>
#include <Arduino.h>
#include <lib.h>
#include "SoftPWM.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 3
#define CS_PIN 2
#define RED 4
#define GREEN 5
class Radio {
public:
RF24 instance;
};
class Motor {
public:
int power;
};
const uint64_t dozer_pipe = 0xF0F0F0F0D2LL;
const uint64_t remote_pipe = 0xF0F0F0F0E1LL;
struct StickValues {
uint32_t stick_1_a;
uint32_t stick_1_b;
uint32_t stick_2_a;
uint32_t stick_2_b;
};
int main() {
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
uart_init();
RF24 radio(CE_PIN, CS_PIN);
radio.begin();
radio.openWritingPipe(remote_pipe);
radio.openReadingPipe(1, dozer_pipe);
radio.startListening();
bool breaking = false;
while (!breaking) {
_delay_ms(100);
// Get input
// Parse into a struct
StickValues stick_values;
stick_values.stick_1_a = 100;
stick_values.stick_1_b = 50;
stick_values.stick_2_a = 0;
stick_values.stick_2_b = 0;
// Send to dozer
radio.stopListening();
radio.write(&stick_values, sizeof(uint32_t)*4);
radio.startListening();
}
}
// Dot really ever have to read
//if (radio.available()) {
// while (radio.available()) {
// radio.read(&got_time, sizeof(unsigned long));
// }
//}
Loading…
Cancel
Save