PWM via STM32F446RE Timer on alternative pins in Arduino IDE - timer
I am trying to implement a PWM-signal with the STM32 Timers in Visual Studio Code and in the Arduino IDE. I am using a Nucleo F446RE Board.
I have the following code working well within VSC:
#include <Arduino.h>
HardwareTimer* Timer3 = new HardwareTimer(TIM3);
void setup() {
Timer3->setMode(4, TIMER_OUTPUT_COMPARE_PWM1, PB_1_ALT1);
Timer3->setPrescaleFactor(90);
Timer3->setCaptureCompare(4, 1000, TICK_COMPARE_FORMAT);
Timer3->setOverflow(5000, TICK_FORMAT);
Timer3->resume();
}
void loop() { }
If I try to compile the same code in the Arduino IDE I get the following error:
'PB_1_ALT1' was not declared in this scope
So I tried to include the PeripheralPins.h manually because the PeripheralPins.c defines PB_1_ALT1 but it did not solve the problem. Next I looked the definition of PB_1_ALT1 up which looks like this:
PB_1_ALT1 = PB_1 | ALT1
ALT1 = 0x100
But the Arduino IDE doesn't know ALT1 either, therefore I changed the setMode-command to:
Timer3->setMode(4, TIMER_OUTPUT_COMPARE_PWM1, PB_1 | 0x100);
This time the code gets compiled and it is possible to upload it but the pin still does not output anything.
Therefore my questions:
How do I use the alternative output pins of the timers in the Arduino IDE correctly?
The PeripheralPins.c gives a list of all possible Timer-Channel-Pin-combinations. But where do I find these combinations in the datasheet? I only found them randomly in the code but nowhere in the datasheet of the STM32F446RE.
Solution to question 1!
The PWM-pin combinations are stored in:
[]\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.9.0\variants\NUCLEO_F446RE\PeripheralPins.c
The file does not include the alternative pin combinations with different names like in the library for VSC. Instead you have to uncomment the combination you like to use and comment the normal one.
Hint
Directly editing this library would change the PWM-settings for all programs. Therefore I suggest to copy the whole table into a new c-file in the program's folder. It has to include the corresponding h-file which can be included this in the program.
Own_PeripheralPins.c:
#include "Own_PeripheralPins.h"
#include <Arduino.h>
const PinMap PinMap_PWM[] = {
{PA_0, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 1, 0)}, // TIM2_CH1
// {PA_0, TIM5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 1, 0)}, // TIM5_CH1
// {PA_1, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2
{PA_1, TIM5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 2, 0)}, // TIM5_CH2
// {PA_2, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3 - STLink Tx
// {PA_2, TIM5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 3, 0)}, // TIM5_CH3 - STLink Tx
// {PA_2, TIM9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 1, 0)}, // TIM9_CH1 - STLink Tx
// {PA_3, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4 - STLink Rx
// {PA_3, TIM5, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5, 4, 0)}, // TIM5_CH4 - STLink Rx
// {PA_3, TIM9, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9, 2, 0)}, // TIM9_CH2 - STLink Rx
{PA_5, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 1, 0)}, // TIM2_CH1
// {PA_5, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 1, 1)}, // TIM8_CH1N
// {PA_6, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1
{PA_6, TIM13, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF9_TIM13, 1, 0)}, // TIM13_CH1
// {PA_7, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 1, 1)}, // TIM1_CH1N
// {PA_7, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2
// {PA_7, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 1, 1)}, // TIM8_CH1N
{PA_7, TIM14, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF9_TIM14, 1, 0)}, // TIM14_CH1
{PA_8, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 1, 0)}, // TIM1_CH1
{PA_9, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 2, 0)}, // TIM1_CH2
{PA_10, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 3, 0)}, // TIM1_CH3
{PA_11, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 4, 0)}, // TIM1_CH4
{PA_15, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 1, 0)}, // TIM2_CH1
// {PB_0, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 2, 1)}, // TIM1_CH2N
// {PB_0, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3
{PB_0, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 2, 1)}, // TIM8_CH2N
// {PB_1, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 3, 1)}, // TIM1_CH3N
// {PB_1, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4
{PB_1, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 3, 1)}, // TIM8_CH3N
{PB_2, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 4, 0)}, // TIM2_CH4
{PB_3, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2
{PB_4, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1
{PB_5, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2
{PB_6, TIM4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 1, 0)}, // TIM4_CH1
{PB_7, TIM4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 2, 0)}, // TIM4_CH2
// {PB_8, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 1, 0)}, // TIM2_CH1
{PB_8, TIM4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 3, 0)}, // TIM4_CH3
// {PB_8, TIM10, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM10, 1, 0)}, // TIM10_CH1
// {PB_9, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 2, 0)}, // TIM2_CH2
// {PB_9, TIM4, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM4, 4, 0)}, // TIM4_CH4
{PB_9, TIM11, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM11, 1, 0)}, // TIM11_CH1
{PB_10, TIM2, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2, 3, 0)}, // TIM2_CH3
{PB_13, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 1, 1)}, // TIM1_CH1N
// {PB_14, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 2, 1)}, // TIM1_CH2N
// {PB_14, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 2, 1)}, // TIM8_CH2N
{PB_14, TIM12, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF9_TIM12, 1, 0)}, // TIM12_CH1
// {PB_15, TIM1, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM1, 3, 1)}, // TIM1_CH3N
// {PB_15, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 3, 1)}, // TIM8_CH3N
{PB_15, TIM12, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF9_TIM12, 2, 0)}, // TIM12_CH2
{PC_6, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 1, 0)}, // TIM3_CH1
// {PC_6, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 1, 0)}, // TIM8_CH1
// {PC_7, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 2, 0)}, // TIM3_CH2
{PC_7, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 2, 0)}, // TIM8_CH2
{PC_8, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 3, 0)}, // TIM3_CH3
// {PC_8, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 3, 0)}, // TIM8_CH3
// {PC_9, TIM3, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3, 4, 0)}, // TIM3_CH4
{PC_9, TIM8, STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM8, 4, 0)}, // TIM8_CH4
{NC, NP, 0}
};
Important: The attribute weak has to be removed!
Important: This table has to be adjusted for the individually used STM-type!
I would prefer directly defining the PinMap_PWM in the header file but I don't know how to do this or wheter it is even possible. If the file is included by multiple other files, I get a multiple definition error. Neither it is possible to set the pointer constant nor static, because there would be different types of defition of the variable.
Related
Identify rows of one numpy array that gave rise to another numpy array
I think I'm missing something obvious. Consider the following code: import numpy as np a = np.array([[ 0, 2, 0, 5, 4, 6, 2, 4], [ 3, 4, 0, 1, 0, 7, 4, 6], [ 2, 6, 3, 5, 2, 5, 5, 8], [ 0, 1, 0, 8, 0, 5, 8, 10], [ 7, 9, 2, 7, 0, 6, 7, 2], [ 0, 1, 4, 9, 0, 7, 9, 9], [ 0, 6, 7, 5, 6, 2, 4, 13], [ 0, 1, 1, 4, 1, 3, 2, 3]] # isolate columns 2,3,6,7 mask = [False,False, True, True,False,False, True, True] b = a[:,mask] # determine rows of b having unique elements s = np.sort(b, axis=1) c = b[~(s[:,:-1] == s[:,1:]).any(1)] c looks like: c = [[ 0, 5, 2, 4], [ 0, 1, 4, 6], [ 7, 5, 4, 13], [ 1, 4, 2, 3]] QUESTION: How do I 'recover' the rows of a that gave rise to the rows of c? The output should be like: d = [[ 0, 2, 0, 5, 4, 6, 2, 4], [ 3, 4, 0, 1, 0, 7, 4, 6], [ 0, 6, 7, 5, 6, 2, 4, 13], [ 0, 1, 1, 4, 1, 3, 2, 3]]
How to get depth of tree of arrays given total num items and max array size?
Given this divide algorithm and sample data: const data = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ] function divide(data, size) { const result = [] for (let i = 0; i < data.length; i += size) { const chunk = data.slice(i, i + size); result.push(chunk) } if (result.length > size) { return divide(result, size) } return result; } const result = divide(data, 5); console.log(result) How do you calculate the number of levels in the resulting tree? In this case of block size 5, I think there are 3 are 4 levels about, but what is the equation to use so you don't have to compute the actual tree? Basically getDepth(numItems, blockSize)?
Every recursive call reduces the input size by a factor of blocksize. Starting with your example of 100 items. This is grouped into 20, these 20 are grouped into 4 and the algorithm ends. The expression that capture this is log with a base of blocksize. f(n,m) = ceil(log_base_m(n))
The depth of the example tree is ceil(log5(99)) In general: ceil(logchunksize(datasize-1))
How do I return a Random number every time in Math Random.?
I just have a question, I have been building alexa skill. (the Concept is Randomize Positivity) but somehow it keep returning the same sentence.... this is my code... ` for (var j = 0; j < index; j++){ var rand = Math.floor(Math.random() * index); index -= 1; var temp = indexList[index]; indexList[index] = indexList[rand]; indexList[rand] = temp; }`// I also swap the words so It would sure not to return the same but somehow it happens.. and the index is about 15 strings for now... my Question is how do I make it random as possible?
You're just trying to shuffle an array, right? See the Fisher-Yates shuffle. You're already almost doing the same thing. I tried this and it worked fine. var indexList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; var rand; var temp; for (let i = 0; i < indexList.length; i++) { rand = Math.floor(Math.random() * (i + 1)); temp = indexList[i]; indexList[i] = indexList[rand]; indexList[rand] = temp; console.log(indexList); } It printed the following, showing that it is shuffling the array more and more each step: > Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] > Array [2, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] > Array [3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] > Array [4, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] > Array [4, 1, 5, 3, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] > Array [4, 6, 5, 3, 2, 1, 7, 8, 9, 10, 11, 12, 13, 14, 15] > Array [4, 6, 5, 3, 2, 1, 7, 8, 9, 10, 11, 12, 13, 14, 15] > Array [4, 6, 5, 3, 2, 8, 7, 1, 9, 10, 11, 12, 13, 14, 15] > Array [4, 6, 5, 3, 2, 8, 9, 1, 7, 10, 11, 12, 13, 14, 15] > Array [4, 6, 5, 3, 2, 8, 9, 1, 10, 7, 11, 12, 13, 14, 15] > Array [4, 6, 5, 3, 2, 8, 9, 1, 10, 7, 11, 12, 13, 14, 15] > Array [4, 6, 5, 3, 2, 8, 9, 1, 12, 7, 11, 10, 13, 14, 15] > Array [4, 6, 5, 3, 2, 8, 9, 1, 12, 7, 11, 10, 13, 14, 15] > Array [4, 6, 14, 3, 2, 8, 9, 1, 12, 7, 11, 10, 13, 5, 15] > Array [4, 6, 15, 3, 2, 8, 9, 1, 12, 7, 11, 10, 13, 5, 14]
Merge arrays inside of arrays
I have this array: a = [[1,2,3,4,5],[3,5,6,8,12,45],[3,2,1,5,7,9,10,11],[3,5,6,8,2,1,3,4,6]] I want to merge its inner arrays so that they become: a = [[1,2,3,4,5,3,5,6,8,12,45],[3,2,1,5,7,9,10,11,3,5,6,8,2,1,3,4,6]] How can I do this?
You need to do a = [ [1, 2, 3, 4, 5], [3, 5, 6, 8, 12, 45], [3, 2, 1, 5, 7, 9, 10, 11], [3, 5, 6, 8, 2, 1, 3, 4, 6] ] a.each_slice(2).map(&:flatten) # => [ # [1, 2, 3, 4, 5, 3, 5, 6, 8, 12, 45], # [3, 2, 1, 5, 7, 9, 10, 11, 3, 5, 6, 8, 2, 1, 3, 4, 6] # ] Read the method each_slice(n) Iterates the given block for each slice of n elements. If no block is given, returns an enumerator.
Testing if a player is standing on certain tiles of an integer array
I am trying to make a game in which a player will only fall if they aren't standing on specific tiles. My map is generated using an integer array that corresponds with various tile types. I was wondering if there is any way to test if the tile below the player is a certain tile, besides making an if else statement for every tile. I have given an example of one of the maps and how I am testing if a player is standing on it. int[][] map3 = { //grid3 { 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6 }, { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 }, { 6, 6, 6, 6, 6, 0, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6 }, { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 }, { 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6 }, { 1, 1, 1, 1, 4, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 0 }, { 9, 9, 9, 9, 9, 5, 1, 1, 1, 1, 1, 4, 6, 6, 6, 6 }, { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 5, 1, 1, 1 }, { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, }; if (grid3.getTile( (int) playerX, (int) playerY) != grid3.map[7][4]) { player.setY((float) ((float) player.getY() - 0.7)); } I would repeat this process for every ground tile.
You should just be able to use nested for loops to iterate over your arrays. It would look something like this. for(int c = 0; c < grid3.map.length; c++){ for(int x = 0; x < grid3.map[c].length){ if (grid3.getTile( (int) playerX, (int) playerY) != grid3.map[c][x]) player.setY((float) ((float) player.getY() - 0.7)); } }