commit 2b028d543e69b4f8fe9af4be3a41d245cbe65876 Author: willem oldemans Date: Fri Sep 11 21:08:12 2020 +0200 update for github diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..37e17f7 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..0f0d740 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..382f1d2 --- /dev/null +++ b/README.rst @@ -0,0 +1,38 @@ +.. Copyright 2014-present PlatformIO + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +How to build PlatformIO based project +===================================== + +1. `Install PlatformIO Core `_ +2. Download `development platform with examples `_ +3. Extract ZIP archive +4. Run these commands: + +.. code-block:: bash + + # Change directory to example + > cd platform-ststm32/examples/arduino-blink + + # Build project + > platformio run + + # Upload firmware + > platformio run --target upload + + # Build specific environment + > platformio run -e maple + + # Upload firmware for the specific environment + > platformio run -e maple --target upload + + # Clean build files + > platformio run --target clean diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..7804d6c --- /dev/null +++ b/platformio.ini @@ -0,0 +1,22 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:generic STM32F103TB] +platform = ststm32 +framework = arduino +board = genericSTM32F103TB +upload_protocol = stlink +debug_tool = stlink +board_build.f_cpu = 72000000L +monitor_speed = 115200 +lib_deps = + danya0x07/NRF24L01_SimpleLibrary@^1.0.0 + pkourany/MPU6050@^1.0.3 + sstaub/Ticker@^3.2.0 diff --git a/src/MC34933.cpp b/src/MC34933.cpp new file mode 100644 index 0000000..e73fa6f --- /dev/null +++ b/src/MC34933.cpp @@ -0,0 +1,146 @@ +#include +#include "MC34933.h" + +void MC34933::begin(void) +{ + pinMode(m_MA, OUTPUT); + pinMode(m_MB, OUTPUT); + //init pins Hi-Z + digitalWrite(m_MA, 1); + digitalWrite(m_MB, 1); + + +} + +void MC34933::setDirection(e_direction dir) +{ + m_prevDirection = m_direction; + m_direction = dir; +} + +void MC34933::start( void ) +{ + m_startCmd = true; +} + +bool MC34933::start( int speed ) +{ + if(setSpeed(speed)) + { + m_startCmd = true; + return true; + } + return false; +} + +bool MC34933::start( int speed, int duration_ms) +{ + if(setSpeed(speed)) + { + m_startCmd = true; + return true; + } + return false; +} + +bool MC34933::setSpeed(int speed) +{ + if(speed <= 255) + { + m_speed = speed; + return true; + } + return false; +} + +void MC34933::stop ( void ) +{ + m_stopCmd = true; +} + +void MC34933::forward(int speed) +{ + setDirection(LEFT); + if(setSpeed(speed)) + { + start(); + } + +} + +void MC34933::backward(int speed) +{ + setDirection(RIGHT); + if(setSpeed(speed)) + { + start(); + } +} + + +//motor statemachine +void MC34933::run( void ) +{ + switch(m_status) + { + case STARTING: + { + m_startCmd = false; //clear start flag + if(m_direction == LEFT) + { + analogWrite(m_MA, m_speed); + digitalWrite(m_MB, 1); + } + if(m_direction == RIGHT) + { + analogWrite(m_MB, m_speed); + digitalWrite(m_MA, 1); + } + m_status = RUN; + } + break; + + case RUN: + { + if(m_stopCmd ) + { + m_status = STOPPING; + + } + + if(m_direction != m_prevDirection) + { + m_status = STOPPING; + m_startCmd = true; + } + } + break; + + case STOPPING: + { + m_stopCmd = false; //clear stopflag + //set both pins to break; + analogWrite(m_MA, 0); + analogWrite(m_MB, 0); + digitalWrite(m_MA, 0); + digitalWrite(m_MB, 0); + m_status = IDLE; + } + break; + + default: + case IDLE: + { + if(m_startCmd) + { + m_status = STARTING; + } + //write pins Hi-z + analogWrite(m_MA, 255); + analogWrite(m_MB, 255); + digitalWrite(m_MA, 1); + digitalWrite(m_MB, 1); + } + break; + } +} \ No newline at end of file diff --git a/src/MC34933.h b/src/MC34933.h new file mode 100644 index 0000000..0742b4e --- /dev/null +++ b/src/MC34933.h @@ -0,0 +1,52 @@ +#ifndef MC34933MOTORH +#define MC34933MOTORH + +enum e_direction +{ + LEFT, + RIGHT +}; + +enum e_status +{ + IDLE, + STARTING, + RUN, + STOPPING +}; + +class MC34933 +{ + private: + const int m_MA; + const int m_MB; + e_direction m_direction = LEFT; + e_direction m_prevDirection; + int m_speed; + e_status m_status; + bool m_startCmd = false; + bool m_stopCmd = false; + + + public: + MC34933( int pin_ma, int pin_mb) + : m_MA{pin_ma}, m_MB{pin_mb} {} + + void begin( void ); + void setDirection( e_direction dir); + e_direction getDirection( void ) {return m_direction;} + bool setSpeed( int speed ); + int getSpeed( void ) {return m_speed;} + void start( void ); + bool start( int speed ); + bool start( int speed, int duration_ms); + void stop ( void ); + void forward(int speed); + void backward(int speed); + e_status getStatus( void ) { return m_status;} + + void run(void); + +}; + +#endif //MC34933MOTORH \ No newline at end of file diff --git a/src/hall.cpp b/src/hall.cpp new file mode 100644 index 0000000..85cff39 --- /dev/null +++ b/src/hall.cpp @@ -0,0 +1,54 @@ +#include "Arduino.h" +#include "hall.h" +#include "rc_board.h" +#include "Wire.h" + + +void init_cpu( void ) +{ + //setup system clock (12Mhz xtal) + RCC->CR |= (RCC_CR_PLLON_Msk & !RCC_CR_PLLON); //Turn PLL off + RCC->CR |= RCC_HSE_ON; + while (!(RCC->CR & RCC_CR_HSERDY)); + RCC->CFGR &= ~RCC_CFGR_PLLXTPRE; + RCC->CFGR |= (RCC_CFGR_PLLMULL_Msk & RCC_PLL_MUL6); //Set PLL multiplier + RCC->CFGR &= ~RCC_CFGR_PLLSRC; //set PLL to HSE +// Turn On PLL Clock + RCC->CR |= RCC_CR_PLLON; + // Wait Until PLL Is Ready + while (!(RCC->CR & RCC_CR_PLLRDY)); + // Set System To PLL CLock + RCC->CFGR |= RCC_CFGR_SW_PLL; + // Clear All Interrupts + RCC->CIR = 0x009F0000; + +} + +void init_gpio( void ) +{ + // initialize LED digital pin as an output. + pinMode(PIN_SPARE2, OUTPUT); +} + +void init_serial( void ) +{ + // initialize serial communication + Serial.begin(SERIAL_BAUDRATE); + USART1->BRR = 0x271; + Serial.printf("init serial to %i baud",SERIAL_BAUDRATE); +} + +void init_i2c( void ) +{ + Wire.begin(); + Wire.setClock(CLOCK_SPEED_400KHZ); +} + + +void hall_init() +{ + init_cpu(); + init_gpio(); + init_serial(); + init_i2c(); +} diff --git a/src/hall.h b/src/hall.h new file mode 100644 index 0000000..e503c5d --- /dev/null +++ b/src/hall.h @@ -0,0 +1,6 @@ +#ifndef HALLH +#define HALLH + +void hall_init(); + +#endif //HALLH \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d9c12c8 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,177 @@ +/* + * Blink + * Turns on an LED on for one second, + * then off for one second, repeatedly. + */ + +#include +#include +#include "hall.h" +#include "rc_board.h" +#include +#include +#include +//#include "Wire.h" +#include "MC34933.h" + + +bool ms_task_flag = false; +bool decims_task_flag = false; +bool second_task_flag = false; + +//function prototypes +void Tick_handle_msTask (void) +{ + ms_task_flag = true; +} +void Tick_handle_10msTask( void ) +{ + decims_task_flag = true; +} +void Tick_handle_1sTask( void ) +{ + second_task_flag = true; +} + + +Ticker ms_ticker(Tick_handle_msTask, 1, 0, MILLIS); +Ticker decims_ticker(Tick_handle_10msTask, 10, 0, MILLIS); +Ticker second_ticker(Tick_handle_1sTask, 1000,0, MILLIS); + + + +MPU6050 mpu; +MC34933 motor1(PIN_M1A,PIN_M1B); +MC34933 steering(PIN_M2A, PIN_M2B); + +bool blinkState = false; +// Variables will change: +int ledState = LOW; // ledState used to set the LED +unsigned long previousMillis = 0; // will store last time LED was updated +const long interval = 1000; // interval at which to blink (milliseconds) +const long main_loop_interval = 10; +const long second_interval = 100; //number of decims-ticks +unsigned long decims_counter = 0; + +#define MOTORSPEED 1 + + +// ================================================================ +// === INITIAL SETUP === +// ================================================================ + +void handle_msTask (void) +{ +ms_task_flag = true; +} + +void handle_10msTask( void ) +{ + motor1.run(); +} + +void handle_1sTask (void ) +{ + if (ledState == LOW) + { + ledState = HIGH; + motor1.forward(MOTORSPEED); + } + else + { + ledState = LOW; + motor1.backward(MOTORSPEED); + } + Serial.print("."); + digitalWrite(PIN_SPARE2, ledState); +} + +void init_mpu( void ) +{ +// //MPU6050 init +// Serial.println("Initializing I2C devices..."); +// mpu.initialize(); + +// Serial.println(F("Initializing DMP...")); +// devStatus = mpu.dmpInitialize(); + +// // supply your own gyro offsets here, scaled for min sensitivity +// mpu.setXGyroOffset(220); +// mpu.setYGyroOffset(76); +// mpu.setZGyroOffset(-85); +// mpu.setZAccelOffset(1788); // 1688 factory default for my test chip + +// // make sure it worked (returns 0 if so) +// if (devStatus == 0) { +// // turn on the DMP, now that it's ready +// Serial.println(F("Enabling DMP...")); +// mpu.setDMPEnabled(true); + +// // enable Arduino interrupt detection +// Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)...")); + +// attachInterrupt(0, dmpDataReady, RISING); + +// mpuIntStatus = mpu.getIntStatus(); + +// // set our DMP Ready flag so the main loop() function knows it's okay to use it +// Serial.println(F("DMP ready! Waiting for first interrupt...")); +// dmpReady = true; + +// // get expected DMP packet size for later comparison +// packetSize = mpu.dmpGetFIFOPacketSize(); +// } else { +// // ERROR! +// // 1 = initial memory load failed +// // 2 = DMP configuration updates failed +// // (if it's going to break, usually the code will be 1) +// Serial.print(F("DMP Initialization failed (code ")); +// Serial.print(devStatus); +// Serial.println(F(")")); +// } +} + + +void setup() +{ + hall_init(); + + motor1.begin(); + delay(5000); + + + + + + ms_ticker.start(); + decims_ticker.start(); + second_ticker.start(); +} + + +void loop() +{ + + ms_ticker.update(); + decims_ticker.update(); + second_ticker.update(); + + if( ms_task_flag) + { + ms_task_flag = false; + handle_msTask(); + } + + if( decims_task_flag) + { + decims_task_flag = false; + handle_10msTask(); + } + + if( second_task_flag) + { + second_task_flag = false; + handle_1sTask(); + } + +} diff --git a/src/rc_board.h b/src/rc_board.h new file mode 100644 index 0000000..615cc0c --- /dev/null +++ b/src/rc_board.h @@ -0,0 +1,40 @@ +#ifndef RCBOARDH +#define RCBOARDH + +#define PIN_ADC_VBAT PA0 +//NRF24 pins +#define PIN_NRF_CE PA1 +#define PIN_NRF_CSN PA3 +#define PIN_NRF_SCK PA5 +#define PIN_NRF_MISO PA6 +#define PIN_NRF_MOSI PA7 +#define PIN_NRF_IRQ PB1 + +//MPU6050 pins +#define PIN_MPU_INT PB0 +#define PIN_MPU_SDA PB7 +#define PIN_MPU_SCL PB6 + +//MC34933 motordrive pins +#define PIN_M2A PA15 +#define PIN_M2B PA12 +#define PIN_M1A PA10 +#define PIN_M1B PA11 + +//CPU pins +#define PIN_MCU_BOOT1 PB2 +#define PIN_MCU_SWCLK PA14 +#define PIN_MCU_SWDIO PA13 +#define PIN_MCU_TX PA9 //testpoint (label swapped on board v1) +#define PIN_MCU_RX PA8 //testpoint (label swapped on board v1) + +//spare pins +#define PIN_SPARE1 PA4 //testpoint +#define PIN_SPARE2 PA2 //testpoint + +//board settings +#define SERIAL_BAUDRATE 115200 +#define SERIAL_USART1_BRR 0x271 +#define CLOCK_SPEED_400KHZ 400000 + +#endif //RCBOARDH \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..df5066e --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html