Compare commits
2 Commits
b957a0701b
...
899212d7b9
| Author | SHA1 | Date | |
|---|---|---|---|
| 899212d7b9 | |||
| febf1215db |
@@ -4,13 +4,16 @@
|
|||||||
const int SS_PIN = 10;
|
const int SS_PIN = 10;
|
||||||
const int RST_PIN = 7;
|
const int RST_PIN = 7;
|
||||||
|
|
||||||
// Kierunkowskazy
|
const byte AUTH_UID[4] = {145, 136, 97, 102};
|
||||||
const int TS_RIGHT = 9;
|
const byte AUTH_UID_SIZE = sizeof(AUTH_UID);
|
||||||
const int TS_LEFT = 8;
|
|
||||||
|
|
||||||
// Odległościomierz
|
// // Kierunkowskazy
|
||||||
const int TRIG_PIN = 6;
|
// const int TS_RIGHT = 9;
|
||||||
const int ECHO_PIN = 5;
|
// const int TS_LEFT = 8;
|
||||||
|
|
||||||
|
// // Odległościomierz
|
||||||
|
// const int TRIG_PIN = 6;
|
||||||
|
// const int ECHO_PIN = 5;
|
||||||
|
|
||||||
const long SONAR_MAX_DIST_CM = 400;
|
const long SONAR_MAX_DIST_CM = 400;
|
||||||
|
|
||||||
@@ -25,7 +28,13 @@ const int GEAR_THRESH_HIGH = 682;
|
|||||||
const int LCD_COLS = 16;
|
const int LCD_COLS = 16;
|
||||||
const int LCD_ROWS = 2;
|
const int LCD_ROWS = 2;
|
||||||
|
|
||||||
// Silnik
|
// Skrzynia biegów
|
||||||
const uint8_t CLUTCH_PIN = A7;
|
const uint8_t CLUTCH_PIN = A7;
|
||||||
const uint8_t BRAKE_PIN = A2;
|
const uint8_t BRAKE_PIN = A2;
|
||||||
const uint8_t THROTTLE_PIN = A3;
|
const uint8_t THROTTLE_PIN = A3;
|
||||||
|
|
||||||
|
// Silniki
|
||||||
|
const int IN_1 = 9;
|
||||||
|
const int IN_2 = 8;
|
||||||
|
const int IN_3 = 4;
|
||||||
|
const int IN_4 = 3;
|
||||||
|
|||||||
+16
-16
@@ -45,21 +45,21 @@ void Display::updateGear(char gearChar) {
|
|||||||
_lcd.print(' ');
|
_lcd.print(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
void Display::updateDirection(bool leftOn, bool rightOn) {
|
// void Display::updateDirection(bool leftOn, bool rightOn) {
|
||||||
if (leftOn && rightOn) {
|
// if (leftOn && rightOn) {
|
||||||
_lcd.setCursor(12, 0);
|
// _lcd.setCursor(12, 0);
|
||||||
_lcd.write((uint8_t)CHAR_LEFT);
|
// _lcd.write((uint8_t)CHAR_LEFT);
|
||||||
_lcd.print(' ');
|
// _lcd.print(' ');
|
||||||
_lcd.write((uint8_t)CHAR_RIGHT);
|
// _lcd.write((uint8_t)CHAR_RIGHT);
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
unsigned long now = millis();
|
// unsigned long now = millis();
|
||||||
bool leftBlink = leftOn && ((now / 1000UL) % 2 == 0);
|
// bool leftBlink = leftOn && ((now / 1000UL) % 2 == 0);
|
||||||
bool rightBlink = rightOn && ((now / 1000UL) % 2 == 0);
|
// bool rightBlink = rightOn && ((now / 1000UL) % 2 == 0);
|
||||||
|
|
||||||
_lcd.setCursor(12, 0);
|
// _lcd.setCursor(12, 0);
|
||||||
_lcd.write((uint8_t)(leftBlink ? CHAR_LEFT_BLINK : CHAR_LEFT));
|
// _lcd.write((uint8_t)(leftBlink ? CHAR_LEFT_BLINK : CHAR_LEFT));
|
||||||
_lcd.print(' ');
|
// _lcd.print(' ');
|
||||||
_lcd.write((uint8_t)(rightBlink ? CHAR_RIGHT_BLINK : CHAR_RIGHT));
|
// _lcd.write((uint8_t)(rightBlink ? CHAR_RIGHT_BLINK : CHAR_RIGHT));
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public:
|
|||||||
|
|
||||||
void updateGear(char gearChar);
|
void updateGear(char gearChar);
|
||||||
|
|
||||||
void updateDirection(bool leftOn, bool rightOn);
|
// void updateDirection(bool leftOn, bool rightOn);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
rgb_lcd _lcd;
|
rgb_lcd _lcd;
|
||||||
|
|||||||
+35
-35
@@ -14,74 +14,74 @@ float Engine::readNormalized(uint8_t pin) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::update(float dt, uint8_t gearIdx) {
|
void Engine::update(float dt, uint8_t gearIdx) {
|
||||||
Serial.println("----------------");
|
// Serial.println("----------------");
|
||||||
|
|
||||||
const float throttle = readNormalized(_throttlePin);
|
const float throttle = readNormalized(_throttlePin);
|
||||||
Serial.print("Throttle: ");
|
// Serial.print("Throttle: ");
|
||||||
Serial.println(throttle);
|
// Serial.println(throttle);
|
||||||
|
|
||||||
const float clutch = readNormalized(_clutchPin);
|
const float clutch = readNormalized(_clutchPin);
|
||||||
Serial.print("Clutch: ");
|
// Serial.print("Clutch: ");
|
||||||
Serial.println(clutch);
|
// Serial.println(clutch);
|
||||||
|
|
||||||
const float brake = readNormalized(_brakePin);
|
const float brake = readNormalized(_brakePin);
|
||||||
Serial.print("Brake: ");
|
// Serial.print("Brake: ");
|
||||||
Serial.println(brake);
|
// Serial.println(brake);
|
||||||
|
|
||||||
const float targetRPM = RPM_IDLE + throttle * (RPM_MAX - RPM_IDLE);
|
const float targetRPM = RPM_IDLE + throttle * (RPM_MAX - RPM_IDLE);
|
||||||
Serial.print("targetRPM: ");
|
// Serial.print("targetRPM: ");
|
||||||
Serial.println(targetRPM);
|
// Serial.println(targetRPM);
|
||||||
|
|
||||||
const float wheelRPM = (_speedMs / (2.0f * PI * WHEEL_RADIUS)) * 60.0f;
|
const float wheelRPM = (_speedMs / (2.0f * PI * WHEEL_RADIUS)) * 60.0f;
|
||||||
Serial.print("wheelRPM: ");
|
// Serial.print("wheelRPM: ");
|
||||||
Serial.println(wheelRPM);
|
// Serial.println(wheelRPM);
|
||||||
|
|
||||||
const float gearRatio = GEAR_RATIO[gearIdx];
|
const float gearRatio = GEAR_RATIO[gearIdx];
|
||||||
Serial.print("gearRatio: ");
|
// Serial.print("gearRatio: ");
|
||||||
Serial.println(gearRatio);
|
// Serial.println(gearRatio);
|
||||||
|
|
||||||
const float drivenRPM = wheelRPM * fabs(gearRatio) * FINAL_DRIVE;
|
const float drivenRPM = wheelRPM * fabs(gearRatio) * FINAL_DRIVE;
|
||||||
Serial.print("drivenRPM: ");
|
// Serial.print("drivenRPM: ");
|
||||||
Serial.println(drivenRPM);
|
// Serial.println(drivenRPM);
|
||||||
|
|
||||||
_engineRPM = clutch * drivenRPM + (1.0f - clutch) * targetRPM;
|
_engineRPM = clutch * drivenRPM + (1.0f - clutch) * targetRPM;
|
||||||
Serial.print("_engineRPM: ");
|
// Serial.print("_engineRPM: ");
|
||||||
Serial.println(_engineRPM);
|
// Serial.println(_engineRPM);
|
||||||
|
|
||||||
_engineRPM = constrain(_engineRPM, RPM_IDLE, 7500.0f);
|
_engineRPM = constrain(_engineRPM, RPM_IDLE, 7500.0f);
|
||||||
Serial.print("_engineRPM (constrain): ");
|
// Serial.print("_engineRPM (constrain): ");
|
||||||
Serial.println(_engineRPM);
|
// Serial.println(_engineRPM);
|
||||||
|
|
||||||
const float engineForce = (_engineRPM / RPM_MAX) * (1.0f - clutch) * ENGINE_FORCE_FACTOR;
|
const float engineForce = (_engineRPM / RPM_MAX) * (1.0f - clutch) * ENGINE_FORCE_FACTOR;
|
||||||
Serial.print("engineForce: ");
|
// Serial.print("engineForce: ");
|
||||||
Serial.println(engineForce);
|
// Serial.println(engineForce);
|
||||||
|
|
||||||
const float brakeForce = brake * BRAKE_FORCE_MAX;
|
const float brakeForce = brake * BRAKE_FORCE_MAX;
|
||||||
Serial.print("brakeForce: ");
|
// Serial.print("brakeForce: ");
|
||||||
Serial.println(brakeForce);
|
// Serial.println(brakeForce);
|
||||||
|
|
||||||
const float dragForce = 0.5f * DRAG_COEFF * FRONTAL_AREA * (_speedMs * _speedMs);
|
const float dragForce = 0.5f * DRAG_COEFF * FRONTAL_AREA * (_speedMs * _speedMs);
|
||||||
Serial.print("dragForce: ");
|
// Serial.print("dragForce: ");
|
||||||
Serial.println(dragForce);
|
// Serial.println(dragForce);
|
||||||
|
|
||||||
const int direction = (gearIdx == 5) ? -1 : 1;
|
const int direction = (gearIdx == -1) ? -1 : 1;
|
||||||
Serial.print("direction: ");
|
// Serial.print("direction: ");
|
||||||
Serial.println(direction);
|
// Serial.println(direction);
|
||||||
|
|
||||||
const float netForce = direction * engineForce - brakeForce - dragForce;
|
const float netForce = direction * engineForce - brakeForce - dragForce;
|
||||||
Serial.print("netForce: ");
|
// Serial.print("netForce: ");
|
||||||
Serial.println(netForce);
|
// Serial.println(netForce);
|
||||||
|
|
||||||
const float acceleration = netForce / CAR_MASS;
|
const float acceleration = netForce / CAR_MASS;
|
||||||
Serial.print("acceleration: ");
|
// Serial.print("acceleration: ");
|
||||||
Serial.println(acceleration);
|
// Serial.println(acceleration);
|
||||||
|
|
||||||
_speedMs += acceleration * dt;
|
_speedMs += acceleration * dt;
|
||||||
if (_speedMs < 0.0f) _speedMs = 0.0f;
|
if (_speedMs < 0.0f) _speedMs = 0.0f;
|
||||||
Serial.print("_speedMs: ");
|
// Serial.print("_speedMs: ");
|
||||||
Serial.println(_speedMs);
|
// Serial.println(_speedMs);
|
||||||
|
|
||||||
delay(1000);
|
// delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Engine::getRPM() const {
|
int Engine::getRPM() const {
|
||||||
|
|||||||
+28
-17
@@ -3,26 +3,28 @@
|
|||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "Display.h"
|
#include "Display.h"
|
||||||
#include "Gear.h"
|
#include "Gear.h"
|
||||||
#include "Sonar.h"
|
// #include "Sonar.h"
|
||||||
#include "RFID.h"
|
#include "RFID.h"
|
||||||
#include "Engine.h"
|
#include "Engine.h"
|
||||||
|
|
||||||
Display display;
|
Display display;
|
||||||
GearSelector gear(GEAR_X_PIN, GEAR_Y_PIN, GEAR_THRESH_LOW, GEAR_THRESH_HIGH);
|
GearSelector gear(GEAR_X_PIN, GEAR_Y_PIN, GEAR_THRESH_LOW, GEAR_THRESH_HIGH);
|
||||||
Sonar sonar(TRIG_PIN, ECHO_PIN, SONAR_MAX_DIST_CM);
|
// Sonar sonar(TRIG_PIN, ECHO_PIN, SONAR_MAX_DIST_CM);
|
||||||
RFIDReader rfid(SS_PIN, RST_PIN);
|
RFIDReader rfid(SS_PIN, RST_PIN);
|
||||||
Engine engine(THROTTLE_PIN, CLUTCH_PIN, BRAKE_PIN);
|
Engine engine(THROTTLE_PIN, CLUTCH_PIN, BRAKE_PIN);
|
||||||
|
|
||||||
bool leftBlink = false;
|
|
||||||
bool rightBlink = false;
|
|
||||||
|
|
||||||
unsigned long prevUpdate = 0;
|
unsigned long prevUpdate = 0;
|
||||||
|
|
||||||
|
bool carEnabled = false;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
pinMode(TS_LEFT, INPUT);
|
// pinMode(TS_LEFT, INPUT);
|
||||||
pinMode(TS_RIGHT, INPUT);
|
// pinMode(TS_RIGHT, INPUT);
|
||||||
|
|
||||||
|
pinMode(IN_1, OUTPUT);
|
||||||
|
pinMode(IN_2, OUTPUT);
|
||||||
|
|
||||||
rfid.begin();
|
rfid.begin();
|
||||||
display.begin(LCD_COLS, LCD_ROWS);
|
display.begin(LCD_COLS, LCD_ROWS);
|
||||||
@@ -30,13 +32,25 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (gear.update()) {
|
bool check = rfid.check();
|
||||||
display.updateGear(gear.getGearChar());
|
bool matches = rfid.matches(AUTH_UID, AUTH_UID_SIZE);
|
||||||
}
|
|
||||||
|
|
||||||
leftBlink = digitalRead(TS_LEFT);
|
bool rfidOk = check && matches;
|
||||||
rightBlink = digitalRead(TS_RIGHT);
|
|
||||||
display.updateDirection(leftBlink, rightBlink);
|
if (rfid.check() && rfid.matches(AUTH_UID, AUTH_UID_SIZE)) {
|
||||||
|
delay(3000);
|
||||||
|
carEnabled = !carEnabled;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (carEnabled) {
|
||||||
|
Serial.println("Działa");
|
||||||
|
|
||||||
|
if (gear.update())
|
||||||
|
display.updateGear(gear.getGearChar());
|
||||||
|
|
||||||
|
// display.updateDirection(
|
||||||
|
// digitalRead(TS_LEFT),
|
||||||
|
// digitalRead(TS_RIGHT));
|
||||||
|
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
float dt = (now - prevUpdate) / 1000.0f;
|
float dt = (now - prevUpdate) / 1000.0f;
|
||||||
@@ -47,10 +61,7 @@ void loop() {
|
|||||||
display.updateSpeed(engine.getSpeedKmh());
|
display.updateSpeed(engine.getSpeedKmh());
|
||||||
}
|
}
|
||||||
|
|
||||||
long dist = sonar.measure();
|
// long dist = sonar.measure();
|
||||||
// Serial.println(dist);
|
// Serial.println(dist);
|
||||||
|
|
||||||
if (rfid.check()) {
|
|
||||||
rfid.printUID();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,18 +11,30 @@ void RFIDReader::begin() {
|
|||||||
bool RFIDReader::check() {
|
bool RFIDReader::check() {
|
||||||
if (!_rfid.PICC_IsNewCardPresent()) return false;
|
if (!_rfid.PICC_IsNewCardPresent()) return false;
|
||||||
if (!_rfid.PICC_ReadCardSerial()) return false;
|
if (!_rfid.PICC_ReadCardSerial()) return false;
|
||||||
|
// _rfid.PICC_HaltA(); // stop the tag
|
||||||
|
// _rfid.PCD_StopCrypto1(); // clear RC522 crypto
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RFIDReader::reset() {
|
||||||
_rfid.PICC_HaltA();
|
_rfid.PICC_HaltA();
|
||||||
|
_rfid.PCD_StopCrypto1();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RFIDReader::matches(const byte* expectedUid, byte expectedSize) const {
|
||||||
|
if (_rfid.uid.size != expectedSize) return false;
|
||||||
|
for (byte i = 0; i < expectedSize; ++i) {
|
||||||
|
if (_rfid.uid.uidByte[i] != expectedUid[i]) return false;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RFIDReader::printUID() const {
|
void RFIDReader::printUID() const {
|
||||||
Serial.print(F("RFID Tag UID:"));
|
Serial.println(F("RFID Tag UID:"));
|
||||||
for (byte i = 0; i < _rfid.uid.size; i++) {
|
// for (byte i = 0; i < _rfid.uid.size; i++) {
|
||||||
Serial.print(_rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
|
// Serial.print(_rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
|
||||||
Serial.print(_rfid.uid.uidByte[i], HEX);
|
// Serial.print(_rfid.uid.uidByte[i], HEX);
|
||||||
}
|
// }
|
||||||
Serial.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const byte* RFIDReader::getUID() const {
|
const byte* RFIDReader::getUID() const {
|
||||||
|
|||||||
@@ -10,8 +10,12 @@ public:
|
|||||||
|
|
||||||
bool check();
|
bool check();
|
||||||
|
|
||||||
|
void reset();
|
||||||
|
|
||||||
void printUID() const;
|
void printUID() const;
|
||||||
|
|
||||||
|
bool matches(const byte* expectedUid, byte expectedSize) const;
|
||||||
|
|
||||||
const byte* getUID() const;
|
const byte* getUID() const;
|
||||||
byte getUIDSize() const;
|
byte getUIDSize() const;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user