Browse Source

initial connect

master
mortie 6 years ago
parent
commit
feefae6af9
5 changed files with 249 additions and 33 deletions
  1. 104
    33
      esp/esp.ino
  2. 84
    0
      i2cscanner/i2cscanner.ino
  3. 26
    0
      simplecontroller/simplecontroller.ino
  4. 15
    0
      simpledisplay/simpledisplay.ino
  5. 20
    0
      testtool/testtool.ino

+ 104
- 33
esp/esp.ino View File

@@ -1,56 +1,127 @@
#include <Servo.h>
#include <SSD1306Ascii.h>
#include <SSD1306AsciiAvrI2c.h>

#define PIN_POT A0
#define PIN_BTN 2
#define PIN_ESC 5
#define MIN_SPEED 1000
#define MAX_SPEED 1500
#define DELAY 10
#define PIN_THERM A7
#define PIN_BAT A2

#define MIN_SPEED 900
#define MAX_SPEED 1700
#define ZERO_SPEED 900
#define POT_MIN 183
#define POT_MAX 717

#define STEP 10
#define DISPLAY_I2C 0x3C
#define DISPLAY_CHARS 11
#define THERM_RESISTOR 10000

#define THRESHOLD 5
#define DELAY 50

//#define USE_DISPLAY

Servo esc;
#ifdef USE_DISPLAY
SSD1306AsciiAvrI2c display;
#endif

void setup() {
Serial.begin(115200);
Serial.println("Hello");

#ifdef USE_DISPLAY
Serial.println("Looking for i3c 0x"+String(DISPLAY_I2C, 16)+"...");
display.begin(&Adafruit128x32, DISPLAY_I2C);
display.setFont(Adafruit5x7);
display.clear();
display.set2X();
display.print("Hello");
#endif
pinMode(PIN_BTN, INPUT_PULLUP);
pinMode(PIN_POT, INPUT);
pinMode(PIN_THERM, INPUT);
pinMode(PIN_BAT, INPUT);
esc.attach(PIN_ESC);
esc.writeMicroseconds(MIN_SPEED);
esc.writeMicroseconds(ZERO_SPEED);
delay(4000);
Serial.println("ready");
}

float speedFunc(float t) {
return 0.15 + 0.000000005 * t * t * t;
int prev = -1;

float measureTemp() {
int val = analogRead(PIN_THERM);
float r2 = (THERM_RESISTOR / 10.0) / (1023.0 / (float)val - 1.0);
float logR2 = log(r2);
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
float t = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
float celsius = t - 273.15;
return celsius;
}

int measureBat() {
int val = analogRead(PIN_BAT);
float volt = val * (5.0 / 1023.0);
Serial.println(volt);
return 50;
}

void printTwo(String s1, String s2) {
#ifdef USE_DISPLAY
int nspaces = DISPLAY_CHARS - (s1.length() + s2.length());
char spaces[DISPLAY_CHARS + 1];
memset(spaces, ' ', DISPLAY_CHARS);
spaces[nspaces] = '\0';
display.println(s1+String(spaces)+s2);
#endif
}

void showStats(int actualSpeed, int potmeterVal) {
int throttle = ((double)(actualSpeed - MIN_SPEED) / (double)(MAX_SPEED - MIN_SPEED)) * 100;

#ifdef USE_DISPLAY
display.setCursor(0, 0);
display.set2X();
#endif

String pot = String(potmeterVal);
String thr = String(throttle)+"%";
String spd = String(actualSpeed);
String tmp = String(round(measureTemp()))+"C";
String bat = String(measureBat())+"%";

printTwo(thr, pot+" ");
printTwo(tmp, bat+" ");
}

void loop() {
int pressed = 0;
int t = 0;
int reachedMax = 0;
while (digitalRead(PIN_BTN) == LOW) {
pressed = 1;
int pot = analogRead(PIN_POT);
int speed = map(pot, 0, 1023, 0, MAX_SPEED - MIN_SPEED);

int actualSpeed;
if (!reachedMax) {
float multiplier = min(speedFunc(t), 1);
if (multiplier > 1) {
multiplier = 1;
reachedMax = 1;
}
actualSpeed = speed * multiplier + MIN_SPEED;
} else {
actualSpeed = speed + MIN_SPEED;
}

Serial.println(actualSpeed);

pressed = 1;
int pot = analogRead(PIN_POT);
int speed = map(pot, POT_MIN, POT_MAX, 0, MAX_SPEED - MIN_SPEED);
speed = speed / STEP;
speed = speed * STEP;

int actualSpeed = speed + MIN_SPEED;

if (abs(actualSpeed - ZERO_SPEED) < THRESHOLD)
actualSpeed = ZERO_SPEED;
else if (actualSpeed > MAX_SPEED)
actualSpeed = MAX_SPEED;
else if (actualSpeed < MIN_SPEED)
actualSpeed = MIN_SPEED;

if (actualSpeed != prev) {
esc.writeMicroseconds(actualSpeed);
delay(DELAY);
t += DELAY;
prev = actualSpeed;
}

if (pressed)
esc.writeMicroseconds(0);
showStats(actualSpeed, pot);
delay(DELAY);
}

+ 84
- 0
i2cscanner/i2cscanner.ino View File

@@ -0,0 +1,84 @@
// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}


+ 26
- 0
simplecontroller/simplecontroller.ino View File

@@ -0,0 +1,26 @@
#include <Servo.h>

#define PIN_ESC 3
#define MIN_SPEED 1000
#define MAX_SPEED 2000
#define ZERO_SPEED 1500

Servo esc;

void setup() {
Serial.begin(115200);
Serial.println("Hello");
delay(10000);
Serial.println("Writing ZERO_SPEED");
esc.attach(PIN_ESC);
esc.writeMicroseconds(0);
esc.writeMicroseconds(ZERO_SPEED);
delay(10000);
Serial.println("Writing MAX_SPEED");
esc.writeMicroseconds(MAX_SPEED);

}

void loop() {
}

+ 15
- 0
simpledisplay/simpledisplay.ino View File

@@ -0,0 +1,15 @@
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

void setup() {
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.println("Hi");
}

void loop() {}

+ 20
- 0
testtool/testtool.ino View File

@@ -0,0 +1,20 @@
#include <Servo.h>

#define PIN_ESC 5

Servo esc;

void setup() {
Serial.begin(115200);
Serial.setTimeout(10);

esc.attach(PIN_ESC);
}

void loop() {
String s = Serial.readString();
if (s == "")
return;
esc.writeMicroseconds(s.toInt());
Serial.println(s.toInt());
}

Loading…
Cancel
Save