Commit 1dfd4bbe authored by Ondřej Kučera's avatar Ondřej Kučera

add btn on remote; add Stalin mounts

parent 8af4143e
This diff is collapsed.
No preview for this file type
#include <RFM69.h>
#include <SPI.h>
//radio
#define NETWORKID 5 // Must be the same for all nodes (0 to 255)
#define MYNODEID 1 // My node ID (0 to 255)
#define TONODEID 2 // Destination node ID (0 to 254, 255 = broadcast)
#define FREQUENCY RF69_433MHZ // Frequency set up
#define FREQUENCYSPECIFIC 433000000 // Should be value in Hz
#define CHIP_SELECT_PIN 10 //radio chip select
#define INTERUP_PIN 2
#define IS_RFM69HW_HCW
//pins
#define GO A1
#define TURN A2
RFM69 radio(CHIP_SELECT_PIN, INTERUP_PIN, true); //radio
// main vars
int SPEED = 0;
int STEER = 0;
// vars for monitoring change
int _SPEED = 1;
int _STEER = 1;
// steering bounds
int STRAIGHT = 511;
int LEFT = 0;
int RIGHT = 1023;
/* WIP
bool bmpOk;
bool tempOk;
int temp;
*/
long unsigned int last_sent;
// OUT struct
typedef struct {
int speed;
int steer;
} messageOut;
/*
// IN struct
typedef struct {
bool sensOk;
int temp;
bool tempOk;
} messageIn;
*/
// -------------------------------------------------------------------------------------------
void setup() {
//pins
pinMode(GO, INPUT);
pinMode(STEER, INPUT);
STRAIGHT = analogRead(STEER); //auto calibration
//serial
Serial.begin(9600);
//radio
if (!radio.initialize(FREQUENCY, MYNODEID, NETWORKID)) {
Serial.println("RFM69HW initialization failed!");
} else {
radio.setFrequency(FREQUENCYSPECIFIC);
radio.setHighPower(true); // RFM69HW
radio.setPowerLevel(31); // max power
}
Serial.println("SETUP finished");
}
// -------------------------------------------------------------------------------------------
void loop() {
// read the joystick
readController();
// send data in case it changed, or every second
if (hasDataChanged() | (millis() - last_sent) > 500) {
sendData();
last_sent = millis();
}
// process received data, reply an ACK if required
if (radio.receiveDone()) {
processData();
if (radio.ACKRequested()) { //send ACK
radio.sendACK();
}
}
}
// -------------------------------------------------------------------------------------------
void readController() {
// read ADC (0 - 1023)
int _speed = analogRead(GO);
int _steer = analogRead(TURN);
if ((_speed <= 520) & (_speed >= 495)) { // offset in the middle
SPEED = 0;
} else {
SPEED = map(_speed, 0, 1021, -5, 5); // convert to speed levels
}
if ((_steer <= STRAIGHT + 10) & (_steer >= STRAIGHT - 10)) { // offset in the middle
STEER = 0;
} else if (_steer >= 1000) {
STEER = 5; // fix for mismapping between 4 and 5
}
else {
STEER = map(_steer, 0, 1023, -5, 5); // convert to steering levels
}
}
void sendData() {
// create a struct & fill it w/ data
messageOut data;
data.speed = SPEED;
data.steer = STEER;
//send the struct
radio.send(TONODEID, (const void*)&data, sizeof(data));
}
// true = joystick has been moved
// false = joystick has NOT been moved
bool hasDataChanged() {
if ((SPEED != _SPEED) | (STEER != _STEER)) {
_SPEED = SPEED;
_STEER = STEER;
return true;
} else {
return false;
}
}
/* WIP
void processData() {
messageIn incoming = *(messageIn*)radio.DATA;
if (incoming.sensOk) {
temp = incoming.temp;
} else {}
if (!incoming.tempOk) {
}
}
#include <RFM69.h>
#include <SPI.h>
//radio
#define NETWORKID 5 // Must be the same for all nodes (0 to 255)
#define MYNODEID 1 // My node ID (0 to 255)
#define TONODEID 2 // Destination node ID (0 to 254, 255 = broadcast)
#define FREQUENCY RF69_433MHZ // Frequency set up
#define FREQUENCYSPECIFIC 433000000 // Should be value in Hz
#define CHIP_SELECT_PIN 10 //radio chip select
#define INTERUP_PIN 2
#define IS_RFM69HW_HCW
//pins
#define GO A1
#define TURN A2
#define BTN 9 //btn
RFM69 radio(CHIP_SELECT_PIN, INTERUP_PIN, true); //radio
// main vars
int SPEED = 0;
int STEER = 0;
bool BUTTON = false;
// vars for monitoring change
int _SPEED = 1;
int _STEER = 1;
bool _BUTTON = false;
// steering bounds
int STRAIGHT = 511;
int LEFT = 0;
int RIGHT = 1023;
/* WIP
bool bmpOk;
bool tempOk;
int temp;
*/
long unsigned int last_sent;
// OUT struct
typedef struct {
int speed;
int steer;
bool button;
} messageOut;
/*
// IN struct
typedef struct {
bool sensOk;
int temp;
bool tempOk;
} messageIn;
*/
// -------------------------------------------------------------------------------------------
void setup() {
//pins
pinMode(GO, INPUT);
pinMode(STEER, INPUT);
pinMode(BTN, INPUT);
STRAIGHT = analogRead(STEER); //auto calibration
//serial
Serial.begin(9600);
//radio
if (!radio.initialize(FREQUENCY, MYNODEID, NETWORKID)) {
Serial.println("RFM69HW initialization failed!");
} else {
radio.setFrequency(FREQUENCYSPECIFIC);
radio.setHighPower(true); // RFM69HW
radio.setPowerLevel(31); // max power
}
Serial.println("SETUP finished");
}
// -------------------------------------------------------------------------------------------
void loop() {
// read the joystick
readController();
readBtn();
// send data in case it changed, or every second
if (hasDataChanged() | (millis() - last_sent) > 500) {
sendData();
last_sent = millis();
}
/* WIP
// process received data, reply an ACK if required
if (radio.receiveDone()) {
processData();
if (radio.ACKRequested()) { //send ACK
radio.sendACK();
}
}
*/
}
// -------------------------------------------------------------------------------------------
void readBtn() {
BUTTON = digitalRead(BTN);
}
void readController() {
// read ADC (0 - 1023)
int _speed = analogRead(GO);
int _steer = analogRead(TURN);
if ((_speed <= 520) & (_speed >= 495)) { // offset in the middle
SPEED = 0;
} else {
SPEED = map(_speed, 0, 1021, -5, 5); // convert to speed levels
}
if ((_steer <= STRAIGHT + 10) & (_steer >= STRAIGHT - 10)) { // offset in the middle
STEER = 0;
} else if (_steer >= 1000) {
STEER = 5; // fix for mismapping between 4 and 5
}
else {
STEER = map(_steer, 0, 1023, -5, 5); // convert to steering levels
}
}
void sendData() {
// create a struct & fill it w/ data
messageOut data;
data.speed = SPEED;
data.steer = STEER;
data.button = BUTTON;
//send the struct
radio.send(TONODEID, (const void*)&data, sizeof(data));
}
// true = joystick has been moved
// false = joystick has NOT been moved
bool hasDataChanged() {
if ((SPEED != _SPEED) | (STEER != _STEER) | (BUTTON != BUTTON)) {
_SPEED = SPEED;
_STEER = STEER;
_BUTTON = BUTTON;
return true;
} else {
return false;
}
}
/* WIP
void processData() {
messageIn incoming = *(messageIn*)radio.DATA;
if (incoming.sensOk) {
temp = incoming.temp;
} else {}
if (!incoming.tempOk) {
}
}
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment