Czosnekengine

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
yruiewyrui3
Date:
Wed May 18 12:08:19 2016 +0000
Commit message:
losowo przod i tyl

Changed in this revision

Engine.cpp Show annotated file Show diff for this revision Revisions of this file
Engine.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Engine.cpp	Wed May 18 12:08:19 2016 +0000
@@ -0,0 +1,37 @@
+#include "Engine.h"
+
+// przelicza procenty na pwn:
+// - wartosc poczatkowa - 0.2377,
+// - prog - 0.0077
+float Engine::getPwm(float speed){
+    if (speed == 0)
+        return 0;
+    else if (speed > 0)
+        return 0.2377f + (fabs(speed) - 1) * 0.0077f;
+    else
+        return 0.406f + (fabs(speed) - 1) * 0.006f;
+    }
+
+void Engine::move(int speed){
+    if (speed == 0){
+        //soft stop
+        _fwd = 0;
+        _rev = 0;  
+        return;
+    }
+        
+    if (speed < 0){
+        _fwd = 0;
+        _rev = 1;
+    }
+    else{
+        _fwd = 1;
+        _rev = 0;
+    }
+    _pwm = getPwm(speed);
+}
+
+//hard stop
+void Engine::stop(){
+    _pwm = 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Engine.h	Wed May 18 12:08:19 2016 +0000
@@ -0,0 +1,22 @@
+#ifndef ENGINE_H
+#define ENGINE_H
+
+#include "mbed.h"
+
+
+class Engine{
+  private:
+    PwmOut _pwm;
+    DigitalOut _fwd;
+    DigitalOut _rev;
+    
+    float getPwm(float val);
+    
+  public:
+    Engine(PinName pwm, PinName fwd, PinName rev): _pwm(pwm), _fwd(fwd), _rev(rev){}
+    void move(int speed); // silniki pracuja z okreslona predkoscia (speed ma zakres od -1 do 1)
+    void stop();    //hard stop
+};
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 18 12:08:19 2016 +0000
@@ -0,0 +1,36 @@
+#include "Engine.h"
+
+Serial pc(SERIAL_TX, SERIAL_RX);
+int liczba =0;
+int main() {
+    //inicjalizacja silnikow - piny dzialaja:
+    Engine eng_left = Engine(PB_13, PB_4, PB_10);
+    Engine eng_right = Engine(PB_14, PB_5, PB_3);
+    
+    
+    while(true) {
+        // testy - przyspiesz od 0 do 100% a potem zwolnij:
+            eng_left.move(0);
+            eng_right.move(0);
+            wait(1);
+            srand( time( NULL ) );
+            liczba = (std::rand() % 200 ) - 100;
+            eng_left.move(liczba);
+            eng_right.move(liczba);
+            wait(5);
+            if (liczba==0){
+                pc.printf("%f", time(NULL));
+                pc.printf("Nie ma wiatru, wiec stoje w miejscu.");
+                }
+            else if (liczba>0){
+                pc.printf("%f", time(NULL));
+                pc.printf("Jade do przodu. Predkosc w procentach to %d \n", liczba);
+                }
+            else {
+                pc.printf("%f", time(NULL));
+                pc.printf("Jade do tylu. Predkosc w procentach to %d \n", liczba);
+                }
+        
+    }
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed May 18 12:08:19 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/7c328cabac7e
\ No newline at end of file