MCR2 MiniChallenge 4

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Challenges

Mini challenge 4

{Learn, Create, Innovate};


Mini Challenge 4
Introduction

This mini challenge is intended for the student to review the concepts
introduced in all the previous sessions.

• The activity consists of creating an open loop control for the DC


motor that controls the speed of the motor for a given time.

• This activity is divided into two sub activities:

• Perform a simple system identification of the motor system.

• Use the model to design an open loop controller for the DC


Motor.

• The motor will be controlled using an external computer, a


microcontroller, and a motor driver.

• See following slide for requirements.


Mini Challenge 4: Requirements
Mini Challenge 4

Motor Model

Obtain the motor model, as a first order system.

To obtain the motor model, several steps must be performed.

1. Design the “/Motor” node.

• For the characteristics of the ”/Motor” node see following


slides.

2. Establish a sampling time to obtain data from the system.

3. Set an input step to the real motor.

4. Obtain the output velocity.

5. Calibrate the parameters of the model, as introduced in the


presentation “MCR2_Open_Loop_Control”.
Mini Challenge 4
Motor Node
• The “/Motor” node must run on the MCU (Hackerboard or Arduino).

• This node should act as a transducer, to send information for the motor
driver and at the same time, estimating the speed of the motor to be sent to
the controller.

• The node must subscribe and translate the control input “” in the
“/motor_input” topic to a PWM and direction signals to the Motor
Driver.

• The node must estimate the motor speed using the information form
the encoders and publish the data in the topic “/motor_output”.
Mini Challenge 4
Motor Node
• The output of the MCU to the motor driver must be a “PWM signal” and a
direction signal.

• The PWM duty cycle and direction must be mapped to the interval .

• Where the sign represents the direction of the motor, i.e., (+)
CCW rotation, and (-) CW rotation of the motor.

• The duty cycle percentage (%) range .

• The output of the “/Motor” node (publish) to the controller, must be the
estimated angular velocity of the motor in ,in the range . Where the sign
(+.-) represents the direction of rotation obtained from the dual encoder
channel, and the is the angular speed, obtained by counting the pulses of
the encoder for a certain period of time.

*A simple filter example for a noisy signal can be found


here
Mini Challenge 4

Controller Node Motor set point message (/set_point):


float32 input #Set point input to the system
float64 time #User defined time for controlling the motor
The controller node must use the information of the identified system,
to control the speed of a motor for a given period of time.

• The “/Open_Loop_Controller” node must run on External


Computer.

• Must publish to the topic “/motor_input”

• Must be subscribed to the topic “/set_point”

• The message of the setpoint must have the set point value and the
amount of time the user wants to control the motor for. Those
values must be parameters, e.g., the user wants an angular speed of
5 rad/s for 10 s. then the message would look like the following
Motor set point message (/set_point):
Input = 5
Time =10
Mini Challenge 4

Launch File and Plotting Expected Results


• Use the ROS tool “rqt_plot” to plot both signals.

• Make a Launch file to execute both nodes at the same time.

• The Launch file must be able to all the required terminals to


print the information.

• The Launch file must open the rqt_plot and plot all required
signals in the same window.

• The launch file must load the parameter files, automatically.


Mini Challenge 4: Connection Diagrams
Mini Challenge 4: Connection Diagrams
Final Challenge
5. More information about sampling time can be found here, here

Hints , here.

Sampling Time:

• This can be stablished by applying an input value of 1 to the


motor in open loop, using a small sampling time (e.g. 5 ms), and
plot the values of the output speed. The sampling time can be
approximated using the following expression:

where is the settling time of the system.


Mini Challenge 4
Hints (ESP32 PWM)
void setup(){
• The ESP 32 does not use the “analogwrite()” command to generate a
ledcSetup(ledChannel, freq, resolution);
PWM signal.
ledcAttachPin(ledPin, ledChannel);
• To generate a PWM signal the following steps must be followed.
}
1. Choose a PWM channel. There are 16 channels from 0 to 15.

2. Set the PWM signal frequency (980 Hz standard).


void loop(){
3. Set the signal’s duty cycle resolution. From 1 to 16 bits resolution (8
bits typical, 0 to 255 resolution). ledcWrite(ledChannel, dutyCycle);

4. Specify the output GPIO. delay(15);

5. More information can be found here. }


Mini Challenge 4
Hints (Arduino PWM)
• The Arduino “analogWrite(pin, value)” command generates a PWM signal,
into a defined pin. int ledPin = 9;

• pin: the Arduino pin to write to. void setup()


{
• value: the duty cycle: between 0 (always off) and 255 (always on).
pinMode(ledPin, OUTPUT);
• Allowed data types: int. }

• Usually, for control purposes, the interval [0, 1] is mapped to the interval of void loop()

the parameter “value” [0, 255]. {


analogWrite(ledPin,127);
• More information can be found here.
}

BOARD PWM PINS PWM FREQUENCY


490 Hz (pins 5 and 6: 980
Uno, Nano, Mini 3, 5, 6, 9, 10, 11
Hz)
490 Hz (pins 4 and 13: 980
Mega 2 - 13, 44 - 46
Hz)
Mini Challenge 4
• A volatile variable is a variable that is marked or cast with the keyword "volatile" so
Hints (Arduino/ESP32 Interrupts) that it is established that the variable can be changed by some outside factor, such as

• The Arduino “attachInterrupt(digitalPinToInterrupt(pin), ISR, the operating system or other software.

mode)” establishes an interrupt, to a designated pin, triggered by different • In other words, volatile variables, tell the compiler to recheck the value of the

modes. variable from RAM memory (not a copy in other memory) because the value might
change due to an external factor in this case the encoder.
• pin: the Arduino pin where the interrupt is expected.
• More information about interrupts for Arduino can be found here, here and here.
• ISR: the function to call when the interrupt occurs; this function must take • More information about interrupts for ESP32 can be found here, here and here.
no parameters and return nothing.

• Modes:
• LOW to trigger the interrupt whenever the pin is low,

• CHANGE to trigger the interrupt whenever the pin changes value BOARD INTERRUPT PINS PWM FREQUENCY
Uno, Nano, Mini 2,3
• RISING to trigger when the pin goes from low to high, (pins 20 & 21 are not available to use for
Mega 2, 3, 18, 19, 20, 21 interrupts while they are used for I2C
• FALLING for when the pin goes from high to low. communication)
Hackerboard Pins, Encoder channels A
• When using encoders, some variables need to be shared between the ISR ESP32 All pins and B:
ENC_A: Pin 34 and ENC_B: Pin 36
and the other function for this case we use volatile variables.
Mini Challenge 4
Both example codes, read a switch connected to an input and toggle the output value. For both cases, an LED is connected to the output therefore it changes the state of the
LED.
//Arduino Interrupts Example //ESP32 Interrupts Example
const byte ledPin = 13; #define pushButton_pin 33
const byte interruptPin = 2; #define LED_pin 32
volatile byte state = LOW;
void IRAM_ATTR toggleLED() {
void setup() { digitalWrite(LED_pin, !digitalRead(LED_pin));
pinMode(ledPin, OUTPUT); }
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE); void setup() {
} pinMode(LED_pin, OUTPUT);
void loop() { pinMode(pushButton_pin, INPUT_PULLUP);
digitalWrite(ledPin, state); attachInterrupt(pushButton_pin, toggleLED, RISING);
} }

void blink() { void loop() {


state = !state; }
}

*** In this example, as the Arduino example, the toggle of the LED
can be done using a volatile variable.

You might also like