Gerard-Jan Gerritsen / train
Committer:
GJGerrits
Date:
Tue Feb 25 12:51:19 2020 +0000
Revision:
0:5b36c1f8c206
First issue

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GJGerrits 0:5b36c1f8c206 1 #ifndef MBED_TRAIN_H
GJGerrits 0:5b36c1f8c206 2 #define MBED_TRAIN_H
GJGerrits 0:5b36c1f8c206 3
GJGerrits 0:5b36c1f8c206 4 #include "mbed.h"
GJGerrits 0:5b36c1f8c206 5 #include "rtos.h"
GJGerrits 0:5b36c1f8c206 6
GJGerrits 0:5b36c1f8c206 7 class Train {
GJGerrits 0:5b36c1f8c206 8
GJGerrits 0:5b36c1f8c206 9 public:
GJGerrits 0:5b36c1f8c206 10 /*Class train
GJGerrits 0:5b36c1f8c206 11 * Based on PWM out pin, and two seperate directions pins.
GJGerrits 0:5b36c1f8c206 12 * Original aim was to use L293 chip, as H-bridge driver, to drive model
GJGerrits 0:5b36c1f8c206 13 * railroad with 12V
GJGerrits 0:5b36c1f8c206 14 * Pin fwd = forward motion
GJGerrits 0:5b36c1f8c206 15 * Pin rwd = reverse motion
GJGerrits 0:5b36c1f8c206 16 * Pin pwm = pwm between 0 and 1 to set speed of train
GJGerrits 0:5b36c1f8c206 17 */
GJGerrits 0:5b36c1f8c206 18
GJGerrits 0:5b36c1f8c206 19 /*
GJGerrits 0:5b36c1f8c206 20 * Example:
GJGerrits 0:5b36c1f8c206 21 * @code
GJGerrits 0:5b36c1f8c206 22 *
GJGerrits 0:5b36c1f8c206 23 * #include "mbed.h"
GJGerrits 0:5b36c1f8c206 24 * #include "rtos.h"
GJGerrits 0:5b36c1f8c206 25 * #include "train.h"
GJGerrits 0:5b36c1f8c206 26 * /////////////////////////Outputs///////////////////////////
GJGerrits 0:5b36c1f8c206 27 * //Motor driver (train)
GJGerrits 0:5b36c1f8c206 28 * // Train train1 (LED3, LED2, LED1); //KL25Z LEDs can be used to simulate motor pins
GJGerrits 0:5b36c1f8c206 29 * Train train1 (PTC5, PTC7, PTD1); //tested on K64F
GJGerrits 0:5b36c1f8c206 30 *
GJGerrits 0:5b36c1f8c206 31 * //DEbug serial port, to demonstrate GetDirection and GetSpeed callback
GJGerrits 0:5b36c1f8c206 32 * Serial pc(USBTX, USBRX);
GJGerrits 0:5b36c1f8c206 33 *
GJGerrits 0:5b36c1f8c206 34 * //Main loop
GJGerrits 0:5b36c1f8c206 35 * int main(){
GJGerrits 0:5b36c1f8c206 36 * //enable train1 to update speed, and wait for commands
GJGerrits 0:5b36c1f8c206 37 * train1.Enable(1);
GJGerrits 0:5b36c1f8c206 38 *
GJGerrits 0:5b36c1f8c206 39 * // continously change direction and accelerate.
GJGerrits 0:5b36c1f8c206 40 * // Changing direction will automatically brake the train to 0 speed.
GJGerrits 0:5b36c1f8c206 41 * while(1) {
GJGerrits 0:5b36c1f8c206 42 * // SetDirection determines direction, 0 = stop, 1 = forward, 2 = reverse
GJGerrits 0:5b36c1f8c206 43 * train1.SetDirection(1);
GJGerrits 0:5b36c1f8c206 44 * wait(2);
GJGerrits 0:5b36c1f8c206 45 * train1.SetSpeed(71);
GJGerrits 0:5b36c1f8c206 46 * wait(2);
GJGerrits 0:5b36c1f8c206 47 * pc.printf("Set direction: %d \r\n", train1.GetDirection());
GJGerrits 0:5b36c1f8c206 48 * pc.printf("Set Speed: %d \r\n", train1.GetSpeed());
GJGerrits 0:5b36c1f8c206 49 * wait(2);
GJGerrits 0:5b36c1f8c206 50 * train1.SetDirection(2);
GJGerrits 0:5b36c1f8c206 51 * wait(2);
GJGerrits 0:5b36c1f8c206 52 * train1.SetSpeed(56);
GJGerrits 0:5b36c1f8c206 53 * wait(2);
GJGerrits 0:5b36c1f8c206 54 * pc.printf("Set direction: %d \r\n", train1.GetDirection());
GJGerrits 0:5b36c1f8c206 55 * pc.printf("Set Speed: %d \r\n", train1.GetSpeed());
GJGerrits 0:5b36c1f8c206 56 * }
GJGerrits 0:5b36c1f8c206 57 * }
GJGerrits 0:5b36c1f8c206 58 * @endcode
GJGerrits 0:5b36c1f8c206 59 */
GJGerrits 0:5b36c1f8c206 60
GJGerrits 0:5b36c1f8c206 61 //Create Train Class
GJGerrits 0:5b36c1f8c206 62 Train(PinName fwd, PinName rwd, PinName pwm);
GJGerrits 0:5b36c1f8c206 63
GJGerrits 0:5b36c1f8c206 64 // AccConvert (make sure acceleration curve stays between 1-4 seconds)
GJGerrits 0:5b36c1f8c206 65 int AccConvert (int _acc);
GJGerrits 0:5b36c1f8c206 66 // SetSpeed to value (value between 0 and 100), Train will automaticatly accelerate/brak to set speed
GJGerrits 0:5b36c1f8c206 67 void SetSpeed(int trainSpeed);
GJGerrits 0:5b36c1f8c206 68 // Update loop, called by interupt, check for speed changes and apply them
GJGerrits 0:5b36c1f8c206 69 void Update();
GJGerrits 0:5b36c1f8c206 70 // Update loop, check if speed is 0 before switching direction
GJGerrits 0:5b36c1f8c206 71 void UpdateDir();
GJGerrits 0:5b36c1f8c206 72 // Init train, now train can receive commands
GJGerrits 0:5b36c1f8c206 73 void Enable(int _acc);
GJGerrits 0:5b36c1f8c206 74 // Disable train
GJGerrits 0:5b36c1f8c206 75 void Disable();
GJGerrits 0:5b36c1f8c206 76 // Direct stop. Abrubtly stops train, setting speed and direction to 0.
GJGerrits 0:5b36c1f8c206 77 // When needing braking, use train.SetSpeed(0);
GJGerrits 0:5b36c1f8c206 78 void DirectStop();
GJGerrits 0:5b36c1f8c206 79 // Set new direction to change direction of train. Train will brake to 0, before switching
GJGerrits 0:5b36c1f8c206 80 void SetDirection(int _dir); // 0 = standstill, 1 = forward, 2 = reverse
GJGerrits 0:5b36c1f8c206 81
GJGerrits 0:5b36c1f8c206 82 // Get the last set direction
GJGerrits 0:5b36c1f8c206 83 int GetDirection();
GJGerrits 0:5b36c1f8c206 84 // Get the last set Speed
GJGerrits 0:5b36c1f8c206 85 int GetSpeed();
GJGerrits 0:5b36c1f8c206 86
GJGerrits 0:5b36c1f8c206 87 //Variables:
GJGerrits 0:5b36c1f8c206 88 int acc;
GJGerrits 0:5b36c1f8c206 89 int CurrentDirection;
GJGerrits 0:5b36c1f8c206 90 int CurrentTrainSpeed;
GJGerrits 0:5b36c1f8c206 91 int NewTrainSpeed;
GJGerrits 0:5b36c1f8c206 92 int NewDirection;
GJGerrits 0:5b36c1f8c206 93
GJGerrits 0:5b36c1f8c206 94
GJGerrits 0:5b36c1f8c206 95 private:
GJGerrits 0:5b36c1f8c206 96 //Motor driver (train)
GJGerrits 0:5b36c1f8c206 97 DigitalOut _fwd; // forward motion
GJGerrits 0:5b36c1f8c206 98 DigitalOut _rwd; // rearward motion
GJGerrits 0:5b36c1f8c206 99 PwmOut _pwm; // Speed setting with pwm
GJGerrits 0:5b36c1f8c206 100 float _current_speed;
GJGerrits 0:5b36c1f8c206 101 Ticker t_SetSpeed;
GJGerrits 0:5b36c1f8c206 102 Ticker t_SetDir;
GJGerrits 0:5b36c1f8c206 103 };
GJGerrits 0:5b36c1f8c206 104
GJGerrits 0:5b36c1f8c206 105 #endif