Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mypidror1 Map
Revision 0:1825e249cba5, committed 2019-12-27
- Comitter:
- noamnahum
- Date:
- Fri Dec 27 17:29:23 2019 +0000
- Commit message:
- changes;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Map.lib Fri Dec 27 17:29:23 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/Generic/code/Map/#dad975e2e150
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.cpp Fri Dec 27 17:29:23 2019 +0000 @@ -0,0 +1,46 @@ +/* mbed simple H-bridge motor controller + * Copyright (c) 2007-2010, sford, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "Motor.h" + +#include "mbed.h" + +Motor::Motor(PinName pwm, PinName fwd, PinName rev): + _pwm(pwm), _fwd(fwd), _rev(rev) { + + // Set initial condition of PWM + _pwm.period(0.00001); + _pwm = 0; + + // Initial condition of output enables + _fwd = 0; + _rev = 0; +} + +void Motor::speed(float speed) { + _fwd = (speed > 0.0); + _rev = (speed < 0.0); + _pwm = abs(speed); +} + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motor.h Fri Dec 27 17:29:23 2019 +0000 @@ -0,0 +1,56 @@ +/* mbed simple H-bridge motor controller + * Copyright (c) 2007-2010, sford, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MBED_MOTOR_H +#define MBED_MOTOR_H + +#include "mbed.h" + +/** Interface to control a standard DC motor + * + * with an H-bridge using a PwmOut and 2 DigitalOuts + */ +class Motor { +public: + + /** Create a motor control interface + * + * @param pwm A PwmOut pin, driving the H-bridge enable line to control the speed + * @param fwd A DigitalOut, set high when the motor should go forward + * @param rev A DigitalOut, set high when the motor should go backwards + */ + Motor(PinName pwm, PinName fwd, PinName rev); + + /** Set the speed of the motor + * + * @param speed The speed of the motor as a normalised value between -1.0 and 1.0 + */ + void speed(float speed); + +protected: + PwmOut _pwm; + DigitalOut _fwd; + DigitalOut _rev; + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Dec 27 17:29:23 2019 +0000 @@ -0,0 +1,90 @@ +#include "PID.h" +#include "Motor.h" +#include <Map.hpp> +#include "mbed.h" +#define RATE 0.1 + +AnalogIn analog_value1(PA_0); +AnalogIn analog_value2(PA_1); +AnalogIn analog_value3(PA_3); +AnalogIn analog_value4(PA_4); +Motor myMotor(PA_8, PA_5, PA_6); +int counter = 0; +int counter1 = 0; +//static float setpoint, feedback, output; +const float output_lower_limit = -255; +const float output_upper_limit = 255; +const float kp = 1.5; +const float ki = 1; +const float kd = 0.001; +const float Ts = 0.001; +float pedal1, pedal2, matzeret1, matzeret2, sumpedal, summetzeret, subpedal, submetzeret; +float mdagree = 0, pdagree = 0; +float mypedal,sumPedal,mythorttle,sumthorttle = 0; +float speed = 0; +Map mapvaltovolt = Map(0, 1, 0, 3300); +Map pedaltodagree = Map(865, 1220, 0, 255); +Map mtodagree = Map(490, 3150, 0, 255); +Map nspeed = Map(-255, 255, -1, 1); +PID pid(&pdagree, &mdagree, &speed, output_lower_limit, output_upper_limit,kp, ki, kd, Ts); +Ticker sensor_sample_timer; +Ticker motor_cmd_timer; + +//DigitalOut led(LED1); +Serial pc(USBTX,USBRX); +void commandMotor(){ + counter1++; + if (counter1 == 20){ + pc.printf("Error %.4f\n\r", pid.getError()); + counter1 = 0; + } + float Motorcommand = nspeed.Calculate(speed); + //pc.printf("Motor speed %.4f\n\r", Motorcommand); + if (Motorcommand > 1) { + myMotor.speed(1); + } + else { + myMotor.speed(Motorcommand); + } +} +void readSensors(){ + + pedal1 = analog_value1.read(); // Converts and read the analog input value (value from 0.0 to 1.0) + pedal2 = analog_value2.read(); // Converts and read the analog input value (value from 0.0 to 1.0) + matzeret1 = analog_value3.read(); + pedal1 = mapvaltovolt.Calculate(pedal1); + pedal2 = mapvaltovolt.Calculate(pedal2); + matzeret1 = mapvaltovolt.Calculate(matzeret1); + /*matzeret2 = analog_value4.read(); + matzeret2 = matzeret2 * ConvertToVolt; */ + //pc.printf("pedal1 is: %.4f, matzeret1 is:%.4f\n\r", pedal1, matzeret1); + sumpedal = pedal1+pedal2; + //pc.printf("sumpedal is: %.4f\n\r", sumpedal); + subpedal = abs(3500-sumpedal); + //pc.printf("Subpedal is: %.4f\n\r", subpedal); + if (subpedal<175) { + mypedal = pedaltodagree.Calculate(pedal1); + sumPedal = sumPedal + mypedal; + //setpoint = throttleCmd; + //pc.printf("setpedal dagree %.4f\n\r", pdagree); + mythorttle = mtodagree.Calculate(matzeret1); + sumthorttle = sumthorttle + mythorttle; + //feedback = throttlePosition; + //pc.printf("processvalue dagree %.4f\n\r", mdagree); + counter++; + if (counter == 10) { + pdagree = sumPedal/10; + mdagree = sumthorttle/10; + counter = 0; + sumPedal = 0; + sumthorttle = 0; + } + + } +} +int main() { + sensor_sample_timer.attach(readSensors, 0.0001); + pid.start(); + motor_cmd_timer.attach(commandMotor, 0.0005); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Dec 27 17:29:23 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mypid.lib Fri Dec 27 17:29:23 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/noamnahum/code/mypidror1/#f5c84347bfca