ST Micro in C Code

MrChips

Joined Oct 2, 2009
30,821
There is nothing special about the code to control the motors.

I am using L298N motor controllers. This is a dual H-bridge which allows me to RUN-STOP-REVERSE the motor.
My control program turns on the motor in the direction I need to go and stops when the position is reached.
It simply uses two GPIO output pins to control the L298N H-bridge.
 

Thread Starter

beatsal

Joined Jan 21, 2018
401
There is nothing special about the code to control the motors.

I am using L298N motor controllers. This is a dual H-bridge which allows me to RUN-STOP-REVERSE the motor.
My control program turns on the motor in the direction I need to go and stops when the position is reached.
It simply uses two GPIO output pins to control the L298N H-bridge.
Does it work? How do you manoeuvre around obstacles - by sensors?
 

MrChips

Joined Oct 2, 2009
30,821
That is the reason why I asked what is the microcontroller supposed to do?
You need intelligence.
In order for your robot to move intelligently it has to have sensory input.
How can the robot tell the difference between snow and gravel, or snow and the neighbor's dog?

Yes, you need sensors.
For what you are attempting to do, you will need a camera, color and pattern recognition.
You will need an ultrasonic range finder in order to avoid bumping into a rock or trying to shovel a rock thinking that it is snow.

How does the robot tell the difference between soft snow and ice?
How does the robot turn? How much does it turn? 30 degrees? 75 degrees?
You need a compass.

How does the robot know where to dump the snow?
Just to be able to move from point A to point B and return to point A requires intelligence and a guidance system.

Questions. Lots of questions to be answered.
 

Thread Starter

beatsal

Joined Jan 21, 2018
401
That is the reason why I asked what is the microcontroller supposed to do?
You need intelligence.
In order for your robot to move intelligently it has to have sensory input.
How can the robot tell the difference between snow and gravel, or snow and the neighbor's dog?

Yes, you need sensors.
For what you are attempting to do, you will need a camera, color and pattern recognition.
You will need an ultrasonic range finder in order to avoid bumping into a rock or trying to shovel a rock thinking that it is snow.

How does the robot tell the difference between soft snow and ice?
How does the robot turn? How much does it turn? 30 degrees? 75 degrees?
You need a compass.

How does the robot know where to dump the snow?
Just to be able to move from point A to point B and return to point A requires intelligence and a guidance system.

Questions. Lots of questions to be answered.
What microcontroller do you use?
 

Thread Starter

beatsal

Joined Jan 21, 2018
401
Do you know of anything this complex that works as planned first time around?
btw, this is work in progress.
Struggling with C, CANNOT UNDERSTAND THIS CODE:
C:
/*----------------------------------------------------------------------------
thrBUT: check button state
*----------------------------------------------------------------------------*/
__NO_RETURN static void thrBUT(void *argument) {
  uint32_t button_msk = (1U << Buttons_GetCount()) - 1U;
  (void)argument;
  for (;;) {
osDelay(delay_val); /* Wait */
while (Buttons_GetState() & (button_msk)); /* Wait while holding USER button */
osThreadFlagsSet(tid_thrLED, 0x0001U);
  }
}
 

MrChips

Joined Oct 2, 2009
30,821
Not exactly how I would code it.
However, here goes.
Let us say that you have a 16-bit port. For example PORTB.
16 pins are numbered 15 to 0 going from LEFT to RIGHT.

15-14-13-12-11-10-9-8-7-6-5-4-3-2-1-0

Suppose you have a switch connected to PB5, i.e. bit-5 on PORTB.
Buttons_GetCount( ) presumably returns a value 6 (and I don't know why).

1U means the constant value 1 as a 32-bit integer.
1U is the same as 0000 0000 0000 0000 0000 0000 0000 0001

6 - 1U = 5

x << n means shift the value x by n positions to the left.

1 << 5 becomes 0000 0000 0000 0000 0000 0000 0010 0000

We have created a MASK with a 1 in bit position 5, all other bits are 0.

When we go to evaluate
( Buttons_GetState() & (button_msk) )
we are looking only at bit-5 of the port where the buttons are connected.

Generally, automatic code composers have to use generic ways of connecting with the hardware not knowing where the user has connected input and output pins. This is a cumbersome way of creating code. There are simpler and more direct ways of doing the same thing and in general will be more efficient in code space and execution.

It is best to learn programming by doing simple things on your own rather than trying to understand someone else's code or code generated by a code wizard.
 

MrChips

Joined Oct 2, 2009
30,821
oops. Sorry I misread the braces in the code.
The -1U is executed after the register is shifted.

uint32_t button_msk = (1U << Buttons_GetCount()) - 1U;

Hence
1 << 5 becomes
0000 0000 0000 0000 0000 0000 0010 0000

then we subtract 1 resulting in
0000 0000 0000 0000 0000 0000 0001 1111

indicating that we have selected 5 inputs, from PB4 to PB0.

When we go to evaluate
( Buttons_GetState() & (button_msk) )
we are looking at 5 inputs simultaneously, PB4 to PB0.
 

MrChips

Joined Oct 2, 2009
30,821
Presumably, I can only guess, you are looking at code generated by a code wizard for a RTOS.

If you are now learning how to program in C, this would not be my recommended line of approach.
Begin by programming simple constructs using a generic C platform.
There are C compilers that will run and execute directly on your PC.
 
Top