diff --git a/.gitignore b/.gitignore index b032226..99f75cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /build -firmware.hex +/hex +/.settings .cproject .project -/.settings diff --git a/Makefile b/Makefile index 4b496bb..06957ca 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ -ORACCRSC := /daten/Projekte/Calliope/OpenRoberta/ora-cc-rsc/RobotMbed/libs2017 +ORACCRSC := ../../OpenRoberta/ora-cc-rsc/RobotMbed/libs2017 BUILD := build +HEX := hex MICROBITPATH := $(ORACCRSC)/microbit MICROBITDALPATH := $(ORACCRSC)/microbit-dal @@ -26,7 +27,8 @@ INCDEMO := $(INCLIB) \ -I $(MICROBITPATH)/inc \ -I $(BLEPATH) \ -I $(MICROBITDALPATH)/inc/types \ - -I $(MICROBITDALPATH)/inc/bluetooth + -I $(MICROBITDALPATH)/inc/bluetooth \ + -I lib LDLIBS := -lnosys -lstdc++ -lsupc++ -lm -lc -lgcc -lstdc++ -lsupc++ -lm -lc -lgcc CALLIOPELIBS := $(ORACCRSC)/microbit.a \ @@ -35,36 +37,37 @@ CALLIOPELIBS := $(ORACCRSC)/microbit.a \ $(ORACCRSC)/ble-nrf51822.a \ $(ORACCRSC)/nrf51-sdk.a \ $(ORACCRSC)/mbed-classic.a + +DEMOS := CarControl.hex MotorTest.hex SimpleMotorTest.hex -all: directories lib demo +all: directories lib $(DEMOS) directories: mkdir -p $(BUILD) + mkdir -p $(HEX) lib: DcMotor.a - -demo: firmware.hex - -firmware.hex: MotorTest.hex - srec_cat $(BLE_BOOTLOADER_RESERVED_HEX) -intel $(S110NRF51822_HEX) -intel $(BUILD)/$< -intel -o $@ -intel --line-length=44 +%.hex: %.ihex + srec_cat $(BLE_BOOTLOADER_RESERVED_HEX) -intel $(S110NRF51822_HEX) -intel $(BUILD)/$< -intel -o $(HEX)/$@ -intel --line-length=44 -MotorTest.hex: MotorTest +%.ihex: %.a arm-none-eabi-objcopy -O ihex $(BUILD)/$< $(BUILD)/$@ -MotorTest: MotorTest.o DcMotor.a +%.a: %.o DcMotor.a arm-none-eabi-g++ $(LDFLAGS) -T $(NRF51822) -Wl,-Map,$(BUILD)/MotorTest.map -Wl,--start-group $(foreach i,$^,$(BUILD)/$(i)) $(LDLIBS) $(CALLIOPELIBS) -Wl,-end-group -o $(BUILD)/$@ -MotorTest.o: src/MotorTest.cpp +%.o: src/%.cpp arm-none-eabi-g++ $(INCDEMO) $(CPPLAGS) -MT $(BUILD)/$@.cpp.o -MF $(BUILD)/$@.cpp.o.d -o $(BUILD)/$@ -c $< DcMotor.a: DcMotor.o ar rvs $(BUILD)/$@ $(BUILD)/$^ -DcMotor.o: src/DcMotor.cpp +DcMotor.o: lib/DcMotor.cpp arm-none-eabi-g++ $(INCLIB) $(CPPLAGS) -MT $(BUILD)/$@.o -MF $(BUILD)/$@.o.d -c -o $(BUILD)/$@ $< clean: - -rm firmware.hex $(BUILD)/* + -rm $(HEX)/* $(BUILD)/* + -rmdir $(HEX) -rmdir $(BUILD) diff --git a/OpenRoberta/CarControl.xml b/OpenRoberta/CarControl.xml new file mode 100644 index 0000000..74c5881 --- /dev/null +++ b/OpenRoberta/CarControl.xml @@ -0,0 +1 @@ +Motor1Number0Motor2Number0_R_R#ff0000EQKEY_PRESSEDATRUE2000_R#ffff33EQKEY_PRESSEDATRUEMotor15Motor2-5_R#33ffffEQKEY_PRESSEDBTRUEMotor1-5Motor25_R#000099EQPINTOUCH_PRESSED1TRUEMotor10Motor20_R#ffffffEQPINTOUCH_PRESSED0TRUEMotor125Motor225_R#ff0000EQPINTOUCH_PRESSED3TRUEMotor1-25Motor2-25_R#6600cc250_R#009900checkAndWriteMotor1Motor1-100100Motor2Motor2-1001001Motor12Motor2i0DIVIDEMotor12010i5k0DIVIDEMotor1-2011k5j0DIVIDEMotor22014j5m0DIVIDEMotor2-2013m5AABB_A_R_B_S_C_T_G_Lundefined \ No newline at end of file diff --git a/OpenRoberta/Motortest.xml b/OpenRoberta/Motortest.xml new file mode 100644 index 0000000..5eea8f1 --- /dev/null +++ b/OpenRoberta/Motortest.xml @@ -0,0 +1 @@ +aNumber0bNumber0_R_R#ff0000EQKEY_PRESSEDATRUE2000_R#ffff33EQKEY_PRESSEDATRUEaADDa251a_R#3366ffEQKEY_PRESSEDBTRUEbADDb252b_R#cc33ccEQPINTOUCH_PRESSED1TRUEa0b01a2b_R#ffffffEQPINTOUCH_PRESSED0TRUEaMINUSa251a_R#cc0000EQPINTOUCH_PRESSED3TRUEbMINUSb252b_R#ff6600250_R#009900AABB_A_R_B_S_C_T_G_Lundefined \ No newline at end of file diff --git a/OpenRoberta/SimpleMotorTest.xml b/OpenRoberta/SimpleMotorTest.xml new file mode 100644 index 0000000..e466866 --- /dev/null +++ b/OpenRoberta/SimpleMotorTest.xml @@ -0,0 +1 @@ +speedNumber0oldspeedNumber-1_R#009900PRESSEDAspeedADDspeed10PRESSEDBspeedMINUSspeed10LTspeed0speed100GTspeed100speed0NEQspeedoldspeed_R#ff0000oldspeedspeedTEXTROUNDDIVIDEspeed101speed_R#009900500Port_AABB_S_L_G_CundefinedAA_A_T_R_B \ No newline at end of file diff --git a/README.md b/README.md index 2b0b5b9..a2946f8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # DC Motor Library and Demo -This repository contains a library to control the DC Motor Module with the Calliope Mini and an example program. +This repository contains a library to control the DC Motor Module with the Calliope Mini and some examples. ## Compiling the firmware diff --git a/src/DcMotor.cpp b/lib/DcMotor.cpp similarity index 100% rename from src/DcMotor.cpp rename to lib/DcMotor.cpp diff --git a/src/DcMotor.h b/lib/DcMotor.h similarity index 100% rename from src/DcMotor.h rename to lib/DcMotor.h diff --git a/src/CarControl.cpp b/src/CarControl.cpp new file mode 100644 index 0000000..e313788 --- /dev/null +++ b/src/CarControl.cpp @@ -0,0 +1,111 @@ +/* + * DC Motor Lib + * + * Copyright (C) 2021 Christian Poulter + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#define _GNU_SOURCE + +#include "MicroBit.h" +#include "NEPODefs.h" +#include +#include +#include +MicroBit _uBit; +#include "DcMotor.h" +DcMotor dcMotor; + +void checkAndWrite(); + +double ___Motor1; +double ___Motor2; + +int main() +{ + _uBit.init(); + ___Motor1 = 0; + ___Motor2 = 0; + _uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE); + dcMotor.init(); + _uBit.rgb.off(); + _uBit.rgb.setColour(MicroBitColor(255, 0, 0, 255)); + while (true) { + if ( _uBit.buttonA.isPressed() == true ) { + break; + } + _uBit.sleep(_ITERATION_SLEEP_TIMEOUT); + } + _uBit.sleep(2000); + _uBit.rgb.setColour(MicroBitColor(255, 255, 51, 255)); + while ( true ) { + if ( _uBit.buttonA.isPressed() == true ) { + ___Motor1 += 5; + ___Motor2 += -5; + checkAndWrite(); + _uBit.rgb.setColour(MicroBitColor(51, 255, 255, 255)); + } else if ( _uBit.buttonB.isPressed() == true ) { + ___Motor1 += -5; + ___Motor2 += 5; + checkAndWrite(); + _uBit.rgb.setColour(MicroBitColor(0, 0, 153, 255)); + } else if ( _uBit.io.P0.isTouched() == true ) { + ___Motor1 = 0; + ___Motor2 = 0; + checkAndWrite(); + _uBit.rgb.setColour(MicroBitColor(255, 255, 255, 255)); + } else if ( _uBit.io.P12.isTouched() == true ) { + ___Motor1 += 25; + ___Motor2 += 25; + checkAndWrite(); + _uBit.rgb.setColour(MicroBitColor(255, 0, 0, 255)); + } else if ( _uBit.io.P16.isTouched() == true ) { + ___Motor1 += -25; + ___Motor2 += -25; + checkAndWrite(); + _uBit.rgb.setColour(MicroBitColor(102, 0, 204, 255)); + } + _uBit.sleep(250); + _uBit.rgb.setColour(MicroBitColor(0, 153, 0, 255)); + _uBit.sleep(_ITERATION_SLEEP_TIMEOUT); + } + dcMotor.release(); + release_fiber(); +} + +void checkAndWrite() { + ___Motor1 = min(max(___Motor1, -100), 100); + ___Motor2 = min(max(___Motor2, -100), 100); + dcMotor.set(Motor::M1, ___Motor1); + dcMotor.set(Motor::M2, ___Motor2); + _uBit.display.clear(); + for (int ___i = 0; ___i < ___Motor1 / ((float) 20); ___i += 1) { + _uBit.display.image.setPixelValue(0, ___i, (5) * _SET_BRIGHTNESS_MULTIPLIER); + _uBit.sleep(_ITERATION_SLEEP_TIMEOUT); + } + for (int ___k = 0; ___k < ___Motor1 / ((float) -20); ___k += 1) { + _uBit.display.image.setPixelValue(1, ___k, (5) * _SET_BRIGHTNESS_MULTIPLIER); + _uBit.sleep(_ITERATION_SLEEP_TIMEOUT); + } + for (int ___j = 0; ___j < ___Motor2 / ((float) 20); ___j += 1) { + _uBit.display.image.setPixelValue(4, ___j, (5) * _SET_BRIGHTNESS_MULTIPLIER); + _uBit.sleep(_ITERATION_SLEEP_TIMEOUT); + } + for (int ___m = 0; ___m < ___Motor2 / ((float) -20); ___m += 1) { + _uBit.display.image.setPixelValue(3, ___m, (5) * _SET_BRIGHTNESS_MULTIPLIER); + _uBit.sleep(_ITERATION_SLEEP_TIMEOUT); + } +} diff --git a/src/MotorTest.cpp b/src/MotorTest.cpp index e2ea137..67773f8 100644 --- a/src/MotorTest.cpp +++ b/src/MotorTest.cpp @@ -29,7 +29,6 @@ MicroBit _uBit; #include "DcMotor.h" DcMotor dcMotor; - double ___a; double ___b; @@ -74,7 +73,7 @@ int main() dcMotor.set(Motor::M2, ___b); _uBit.rgb.setColour(MicroBitColor(255, 102, 0, 255)); } - _uBit.sleep(1000); + _uBit.sleep(250); _uBit.rgb.setColour(MicroBitColor(0, 153, 0, 255)); _uBit.sleep(_ITERATION_SLEEP_TIMEOUT); } diff --git a/src/SimpleMotorTest.cpp b/src/SimpleMotorTest.cpp new file mode 100644 index 0000000..8e224fb --- /dev/null +++ b/src/SimpleMotorTest.cpp @@ -0,0 +1,69 @@ +/* + * DC Motor Lib + * + * Copyright (C) 2021 Christian Poulter + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#define _GNU_SOURCE + +#include "MicroBit.h" +#include "NEPODefs.h" +#include +#include +#include +MicroBit _uBit; +#include "DcMotor.h" +DcMotor dcMotor; + +double ___speed; +double ___oldspeed; + +int main() +{ + _uBit.init(); + ___speed = 0; + ___oldspeed = -1; + + dcMotor.init(); + _uBit.display.clear(); + _uBit.rgb.setColour(MicroBitColor(0, 153, 0, 255)); + while ( true ) { + if ( _uBit.buttonA.isPressed() ) { + ___speed = ___speed + 10; + } + if ( _uBit.buttonB.isPressed() ) { + ___speed = ___speed - 10; + } + if ( ___speed < 0 ) { + ___speed = 100; + } + if ( ___speed > 100 ) { + ___speed = 0; + } + if ( !( ___speed == ___oldspeed ) ) { + _uBit.rgb.setColour(MicroBitColor(255, 0, 0, 255)); + ___oldspeed = ___speed; + _uBit.display.scroll(ManagedString(round(___speed / ((float) 10)))); + dcMotor.set(Motor::M1, ___speed); + _uBit.rgb.setColour(MicroBitColor(0, 153, 0, 255)); + } + _uBit.sleep(500); + _uBit.sleep(_ITERATION_SLEEP_TIMEOUT); + } + dcMotor.release(); + release_fiber(); +}